patch Address to accompanyingPeriod: build payload for differents cases

use uniq patch api endpoint, build payload body in action,
call updateAddress action when submitting, assigning or removing.
This commit is contained in:
Mathieu Jaumotte 2021-08-16 16:23:10 +02:00
parent 966292cdd5
commit 107699ec31
4 changed files with 48 additions and 40 deletions

View File

@ -9,12 +9,26 @@
</template>
<script>
import {mapState} from "vuex";
export default {
name: "ButtonLocation",
props: ['person'],
computed: {
...mapState({
context: state => state.addressContext
}),
},
methods: {
assignAddress() {
console.log('assignAddress id', this.person.current_household_address.address_id);
console.log('assignAddress id', this.person.current_household_address);
let payload = {
entity: this.context.entity.type,
entityId: this.context.entity.id,
addressToPatch: 'person',
personId: this.person.id
};
this.$store.dispatch('updateLocation', payload);
}
}
}

View File

@ -23,7 +23,7 @@
<ul class="record_actions">
<li>
<add-address
v-if="hasTemporaryAddressButton"
v-if="!isPersonLocation"
:context="context"
:key="addAddress.type"
:options="addAddress.options"
@ -32,11 +32,12 @@
ref="addAddress">
</add-address>
</li>
<li>
<button v-if="isContextEdit"
<li v-if="isContextEdit && isPersonLocation">
<button
class="btn btn-remove"
@click="removeAddress"
:title="$t('courselocation.remove_button')"></button>
:title="$t('courselocation.remove_button')">
</button>
</li>
</ul>
@ -85,12 +86,12 @@ export default {
accompanyingCourse: state => state.accompanyingCourse,
context: state => state.addressContext
}),
hasTemporaryAddressButton() { // return true if locationStatus equal 'none' or 'address'
return this.accompanyingCourse.locationStatus !== 'person';
},
isTemporaryAddress() {
return this.accompanyingCourse.locationStatus === 'address';
},
isPersonLocation() {
return this.accompanyingCourse.locationStatus === 'person';
},
isContextEdit() {
return this.context.edit;
}
@ -117,6 +118,12 @@ export default {
},
removeAddress() {
console.log('remove address');
let payload = {
entity: this.context.entity.type,
entityId: this.context.entity.id,
addressToPatch: 'none'
};
this.$store.dispatch('updateLocation', payload);
},
displayErrors() {
return this.$refs.addAddress.errorMsg;
@ -125,7 +132,9 @@ export default {
console.log('@@@ click on Submit Address Button');
let payload = this.$refs.addAddress.submitNewAddress();
console.log('payload from parent', payload);
// precise in payload that it is a temporary address
payload['addressToPatch'] = 'address';
this.$store.dispatch('updateLocation', payload);
}

View File

@ -1,6 +1,5 @@
import 'es6-promise/auto';
import { createStore } from 'vuex';
import { patchAddressToAccompanyingPeriod } from "../../_api/AddAddress.js";
import { getAccompanyingCourse,
patchAccompanyingCourse,
confirmAccompanyingCourse,
@ -225,7 +224,21 @@ let initPromise = getAccompanyingCourse(id)
/////
updateLocation({ commit }, payload) {
console.log('## action: updateLocation', payload);
patchAddressToAccompanyingPeriod(payload.entityId, payload.addressId)
let body = { 'type': payload.entity, 'id': payload.entityId };
let location = {};
if (payload.addressToPatch === 'person') {
// payload to patch person address (don't remove addressLocation)
location = { 'personLocation': { 'type': 'person', 'id': payload.personId }};
}
else if(payload.addressToPatch === 'address') {
// payload to patch temporary address
location = { 'personLocation': null, 'addressLocation': { 'id': payload.addressId }};
}
else {
location = { 'personLocation': null };
}
Object.assign(body, location);
patchAccompanyingCourse(payload.entityId, body)
.then(accompanyingCourse => new Promise((resolve, reject) => {
commit('updateLocation', {
location: accompanyingCourse.location,

View File

@ -48,32 +48,4 @@ const postAddressToHousehold = (householdId, addressId) => {
};
/*
* Endpoint AccompanyingPeriod
* method POST, post AccompanyingPeriod AddressLocation (temporary address)
*
* @id integer - id of AccompanyingPeriod
* @body Object - dictionary with changes to post
*/
const patchAddressToAccompanyingPeriod = (accompanyingPeriodId, addressId) => {
const body = {
'type': 'accompanying_period',
'id': accompanyingPeriodId,
'personLocation': null,
'addressLocation': {
'id': addressId
}
};
const url = `/api/1.0/person/accompanying-course/${accompanyingPeriodId}.json`;
return fetch (url, {
method: 'PATCH',
headers: {'Content-Type': 'application/json;charset=utf-8'},
body: JSON.stringify(body)
})
.then(response => {
if (response.ok) { return response.json(); }
throw Error('Error with request resource response');
});
};
export { postAddressToPerson, postAddressToHousehold, patchAddressToAccompanyingPeriod };
export { postAddressToPerson, postAddressToHousehold };