Skip to content

Commit

Permalink
Change submission language
Browse files Browse the repository at this point in the history
  • Loading branch information
jyhein committed Jul 10, 2024
1 parent 76b4148 commit 2a032ef
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 6 deletions.
40 changes: 40 additions & 0 deletions src/components/Container/WorkflowPage.vue
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<script type="text/javascript">
import Page from './Page.vue';
import ChangeSubmissionLanguage from '@/pages/workflow/ChangeSubmissionLanguage.vue';
import ContributorsListPanel from '@/components/ListPanel/contributors/ContributorsListPanel.vue';
import PublicationSectionJats from '@/pages/workflow/PublicationSectionJats.vue';
import Composer from '@/components/Composer/Composer.vue';
import Dropdown from '@/components/Dropdown/Dropdown.vue';
import Modal from '@/components/Modal/Modal.vue';
import PkpHeader from '@/components/Header/Header.vue';
import {useModal} from '@/composables/useModal';
import LocalizeSubmission from '@/mixins/localizeSubmission.js';
import ajaxError from '@/mixins/ajaxError';
import dialog from '@/mixins/dialog.js';
export default {
name: 'WorkflowPage',
components: {
ChangeSubmissionLanguage,
ContributorsListPanel,
Composer,
Dropdown,
Expand All @@ -26,6 +29,8 @@ export default {
return {
activityLogLabel: '',
canAccessPublication: false,
canChangeSubmissionLanguage: false,
currentSubmissionLanguageLabel: '',
canEditPublication: false,
currentPublication: null,
decisionUrl: '',
Expand Down Expand Up @@ -57,6 +62,7 @@ export default {
workingPublication: null,
isModalOpenedSelectRevisionDecision: false,
isModalOpenedSelectRevisionRecommendation: false,
openSideModal: null,
};
},
computed: {
Expand Down Expand Up @@ -161,6 +167,12 @@ export default {
* Load forms
*/
this.setPublicationForms(this.workingPublication);
/**
* ModalStore
*/
const {openSideModal} = useModal();
this.openSideModal = openSideModal;
},
mounted() {
/**
Expand All @@ -177,6 +189,7 @@ export default {
pkp.eventBus.$off('unpublish:publication');
pkp.eventBus.$off('decision:revisions');
pkp.eventBus.$off('recommendation:revisions');
pkp.eventBus.$off('change-submission-language:form');
},
methods: {
/**
Expand Down Expand Up @@ -299,6 +312,19 @@ export default {
).pkpHandler('$.pkp.controllers.modal.AjaxModalHandler', opts);
},
/**
* Open a modal displaying the change submission language form
*/
openChangeSubmissionLanguageModal() {
this.openSideModal(ChangeSubmissionLanguage, {
form: this.components[
pkp.const.FORM_CHANGE_SUBMISSION_LANGUAGE_METADATA
],
/* for test */ publicationId: this.workingPublication.id,
submissionId: this.submission.id,
});
},
/**
* Open a modal to prompt the user to confirm creation of
* a new version
Expand Down Expand Up @@ -722,6 +748,20 @@ export default {
}
}
.pkpSubmission__localeNotSupported {
margin: 0 -2rem;
padding: 1rem;
background: @primary;
font-size: @font-sml;
color: #fff;
text-align: center;
}
.pkpPublication__changeSubmissionLanguage {
display: block;
padding-bottom: 0.25rem;
}
// Integrate the grids in the publication tab
.pkpWorkflow__contributors,
#representations-grid {
Expand Down
4 changes: 4 additions & 0 deletions src/components/Container/WorkflowPageOMP.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ export default {
/**
* Update the work type to be an edited volume
* Update authors' isVolumeEditor to bool if null
*/
setAsEditedVolume() {
this.updateWorkType(this.getConstant('WORK_TYPE_EDITED_VOLUME'));
this.workingPublication.authors.forEach(
(author) => (author.isVolumeEditor = !!author.isVolumeEditor),
);
},
/**
Expand Down
7 changes: 1 addition & 6 deletions src/components/Form/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,9 @@ export default {
missingValue = !value;
break;
case 'string':
case 'array':
if (!value.length) {
missingValue = true;
}
break;
case 'object':
// null values are stored as objects
if (!value) {
if (!value || (Array.isArray(value) && !value.length)) {
missingValue = true;
}
break;
Expand Down
3 changes: 3 additions & 0 deletions src/components/Form/FormPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ export default {
if (typeof group.showWhen === 'string') {
return !!whenField.value;
}
if (Array.isArray(group.showWhen[1])) {
return group.showWhen[1].includes(whenField.value);
}
return whenField.value === group.showWhen[1];
},
Expand Down
60 changes: 60 additions & 0 deletions src/pages/workflow/ChangeSubmissionLanguage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<template>
<SideModalBody>
<template #pre-title>
{{ props.submissionId }}
</template>
<template #title>
{{ t('submission.list.changeSubmissionLanguage.title') }}
</template>
<template #description>
{{ publicationTitle }}
</template>
<div class="p-4">
<div class="bg-secondary p-4">
<div id="changeSubmissionLanguage" aria-live="polite">
<pkp-form
v-bind="store.form"
:hide-form-locales="true"
@set="store.updateFormData"
@success="store.changeLanguage"
></pkp-form>
</div>
</div>
</div>
</SideModalBody>
</template>

<script setup>
import {defineProps, ref} from 'vue';
import PkpForm from '@/components/Form/Form.vue';
import SideModalBody from '@/components/Modal/SideModalBody.vue';
import {useChangeSubmissionLanguageStore} from '@/pages/workflow/changeSubmissionLanguageStore';
/**
* Variables
*/
const props = defineProps({
form: {
type: Object,
required: true,
},
// for test
publicationId: {
type: Number,
required: true,
},
submissionId: {
type: Number,
required: true,
},
});
const store = useChangeSubmissionLanguageStore({
form: props.form,
/* for test */ submissionId: props.submissionId,
/* for test */ publicationId: props.publicationId,
});
const publicationTitle = ref(
props.form.fields[2].value[props.form.primaryLocale],
);
</script>
93 changes: 93 additions & 0 deletions src/pages/workflow/changeSubmissionLanguageStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* import { ref } from 'vue'; */
import {defineComponentStore} from '@/utils/defineComponentStore';
import {useApiUrl} from '@/composables/useApiUrl'; // test
import {useFetch} from '@/composables/useFetch'; // test
import {useForm} from '@/composables/useForm';
import cloneDeep from 'clone-deep';

export const useChangeSubmissionLanguageStore = defineComponentStore(
'changeSubmissionLanguage',
(props) => {
/**
* Variables
*/

const {
form: {value: form} /* , setValue */,
} = useForm(cloneDeep(props.form));
// Get multilingual props
const formMProps = form.fields.reduce((pp, f) => {
if (f.isMultilingual) {
pp[f.name] = f.value;
}
return pp;
}, {});
// Make form monolingual
/* form.fields.forEach(f => {
if (f.isMultilingual) {
f.isMultilingual = false;
f.value = f.value[form.primaryLocale];
}
}); */

/**
* Functions
*/

/**
* Change submission language
*/
const changeLanguage = async () => {
//location.reload();

// Test new api endpoint
const {apiUrl} = useApiUrl(
`submissions/${props.submissionId}/publications/${props.publicationId}/changeLocale`,
);
const {fetch} = useFetch(apiUrl, {
method: 'PUT',
body: {
locale: form.primaryLocale,
title: form.fields[2].value,
abstract: form.fields[3].value,
},
});
await fetch();
};

/**
* Update form data
*/
const updateFormData = (_, data) => {
Object.keys(data).forEach((key) => (form[key] = data[key]));
const oldLocale = form.primaryLocale;
const newLocale = data.fields?.[0].value ?? oldLocale;
// Set multilingual form fields when changing language
if (newLocale !== oldLocale) {
form.primaryLocale = newLocale;
form.visibleLocales = [newLocale]; // for test
form.fields.forEach((f) => {
if (formMProps[f.name]) {
//setValue(f.name, formMProps[f.name][newLocale]);
// i18n.js t doesn't seem to support variable as tranlation key
// (i18nExtractKeys.vite.js: uiLocaleKeysBackend.json), using str.replace for now.
f.description = f.description.replace(
getLocaleName(oldLocale),
getLocaleName(newLocale),
);
}
});
}
};

return {changeLanguage, updateFormData, form};

/**
* Aux functions
*/

function getLocaleName(locale) {
return form.fields[0].options.find(({value}) => value === locale).label;
}
},
);

0 comments on commit 2a032ef

Please sign in to comment.