Implements new two stage submission logic on form #388
This commit is contained in:
parent
1f83f45444
commit
6d4262b2ff
|
@ -321,23 +321,39 @@ export const deleteGroupFromItemSubmissionMetadatum = ({ commit }, { metadatum_i
|
|||
}
|
||||
|
||||
export const submitItemSubmission = ({ commit }, { itemSubmission, itemSubmissionMetadata }) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
for (let key of Object.keys(itemSubmission)) {
|
||||
if (['document', 'attachments', 'thumbnail'].includes(key) )
|
||||
delete itemSubmission[key];
|
||||
}
|
||||
|
||||
axios.tainacan.post('/collection/' + itemSubmission.collection_id + '/items/submission', {...itemSubmission, metadata: itemSubmissionMetadata } )
|
||||
.then( res => {
|
||||
resolve( res.data.id );
|
||||
}).catch( error => {
|
||||
reject({
|
||||
errors: error.response.data.errors,
|
||||
error_message: error.response.data.error_message
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export const finishItemSubmission = ({ commit }, { itemSubmission, fakeItemId }) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let config = {
|
||||
headers: { 'content-type': 'multipart/form-data' }
|
||||
}
|
||||
|
||||
let item = {...itemSubmission, metadata: itemSubmissionMetadata };
|
||||
|
||||
const formData = new FormData();
|
||||
|
||||
for (let key of Object.keys(itemSubmission)) {
|
||||
if (['document', 'attachments', 'thumbnail'].includes(key) ) {
|
||||
if (['document', 'attachments', 'thumbnail'].includes(key) )
|
||||
formData.append(key, itemSubmission[key]);
|
||||
delete item[key];
|
||||
}
|
||||
}
|
||||
formData.append('item', JSON.stringify(item));
|
||||
|
||||
axios.tainacan.post('/collection/' + itemSubmission.collection_id + '/items/submission', formData, config )
|
||||
axios.tainacan.post('/collection/' + itemSubmission.collection_id + '/items/submission/' + fakeItemId + '/finish', formData, config )
|
||||
.then( res => {
|
||||
resolve( res.data );
|
||||
}).catch( error => {
|
||||
|
|
|
@ -387,7 +387,6 @@ export default {
|
|||
thumbnail: null
|
||||
},
|
||||
formErrorMessage: '',
|
||||
thumbPlaceholderPath: tainacan_plugin.base_url + '/assets/images/placeholder_square.png',
|
||||
hasSentForm: false,
|
||||
showThumbnailInput: false
|
||||
}
|
||||
|
@ -399,9 +398,6 @@ export default {
|
|||
itemSubmissionMetadata() {
|
||||
return this.getItemSubmissionMetadata();
|
||||
},
|
||||
collection() {
|
||||
return this.getCollection()
|
||||
},
|
||||
metadatumList() {
|
||||
return (this.itemSubmissionMetadata && this.itemSubmissionMetadata.length) ? JSON.parse(JSON.stringify(this.getMetadata().map((metadatum) => { return { metadatum: metadatum, item: {}, value: this.itemSubmissionMetadata.find((aMetadatum) => aMetadatum.metadatum_id == metadatum.id).value } } ))) : [];
|
||||
},
|
||||
|
@ -435,18 +431,12 @@ export default {
|
|||
...mapActions('item', [
|
||||
'setItemSubmission',
|
||||
'setItemSubmissionMetadata',
|
||||
'updateItemSubmission',
|
||||
'submitItemSubmission',
|
||||
'updateItem',
|
||||
'updateItemDocument',
|
||||
'fetchItemMetadata',
|
||||
'fetchItem',
|
||||
'clearItemSubmission',
|
||||
'updateThumbnail'
|
||||
'finishItemSubmission',
|
||||
'clearItemSubmission'
|
||||
]),
|
||||
...mapGetters('item',[
|
||||
'getItemSubmission',
|
||||
'getItemMetadata',
|
||||
'getItemSubmissionMetadata',
|
||||
]),
|
||||
...mapActions('metadata',[
|
||||
|
@ -455,12 +445,6 @@ export default {
|
|||
...mapGetters('metadata',[
|
||||
'getMetadata'
|
||||
]),
|
||||
...mapActions('collection', [
|
||||
'deleteItem',
|
||||
]),
|
||||
...mapGetters('collection', [
|
||||
'getCollection',
|
||||
]),
|
||||
onSubmit() {
|
||||
|
||||
// Puts loading on Item edition
|
||||
|
@ -475,9 +459,28 @@ export default {
|
|||
eventBusItemMetadata.errors = [];
|
||||
|
||||
this.submitItemSubmission({ itemSubmission: this.itemSubmission, itemSubmissionMetadata: this.itemSubmissionMetadata })
|
||||
.then(() => {
|
||||
this.hasSentForm = true;
|
||||
this.isLoading = false;
|
||||
.then((fakeItemId) => {
|
||||
if (fakeItemId) {
|
||||
this.finishItemSubmission({ itemSubmission: this.itemSubmission, fakeItemId: fakeItemId })
|
||||
.then(() => {
|
||||
this.hasSentForm = true;
|
||||
this.isLoading = false;
|
||||
})
|
||||
.catch((errors) => {
|
||||
if (errors.errors) {
|
||||
for (let error of errors.errors) {
|
||||
for (let metadatum of Object.keys(error)){
|
||||
eventBusItemMetadata.errors.push({
|
||||
metadatum_id: metadatum,
|
||||
errors: error[metadatum]
|
||||
});
|
||||
}
|
||||
}
|
||||
this.formErrorMessage = errors.error_message;
|
||||
}
|
||||
this.isLoading = false;
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch((errors) => {
|
||||
if (errors.errors) {
|
||||
|
|
Loading…
Reference in New Issue