forked from pkp/ui-library
-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
203 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<template> | ||
<SideModalBody> | ||
<template #pre-title> | ||
{{ props.submissionId }} | ||
</template> | ||
<template #title> | ||
{{ t('submission.list.changeSubmissionLanguage.title') }} | ||
</template> | ||
<template #description> | ||
{{ store.publicationTitle }} | ||
</template> | ||
<div class="p-4"> | ||
<div class="bg-secondary p-4"> | ||
<div id="changeSubmissionLanguage"> | ||
<PkpForm | ||
v-bind="store.form" | ||
@set="store.setCustom" | ||
@success="store.success" | ||
@cancel="store.closeSideModal" | ||
></PkpForm> | ||
</div> | ||
</div> | ||
</div> | ||
</SideModalBody> | ||
</template> | ||
|
||
<script setup> | ||
import {defineProps} from 'vue'; | ||
import PkpForm from '@/components/Form/Form.vue'; | ||
import SideModalBody from '@/components/Modal/SideModalBody.vue'; | ||
import {useChangeSubmissionLanguageStore} from '@/pages/workflow/changeSubmissionLanguageStore'; | ||
const props = defineProps({ | ||
form: { | ||
type: Object, | ||
required: true, | ||
}, | ||
publicationId: { | ||
type: Number, | ||
required: true, | ||
}, | ||
submissionId: { | ||
type: Number, | ||
required: true, | ||
}, | ||
}); | ||
const store = useChangeSubmissionLanguageStore({ | ||
form: props.form, | ||
publicationId: props.publicationId, | ||
submissionId: props.submissionId, | ||
}); | ||
</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,115 @@ | ||
import {inject, ref} from 'vue'; | ||
import {defineComponentStore} from '@/utils/defineComponentStore'; | ||
import {useApiUrl} from '@/composables/useApiUrl'; | ||
import {useFetch} from '@/composables/useFetch'; | ||
import {useForm} from '@/composables/useForm'; | ||
import {useLocalize} from '@/composables/useLocalize'; | ||
import cloneDeep from 'clone-deep'; | ||
|
||
export const useChangeSubmissionLanguageStore = defineComponentStore( | ||
'changeSubmissionLanguage', | ||
(props) => { | ||
/** | ||
* Variables and init data | ||
*/ | ||
|
||
const {t, tk} = useLocalize(); | ||
|
||
const DescriptionLocaleKeys = { | ||
title: tk( | ||
'submission.list.changeSubmissionLanguage.metadataDescription.title', | ||
), | ||
abstract: tk( | ||
'submission.list.changeSubmissionLanguage.metadataDescription.abstract', | ||
), | ||
}; | ||
|
||
const { | ||
apiUrl: {value: publicationApiUrl}, | ||
} = useApiUrl( | ||
`submissions/${props.submissionId}/publications/${props.publicationId}`, | ||
); | ||
|
||
const { | ||
form: {value: form}, | ||
getValue, | ||
set, | ||
setValue, | ||
} = useForm(cloneDeep(props.form)); | ||
// Set action api url | ||
form.action = publicationApiUrl + '/changeLocale'; | ||
|
||
// Set initial value | ||
const publicationTitle = ref(getValue('title')); | ||
|
||
const publicationProps = {}; | ||
// Get publication props | ||
getData(); | ||
|
||
const closeModal = inject('closeModal'); | ||
|
||
/** | ||
* Functions | ||
*/ | ||
|
||
function closeSideModal() { | ||
closeModal(); | ||
} | ||
|
||
/** | ||
* Set form data | ||
*/ | ||
const setCustom = (_, data) => { | ||
set(_, data); | ||
const oldLocale = form.primaryLocale; | ||
const newLocale = getValue('locale'); | ||
// Set fields when changing language | ||
if (newLocale !== oldLocale) { | ||
form.primaryLocale = newLocale; | ||
form.fields.forEach((field) => { | ||
if (publicationProps[field.name]) { | ||
setValue( | ||
field.name, | ||
publicationProps[field.name][newLocale] ?? | ||
publicationProps[field.name], | ||
); | ||
field.description = t(DescriptionLocaleKeys[field.name], { | ||
language: getLocaleName(newLocale), | ||
}); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
/** | ||
* Form success | ||
*/ | ||
const success = () => { | ||
window.location.reload(); | ||
}; | ||
|
||
return {closeSideModal, setCustom, success, form, publicationTitle}; | ||
|
||
/** | ||
* Aux functions | ||
*/ | ||
|
||
async function getData() { | ||
const {data, fetch} = useFetch(publicationApiUrl, { | ||
method: 'GET', | ||
}); | ||
await fetch(); | ||
|
||
Object.assign(publicationProps, data.value ?? {}); | ||
delete publicationProps['locale']; | ||
|
||
publicationTitle.value = data.value.title[props.form.primaryLocale]; | ||
} | ||
|
||
function getLocaleName(locale) { | ||
return form.fields | ||
.find(({name}) => name === 'locale') | ||
.options.find(({value}) => value === locale).label; | ||
} | ||
}, | ||
); |