Skip to content

Commit

Permalink
Fix #22 with better hash and added support for new authors in contrib…
Browse files Browse the repository at this point in the history
…utor modal
  • Loading branch information
TheRolfFR committed Jan 12, 2025
1 parent ffc6f96 commit c7a281b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 39 deletions.
1 change: 1 addition & 0 deletions pages/contribution/contribution-form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
:error-messages="
content.length === 0 ? [$root.lang('database.subtitles.no_contributor_yet')] : []
"
@newUsers="(l) => this.$emit('newUser', l) "
/>
</v-col>
</v-row>
Expand Down
113 changes: 74 additions & 39 deletions pages/contribution/contribution-modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<v-list-item-title>{{ panelLabels[form.formId] }}</v-list-item-title>
<v-list-item-subtitle class="text-truncate">
<span v-if="form.authors.length">
{{ contributorsFromIds(form.authors) }}
{{ formAuthorNames[form.formId] || '...' }}
</span>
<i v-else>{{ $root.lang("database.subtitles.no_contributor_yet") }}</i>
</v-list-item-subtitle>
Expand Down Expand Up @@ -101,11 +101,17 @@
:disabled="activeForm === undefined"
:contributors="contributors"
:multiple="multiple"
@newUser="
(l) => {
this.contributors = l;
}
"
/>
</modal-form>
</template>

<script>
import axios from "axios";
import moment from "moment";
import ModalForm from "@components/modal-form.vue";
Expand Down Expand Up @@ -143,11 +149,57 @@ export default {
modalOpened: false,
closeOnSubmit: true,
formRecords: {},
formAuthorNames: {},
packsList: [],
lastFormId: 0,
openedFormId: undefined,
};
},
computed: {
activeForm() {
if (this.openedFormId === undefined) return undefined;
const formObj = this.formRecords[this.openedFormId];
if (formObj === undefined) return undefined;
const res = JSON.parse(JSON.stringify(formObj));
return res;
},
formRecordsList() {
return Object.values(this.formRecords);
},
formRecordsLength() {
return this.formRecordsList.length;
},
formRecordsAuthors() {
return Object.entries(this.formRecords).map(([formID, form]) => ([ formID, form.authors ]));
},
panelLabels() {
return Object.entries(this.formRecords)
.map(([formID, form]) => [
formID,
`${this.formatPack(form.pack)}${moment(new Date(form.date)).format("ll")}`,
])
.reduce((acc, [formID, formLabel]) => {
acc[formID] = formLabel;
return acc;
}, {});
}
},
watch: {
formRecordsAuthors: {
handler: function(formEntriesAuthorIds) {
Promise.all(formEntriesAuthorIds.map(async ([formID, formAuthorsIDs]) => [
formID, await this.contributorsFromIds(formAuthorsIDs)
])).then((allAuthorNames) => {
this.formAuthorNames = allAuthorNames.reduce((acc, [formID, formAuthorNames]) => {
acc[formID] = formAuthorNames;
return acc;
}, {});
})
},
deep: true
}
},
methods: {
addNewForm() {
// create new form
Expand Down Expand Up @@ -192,23 +244,33 @@ export default {
this.openedFormId = createdFormObj.formId;
this.closeOnSubmit = !!closeOnSubmit;
},
contributorsFromIds(authorIds) {
async contributorsFromIds(authorIds) {
if (!authorIds || authorIds.length === 0) return "";
const contributorNames = this.contributors
.filter((c) => authorIds.includes(c.id))
.map((c) => c.username);
const total = authorIds.length;
const alreadyAuthors = this.contributors.filter((c) => authorIds.indexOf(c.id) !== -1);
const alreadyAuthorsIds = alreadyAuthors.map(c => c.id);
const notAlreadyAuthorsIds = authorIds.filter((id) => alreadyAuthorsIds.indexOf(id) === -1);
const searchIDsparam = notAlreadyAuthorsIds.join(',')
const notAlreadyAuthorsFound = (!searchIDsparam) ? [] :
await axios.get(`${this.$root.apiURL}/users/${searchIDsparam}`)
.then((res) => res.data)
.then((data) => Array.isArray(data) ? data : [data]);
const total = contributorNames.length;
const anonymousTotal = contributorNames.filter((username) => !username).length;
const knownNames = contributorNames.filter((username) => username);
const allAuthorsFound = [...alreadyAuthors, ...notAlreadyAuthorsFound].filter((v, i, a) => a.indexOf(v) === i);
const anonymousTotal = allAuthorsFound.filter(a => !a.username).length;
const notAnonymous = allAuthorsFound.filter(a => !!a.username);
const notAnonymousNames = notAnonymous.map(user => user.username);
let allNames = notAnonymousNames;
if (anonymousTotal > 0) {
const anonymousStr = `${anonymousTotal} ${this.$root.lang("database.labels.anonymous")}`;
knownNames.splice(0, 0, anonymousStr);
allNames.splice(0, 0, anonymousStr); // insert first anonymous
}
return `[${total}]: ${knownNames.join(", ")}`;
allNames = allNames.filter((v, i, a) => a.indexOf(v) === i);
return `[${total}]: ${allNames.join(", ")}`;
},
changeOpenedForm(formId) {
if (this.openedFormId === formId) this.openedFormId = undefined;
Expand Down Expand Up @@ -248,8 +310,7 @@ export default {
};
},
getNewFormId() {
this.lastFormId++;
return String(this.lastFormId);
return crypto.randomUUID();
},
onFormInput(form) {
// stop undefined object
Expand Down Expand Up @@ -286,31 +347,5 @@ export default {
this.$set(this, "formRecords", newFormRecords); // affect
},
},
computed: {
activeForm() {
if (this.openedFormId === undefined) return undefined;
const formObj = this.formRecords[this.openedFormId];
if (formObj === undefined) return undefined;
const res = JSON.parse(JSON.stringify(formObj));
return res;
},
formRecordsList() {
return Object.values(this.formRecords);
},
formRecordsLength() {
return this.formRecordsList.length;
},
panelLabels() {
// faster than using Object.entries + reduce
const acc = {};
for (const formID of Object.keys(this.formRecords)) {
const form = this.formRecords[formID];
acc[formID] = `${this.formatPack(form.pack)}${moment(new Date(form.date)).format("ll")}`;
}
return acc;
},
},
};
</script>

0 comments on commit c7a281b

Please sign in to comment.