Skip to content

Commit

Permalink
Change submission language
Browse files Browse the repository at this point in the history
  • Loading branch information
jyhein committed Feb 23, 2024
1 parent 564991c commit 1cded2a
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 91 deletions.
139 changes: 139 additions & 0 deletions src/components/Container/ChangeSubmissionLanguage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<template>
<pkp-form
v-bind="form"
@set="
(formId, data) =>
$emit('updateSubmissionLangMetadataFormData', formId, data)
"
@success="changeLanguage"
></pkp-form>
</template>

<script type="text/javascript">
import PkpForm from '@/components/Form/Form.vue';
import ajaxError from '@/mixins/ajaxError';
export default {
name: 'ChangeSubmissionLanguage',
components: {
PkpForm,
},
mixins: [ajaxError],
props: {
form: {
type: Object,
required: true,
},
isModalOpenedChangeSubmissionLanguage: {
type: Boolean,
required: true,
},
submission: {
type: Object,
required: true,
},
submissionApiUrl: {
type: String,
required: true,
},
workingPublication: {
type: Object,
required: true,
},
},
data() {
return {
formMetadataId: pkp.const.FORM_CHANGE_SUBMISSION_LANGUAGE_METADATA,
formLocaleId: pkp.const.FORM_CHANGE_SUBMISSION_LANGUAGE,
};
},
created() {
/**
* Subscribe to publication forms and update the publication
* with the details that have changed
*/
pkp.eventBus.$on('form-success', (formId) => {
if (formId === this.formLocaleId) {
window.location.reload();
}
});
},
unmounted() {
pkp.eventBus.$off('form-success');
},
methods: {
/**
* Change submission language and update contributor props
*/
async changeLanguage() {
const oldLocale = this.submission.locale;
const newLocale = this.form.primaryLocale;
const self = this;
return editContributors().then(
submitLang().then(() => {
self.$emit('updateIsModalOpenedChangeSubmissionLanguage', false);
}, $.noop),
$.noop,
);
async function editContributors() {
if (newLocale === oldLocale) return;
return $.when(
...self.workingPublication.authors.map((author) => {
return send(
`${self.submissionApiUrl}/publications/${self.workingPublication.id}/contributors/${author.id}`,
edit(author),
);
}),
);
function edit(author) {
// Only edit if given name empty
if (author['givenName'][newLocale] !== '') return {};
return ['givenName', 'familyName', 'preferredPublicName'].reduce(
(editedProps, prop) => {
if (author[prop][newLocale] === '') {
editedProps[prop] = {[newLocale]: author[prop][oldLocale]};
}
return editedProps;
},
{},
);
}
}
async function submitLang() {
return send(self.submissionApiUrl, {locale: newLocale}, (r) =>
pkp.eventBus.$emit('form-success', self.formLocaleId, r),
);
}
async function send(url, data, success = $.noop) {
return $.ajax({
url: url,
method: 'POST',
headers: {
'X-Csrf-Token': pkp.currentUser.csrfToken,
'X-Http-Method-Override': 'PUT',
},
data: data,
success(r) {
success(r);
},
error(r) {
self.ajaxErrorCallback(r);
},
});
}
},
},
};
</script>

<style lang="less">
// Hide formlocales of change language metadata form
#changeSubmissionLanguage form .pkpFormLocales {
display: none;
}
</style>
100 changes: 9 additions & 91 deletions src/components/Container/WorkflowPage.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script type="text/javascript">
import Page from './Page.vue';
import ChangeSubmissionLanguage from '@/components/Container/ChangeSubmissionLanguage.vue';
import ContributorsListPanel from '@/components/ListPanel/contributors/ContributorsListPanel.vue';
import PublicationSectionJats from '@/pages/workflow/PublicationSectionJats.vue';
import Composer from '@/components/Composer/Composer.vue';
Expand All @@ -13,6 +14,7 @@ import dialog from '@/mixins/dialog.js';
export default {
name: 'WorkflowPage',
components: {
ChangeSubmissionLanguage,
ContributorsListPanel,
Composer,
Dropdown,
Expand Down Expand Up @@ -44,7 +46,6 @@ export default {
submissionFileApiUrl: '',
submissionLibraryLabel: '',
submissionLibraryUrl: '',
submissionSupportedLocales: [],
supportsReferences: false,
statusLabel: '',
unpublishConfirmLabel: '',
Expand Down Expand Up @@ -137,10 +138,6 @@ export default {
this.submission.currentPublicationId = newPublication.id;
this.refreshSubmission();
}
if (formId === pkp.const.FORM_CHANGE_SUBMISSION_LANGUAGE) {
window.location.reload();
}
});
/**
Expand Down Expand Up @@ -186,78 +183,6 @@ export default {
pkp.eventBus.$off('recommendation:revisions');
},
methods: {
/**
* Change submission language and update contributor props
*/
async changeSubmissionLanguage() {
const oldLocale = this.submission.locale;
const newLocale =
this.components[pkp.const.FORM_CHANGE_SUBMISSION_LANGUAGE_METADATA]
.primaryLocale;
const self = this;
return editContributors().then(
submitLang().then(() => {
this.isModalOpenedChangeSubmissionLanguage = false;
}, $.noop),
$.noop,
);
async function editContributors() {
if (newLocale === oldLocale) return;
return $.when(
...self.workingPublication.authors.map((author) => {
return send(
`${self.submissionApiUrl}/publications/${self.workingPublication.id}/contributors/${author.id}`,
edit(author),
);
}),
);
function edit(author) {
// Only edit if given name empty
if (author['givenName'][newLocale] !== '') return {};
return ['givenName', 'familyName', 'preferredPublicName'].reduce(
(editedProps, prop) => {
if (author[prop][newLocale] === '') {
editedProps[prop] = {[newLocale]: author[prop][oldLocale]};
}
return editedProps;
},
{},
);
}
}
async function submitLang() {
return send(self.submissionApiUrl, {locale: newLocale}, (r) =>
pkp.eventBus.$emit(
'form-success',
pkp.const.FORM_CHANGE_SUBMISSION_LANGUAGE,
r,
),
);
}
async function send(url, data, success = $.noop) {
return $.ajax({
url: url,
method: 'POST',
headers: {
'X-Csrf-Token': pkp.currentUser.csrfToken,
'X-Http-Method-Override': 'PUT',
},
data: data,
success(r) {
success(r);
},
error(r) {
self.ajaxErrorCallback(r);
},
});
}
},
/**
* Create a new version of the latest publication
*/
Expand Down Expand Up @@ -669,16 +594,14 @@ export default {
updateSubmissionLangMetadataFormData(formId, data) {
this.set(formId, data);
const form = this.components[formId];
const oldLocale = form.primaryLocale;
const newLocale = data.fields ? data.fields[0].value : oldLocale;
const newLocale = data.fields ? data.fields[0].value : form.primaryLocale;
if (newLocale === oldLocale) return;
form.primaryLocale = newLocale;
form.visibleLocales = [newLocale];
Object.keys(data).forEach(function (key) {
form[key] = data[key];
});
if (newLocale !== form.primaryLocale) {
this.set(formId, {
primaryLocale: newLocale,
visibleLocales: [newLocale],
});
}
},
/**
Expand Down Expand Up @@ -833,9 +756,4 @@ export default {
#representations-grid {
padding-top: 2rem;
}
// Hide formlocales of change language metadata form
#changeSubmissionLanguage form .pkpFormLocales {
display: none;
}
</style>

0 comments on commit 1cded2a

Please sign in to comment.