make Address works with household context

This commit is contained in:
Mathieu Jaumotte 2021-08-07 17:25:04 +02:00
parent 9f77b84e48
commit 63fbf4b249
8 changed files with 165 additions and 89 deletions

View File

@ -19,7 +19,7 @@
</div> </div>
<add-address <add-address
v-bind:key="addAddress.type" v-bind:key="context.entity.type"
v-bind:context="context" v-bind:context="context"
v-bind:options="addAddress.options" v-bind:options="addAddress.options"
v-bind:result="addAddress.result" v-bind:result="addAddress.result"
@ -39,40 +39,44 @@ export default {
return { return {
context: { context: {
edit: window.mode === 'edit', edit: window.mode === 'edit',
personId: window.personId, entity: {
addressId: window.addressId, type: window.entityType,
id: window.entityId
},
addressId: window.addressId | null,
backUrl: window.backUrl, backUrl: window.backUrl,
}, },
addAddress: { addAddress: {
options: { options: {
/// Options override default. /// Options override default.
/// null value take default component value /// null value take default component value defined in AddAddress data()
button: { button: {
text: { text: {
/// if create or edit address
create: window.buttonText || null, create: window.buttonText || null,
edit: window.buttonText || null edit: window.buttonText || null
}, },
type: window.button.type || null, type: window.button.type || null,
size: window.button.size || null, size: window.button.size || null,
display: window.button.display display: window.button.display //boolean, default: true
}, },
/// Modal title text if create or edit address (trans chain, see i18n) /// Modal title text if create or edit address (trans chain, see i18n)
title: { title: {
create: window.modalTitle || null, create: window.modalTitle || null,
edit: window.modalTitle || null edit: window.modalTitle || null
}, },
/// Display each step in page or Modal /// Display each step in page or Modal
bindModal: { bindModal: {
step1: window.binModalStep1, step1: window.binModalStep1, //boolean, default: true
step2: window.binModalStep2 step2: window.binModalStep2 //boolean, default: true
}, },
// Options only for root parent component // Options only for root parent component
displayResult: true, displayResult: true,
redirectToBackUrl: true redirectToBackUrl: true
}, },
type: 'person',
result: null // <== returned from addAddress component result: null // <== returned from addAddress component
} }
} }

View File

@ -1,5 +1,6 @@
<template> <template>
<!-- start with a button -->
<button v-if="step1WithModal" <button v-if="step1WithModal"
@click="openShowPane" @click="openShowPane"
class="btn" :class="getClassButton" class="btn" :class="getClassButton"
@ -123,7 +124,6 @@ import { postAddressToHousehold } from "ChillPersonAssets/vuejs/_api/AddAddress/
import ShowAddress from './ShowAddress.vue'; import ShowAddress from './ShowAddress.vue';
import EditAddress from './EditAddress.vue'; import EditAddress from './EditAddress.vue';
export default { export default {
name: "AddAddress", name: "AddAddress",
props: ['context', 'options', 'result'], props: ['context', 'options', 'result'],
@ -358,7 +358,7 @@ export default {
}, },
/* /*
* When changes are validated, * When changes are validated (step2 edit pane),
* apply some transformations before asyncing with backend * apply some transformations before asyncing with backend
* from entity.selected to entity.address * from entity.selected to entity.address
*/ */
@ -394,7 +394,10 @@ export default {
} }
if (this.context.edit) { if (this.context.edit) {
this.updateAddress({ addressId: this.context.addressId, newAddress: newAddress }); this.updateAddress({
addressId: this.context.addressId,
newAddress: newAddress
});
} else { } else {
this.addAddress(newAddress); this.addAddress(newAddress);
} }
@ -404,8 +407,8 @@ export default {
* Async PATCH transactions, * Async PATCH transactions,
* then update existing address with backend datas when promise is resolved * then update existing address with backend datas when promise is resolved
*/ */
updateAddress(payload) { updateAddress(payload)
{
// TODO change the condition because it writes new postal code in edit mode now: !writeNewPostalCode // TODO change the condition because it writes new postal code in edit mode now: !writeNewPostalCode
this.flag.loading = true; this.flag.loading = true;
if ('newPostcode' in payload.newAddress) { if ('newPostcode' in payload.newAddress) {
@ -446,8 +449,8 @@ export default {
* Async POST transactions, * Async POST transactions,
* creating new address, and receive backend datas when promise is resolved * creating new address, and receive backend datas when promise is resolved
*/ */
addAddress(payload) { addAddress(payload)
{
this.flag.loading = true; this.flag.loading = true;
if('newPostcode' in payload){ if('newPostcode' in payload){
let postcodeBody = payload.newPostcode; let postcodeBody = payload.newPostcode;
@ -484,33 +487,49 @@ export default {
}, },
/* /*
* When submited * When submit address (step1 show pane)
*/ */
submitAddress() { submitAddress()
console.log('@@@ CLICK button submitAddress'); {
this.addDateToAddressAndAddressToPerson({ let payload = {
personId: this.context.personId, entityId: this.context.entityId,
addressId: this.entity.address.address_id, addressId: this.entity.address.address_id,
body: { validFrom: { datetime: `${this.$refs.showAddress.validFrom}T00:00:00+0100`}}, body: {
validFrom: {
datetime: `${this.$refs.showAddress.validFrom}T00:00:00+0100`
}
},
backUrl: this.context.backUrl backUrl: this.context.backUrl
}); }
console.log('@@@ CLICK button submitAddress for', this.context.entity.type, 'with payload', payload);
if (this.context.entity.type === 'person') {
this.addDateToAddressAndAddressToPerson();
} else if (this.context.entity.type === 'household') {
this.addDateToAddressAndAddressToHousehold();
} else {
this.errorMsg.push('this entity type is not yet allowed');
}
this.initForm(); this.initForm();
this.flag.showPane = false; this.flag.showPane = false;
}, },
addDateToAddressAndAddressToPerson(payload) { addDateToAddressAndAddressToPerson(payload)
console.log('addDateToAddressAndAddressToPerson payload', payload); {
this.flag.loading = true; this.flag.loading = true;
patchAddress(payload.addressId, payload.body) patchAddress(payload.addressId, payload.body)
.then(address => new Promise((resolve, reject) => { .then(address => new Promise((resolve, reject) => {
this.valid.from = address.validFrom; this.valid.from = address.validFrom;
resolve(); resolve();
}).then( }).then(
postAddressToPerson(payload.personId, payload.addressId) postAddressToPerson(payload.entityId, payload.addressId)
.then(person => new Promise((resolve, reject) => { .then(person => new Promise((resolve, reject) => {
console.log('commit addAddressToPerson !!!', person); console.log('commit addAddressToPerson !!!', person);
this.$props.result = person; //this.$props.result = person;
this.flag.loading = false; this.flag.loading = false;
this.flag.success = true; this.flag.success = true;
@ -524,12 +543,48 @@ export default {
this.errorMsg.push(error); this.errorMsg.push(error);
this.flag.loading = false; this.flag.loading = false;
}) })
)) ))
.catch((error) => { .catch((error) => {
this.errorMsg.push(error); this.errorMsg.push(error);
this.flag.loading = false; this.flag.loading = false;
}); });
} },
addDateToAddressAndAddressToHousehold(payload)
{
this.flag.loading = true;
patchAddress(payload.addressId, payload.body)
.then(address => new Promise((resolve, reject) => {
this.valid.from = address.validFrom;
resolve();
}).then(
postAddressToHousehold(payload.entityId, payload.addressId)
.then(household => new Promise((resolve, reject) => {
console.log('commit addAddressToHousehold', household);
this.flag.success = true;
this.flag.loading = false;
if (this.options.redirectToBackUrl) {
window.location.assign(payload.backUrl);
}
resolve();
}))
.catch((error) => {
this.errorMsg.push(error);
this.flag.loading = false;
})
))
.catch((error) => {
this.errorMsg.push(error);
this.flag.loading = false;
});
},
} }
} }
</script> </script>

View File

@ -1,38 +1,73 @@
<div id="address"></div>{# <== vue_address component #} {#
This Twig template include load vue_address component.
It push all variables from context in Address/App.vue.
OPTIONS
* mode string ['edit*'|'new'|'create']
* address_id integer
* backUrl twig route: path('route', {parameters})
* modalTitle twig translated chain
* buttonText twig translated chain
* buttonType chill class like 'btn-update'
* buttonSize bootstrap class like 'btn-sm'
#}
<div id="address"></div>
<script type="text/javascript"> <script type="text/javascript">
window.vueRootComponent = 'app'; window.vueRootComponent = 'app';
window.mode = '{{ mode|e('js') }}';
window.personId = {{ person.id|e('js') }};
window.addressId = {{ address_id|e('js') }};
{% if backUrl is defined %}
window.backUrl = '{{ backUrl|e('js') }}';
{% else %}
window.backUrl = '{{ path('chill_person_address_list', { 'person_id': person.id })|e('js') }}';
{% endif %}
{% if modalTitle is defined %} {% if person is defined %}
window.modalTitle = '{{ modalTitle|trans|e('js') }}'; window.entityType = 'person';
{% endif %} window.entityId = {{ person.id|e('js') }};
{% elseif household is defined %}
window.entityType = 'household';
window.entityId = {{ household.id|e('js') }};
{% endif %}
{% if mode == 'edit' %}
window.addressId = {{ address_id|e('js') }};
{% endif %}
window.mode = '{{ mode|e('js') }}';
{% if backUrl is not defined %}
{% if person is defined %}
window.backUrl = '{{ path('chill_person_address_list', { 'person_id': person.id })|e('js') }}';
{% elseif household is defined %}
window.backUrl = '{{ path('chill_person_household_addresses', { 'household_id': household.id })|e('js') }}';
{% endif %}
{% else %}
window.backUrl = '{{ backUrl|e('js') }}';
{% endif %}
{% if modalTitle is defined %}
window.modalTitle = '{{ modalTitle|trans|e('js') }}';
{% endif %}
window.button = {}; window.button = {};
{% if buttonText is defined %} {% if buttonText is defined %}
window.buttonText = '{{ buttonText|trans|e('js') }}'; window.buttonText = '{{ buttonText|trans|e('js') }}';
{% endif %} {% endif %}
{% if buttonType is defined %}
window.button.type = '{{ buttonType|e('js') }}'; {% if buttonType is defined %}
{% endif %} window.button.type = '{{ buttonType|e('js') }}';
{% if buttonSize is defined %} {% endif %}
window.button.size = '{{ buttonSize|e('js') }}';
{% endif %} {% if buttonSize is defined %}
{% if buttonDisplay is defined and buttonDisplay == false %} window.button.size = '{{ buttonSize|e('js') }}';
window.button.display = false; {% endif %}
{% endif %}
{% if binModalStep1 is defined and binModalStep1 == false %} {% if buttonDisplay is defined and buttonDisplay == false %}
window.binModalStep1 = false; window.button.display = false;
{% endif %} {% endif %}
{% if binModalStep2 is defined and binModalStep2 == false %}
window.binModalStep2 = false; {% if binModalStep1 is defined and binModalStep1 == false %}
{% endif %} window.binModalStep1 = false;
{% endif %}
{% if binModalStep2 is defined and binModalStep2 == false %}
window.binModalStep2 = false;
{% endif %}
</script> </script>
{{ encore_entry_script_tags('vue_address') }} {{ encore_entry_script_tags('vue_address') }}

View File

@ -28,8 +28,8 @@
{# include vue_address component #} {# include vue_address component #}
{% include '@ChillPerson/Address/_insert_vue_address.html.twig' with { {% include '@ChillPerson/Address/_insert_vue_address.html.twig' with {
address_id: person.lastAddress.id,
mode: 'edit', mode: 'edit',
address_id: person.lastAddress.id,
binModalStep1: false, binModalStep1: false,
binModalStep2: false, binModalStep2: false,
} %} } %}

View File

@ -28,7 +28,6 @@
{# include vue_address component #} {# include vue_address component #}
{% include '@ChillPerson/Address/_insert_vue_address.html.twig' with { {% include '@ChillPerson/Address/_insert_vue_address.html.twig' with {
address_id: person.lastAddress.id,
mode: 'new', mode: 'new',
binModalStep1: false, binModalStep1: false,
binModalStep2: false, binModalStep2: false,

View File

@ -7,21 +7,12 @@
<div> <div>
{# include vue_address component #} {# include vue_address component #}
<div id="household-address"></div> {% include '@ChillPerson/Address/_insert_vue_address.html.twig' with {
mode: 'edit',
address_id: household.lastAddress.id,
binModalStep1: false,
binModalStep2: false,
} %}
</div> </div>
{% block stylesheets %}
{{ encore_entry_link_tags('vue_address') }}
{% endblock %}
{% block js %}
<script type="text/javascript">
window.householdId = {{ household.id|e('js') }};
window.addressId = {{ address.id|e('js') }};
window.mode = 'edit';
window.vueRootComponent = 'app';
</script>
{{ encore_entry_script_tags('vue_household_address') }}
{% endblock %}
{% endblock %} {% endblock %}

View File

@ -7,19 +7,11 @@
<div> <div>
{# include vue_address component #} {# include vue_address component #}
<div id="household-address"></div> {% include '@ChillPerson/Address/_insert_vue_address.html.twig' with {
mode: 'new',
binModalStep1: false,
binModalStep2: false,
} %}
</div> </div>
{% block stylesheets %}
{{ encore_entry_link_tags('vue_address') }}
{% endblock %}
{% block js %}
<script type="text/javascript">
window.householdId = {{ household.id|e('js') }};
window.vueRootComponent = 'app';
</script>
{{ encore_entry_script_tags('vue_household_address') }}
{% endblock %}
{% endblock %} {% endblock %}

View File

@ -202,9 +202,9 @@ This view should receive those arguments:
<li class="list-inline-item"> <li class="list-inline-item">
{# include vue_address component #} {# include vue_address component #}
{% include '@ChillPerson/Address/_insert_vue_address.html.twig' with { {% include '@ChillPerson/Address/_insert_vue_address.html.twig' with {
mode: 'edit',
address_id: person.lastAddress.id, address_id: person.lastAddress.id,
backUrl: path('chill_person_view', { 'person_id': person.id }), backUrl: path('chill_person_view', { 'person_id': person.id }),
mode: 'edit',
modalTitle: 'Edit address', modalTitle: 'Edit address',
buttonText: 'Edit address', buttonText: 'Edit address',
buttonType: 'btn-update', buttonType: 'btn-update',