-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
201 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}, | ||
); |