-
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
2 changed files
with
148 additions
and
91 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
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> |
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