diff --git a/openlibrary/components/ObservationForm.vue b/openlibrary/components/ObservationForm.vue index 46b7b51647c..163498cba57 100644 --- a/openlibrary/components/ObservationForm.vue +++ b/openlibrary/components/ObservationForm.vue @@ -5,7 +5,6 @@ :work-key="work_key" :username="username" /> - ` - * @param {String} username Username of patron that is deleting an observation - */ -export function deleteObservation(type, value, workKey, username) { - const data = constructDataObject(type, value, username, 'delete'); - - fetch(`${workKey}/observations`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify(data) - }) -} - -/** - * Sends a POST request to add a new patron observation about a work. + * @param {String} username Username of patron that is updating an observation * - * @param {String} type The observation's type - * @param {String} value The observation's value - * @param {String} workKey Location of work, in the form `/works/` - * @param {String} username Username of patron that is adding an observation + * @returns A Promise representing the state of the POST request. */ -export function addObservation(type, value, workKey, username) { - const data = constructDataObject(type, value, username, 'add'); - fetch(`${workKey}/observations`, { +export function updateObservation(action, type, value, workKey, username) { + const data = constructDataObject(type, value, username, action) + + return fetch(`${workKey}/observations`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }) + .then(response => { + if (!response.ok) { + throw new Error('Server response was not ok') + } + }) + .catch(error => { + throw error + }) } /** diff --git a/openlibrary/components/ObservationForm/components/CardBody.vue b/openlibrary/components/ObservationForm/components/CardBody.vue index bafd4f8d034..8f8bbfcee4b 100644 --- a/openlibrary/components/ObservationForm/components/CardBody.vue +++ b/openlibrary/components/ObservationForm/components/CardBody.vue @@ -18,7 +18,7 @@ import Vue from 'vue' import OLChip from './OLChip' -import { deleteObservation, addObservation } from '../ObservationService' +import { updateObservation } from '../ObservationService' export default { name: 'CardBody', @@ -94,19 +94,40 @@ export default { if (isSelected) { if (this.multiSelect) { updatedValues.push(text) + updateObservation('add', this.type, text, this.workKey, this.username) + .catch(() => { + updatedValues.pop(); + }) + .finally(() => { + Vue.set(this.allSelectedValues, this.type, updatedValues); + }) } else { if (updatedValues.length) { - deleteObservation(this.type, updatedValues[0], this.workKey, this.username) + let deleteSuccessful = false; + updateObservation('delete', this.type, updatedValues[0], this.workKey, this.username) + .then(() => { + deleteSuccessful = true; + }) + .finally(() => { + if (deleteSuccessful) { + updateObservation('add', this.type, text, this.workKey, this.username) + .then(() => { + updatedValues = [text] + }) + .finally(() => { + Vue.set(this.allSelectedValues, this.type, updatedValues) + }) + } + }) } - updatedValues = [text] } - - addObservation(this.type, text, this.workKey, this.username); - Vue.set(this.allSelectedValues, this.type, updatedValues); } else { const index = updatedValues.indexOf(text); updatedValues.splice(index, 1); - deleteObservation(this.type, text, this.workKey, this.username) + updateObservation('delete', this.type, text, this.workKey, this.username) + .catch(() => { + updatedValues.push(text); + }) } } }, diff --git a/openlibrary/components/ObservationForm/components/CategorySelector.vue b/openlibrary/components/ObservationForm/components/CategorySelector.vue index 3ebb51c2f66..e878f59000d 100644 --- a/openlibrary/components/ObservationForm/components/CategorySelector.vue +++ b/openlibrary/components/ObservationForm/components/CategorySelector.vue @@ -1,6 +1,7 @@