Merge branch 'address_refactoring' into add-location-period

This commit is contained in:
Mathieu Jaumotte 2021-08-06 21:50:04 +02:00
commit 6d58bf152b
11 changed files with 306 additions and 129 deletions

View File

@ -94,8 +94,8 @@ section.chill-entity {
}
span.address-valid {
&.address-since {}
&.address-until {}
&.date-since {}
&.date-until {}
}
}

View File

@ -1,10 +1,28 @@
<template>
<div v-for="error in displayErrors" class="alert alert-danger my-2">
{{ error }}
</div>
<div v-if="addAddress.options.displayResult && addAddress.result !== null"
class="chill-entity entity-address">
<div class="address multiline">
<p class="street">{{ addAddress.result.street }}
<span class="streetnumber">{{ addAddress.result.streetNumber }}</span>
</p>
<p class="postcode">
<span class="code">{{ addAddress.result.postcode.code }}</span>
<span class="name">{{ addAddress.result.postcode.name }}</span>
</p>
<p class="country">{{ addAddress.result.country.name.fr }}</p>
</div>
</div>
<add-address
v-bind:key="addAddress.type"
v-bind:context="context"
v-bind:key="addAddress.key"
v-bind:options="addAddress.options"
@submitAddress="submitAddress"
v-bind:result="addAddress.result"
ref="addAddress">
</add-address>
</template>
@ -23,35 +41,45 @@ export default {
edit: window.mode === 'edit',
personId: window.personId,
addressId: window.addressId,
backUrl: `/fr/person/${window.personId}/address/list`, //TODO better way to pass this
validFrom: new Date().toISOString().split('T')[0]
backUrl: window.backUrl,
},
addAddress: {
key: 'person',
options: {
/// Options override default
/// First button text if create or edit address (trans chain, see i18n)
//textButton: { create: 'bim', edit: 'bam' },
/// Options override default.
/// null value take default component value
button: {
text: {
/// if create or edit address
create: window.buttonText || null,
edit: window.buttonText || null
},
type: window.button.type || null,
size: window.button.size || null,
display: window.button.display
},
/// Modal title text if create or edit address (trans chain, see i18n)
//title: { create: 'boum', edit: 'pan' },
title: {
create: window.modalTitle || null,
edit: window.modalTitle || null
},
/// Display each step in page or Modal
//bindModal: { step1: false, step2: false } //TODO true-false must not be possible
}
bindModal: {
step1: window.binModalStep1,
step2: window.binModalStep2
},
// Options only for root parent component
displayResult: true,
redirectToBackUrl: true
},
type: 'person',
result: null // <== returned from addAddress component
}
}
},
methods: {
submitAddress({ submited, flag }) {
console.log('@@@ CLICK button submitAddress');
console.log('address to post:', submited);
console.log('datas by refs: ', this.$refs.addAddress.entity.address.text);
this.$refs.addAddress.initForm(); // to cast child method
flag.showPane = false;
displayErrors() {
return this.$refs.addAddress.errorMsg;
}
}
}

View File

@ -2,17 +2,19 @@
<button v-if="step1WithModal"
@click="openShowPane"
class="btn btn-create mt-4" type="button" name="button">
{{ $t(getTextButton) }}
class="btn" :class="getClassButton"
type="button" name="button" :title="$t(getTextButton)">
<span v-if="displayTextButton">{{ $t(getTextButton) }}</span>
</button>
<!-- step 1 -->
<teleport to="body" v-if="step1WithModal">
<modal v-if="flag.showPane"
modalDialogClass="modal-dialog-scrollable modal-xl"
@close="flag.showPane = false">
<template v-slot:header>
<h2 class="modal-title">{{ $t(getTextTitle) }}
<h2 class="modal-title">{{ getTextTitle }}
<span v-if="flag.loading" class="loading">
<i class="fa fa-circle-o-notch fa-spin fa-fw"></i>
<span class="sr-only">Loading...</span>
@ -26,7 +28,8 @@
v-bind:options="this.options"
v-bind:default="this.default"
v-bind:entity="this.entity"
v-bind:flag="this.flag">
v-bind:flag="this.flag"
ref="showAddress">
</show-address>
</template>
@ -36,7 +39,7 @@
{{ $t('action.edit')}}
</button>
<button class="btn btn-save"
@click.prevent="$emit('submitAddress', { getSubmited , flag })">
@click="submitAddress">
{{ $t('action.save')}}
</button>
</template>
@ -51,18 +54,20 @@
v-bind:default="this.default"
v-bind:entity="this.entity"
v-bind:flag="this.flag"
ref="showAddress"
v-bind:insideModal="false" @openEditPane="openEditPane"
@submitAddress="$emit('submitAddress', { getSubmited , flag })">
@submitAddress="submitAddress">
</show-address>
</div>
<!-- step 2 -->
<teleport to="body" v-if="step2WithModal">
<modal v-if="flag.editPane"
modalDialogClass="modal-dialog-scrollable modal-xl"
@close="flag.editPane = false">
<template v-slot:header>
<h2 class="modal-title">{{ $t(getTextTitle) }}
<h2 class="modal-title">{{ getTextTitle }}
<span v-if="flag.loading" class="loading">
<i class="fa fa-circle-o-notch fa-spin fa-fw"></i>
<span class="sr-only">Loading...</span>
@ -122,8 +127,7 @@ import {
export default {
name: "AddAddress",
props: ['context', 'options'],
emits: ['submitAddress'],
props: ['context', 'options', 'result'],
components: {
Modal,
ShowAddress,
@ -138,7 +142,11 @@ export default {
success: false
},
default: {
textButton: { create: 'add_an_address_title', edit: 'edit_address' },
button: {
text: { create: 'add_an_address_title', edit: 'edit_address' },
type: 'btn-create',
display: true
},
title: { create: 'add_an_address_title', edit: 'edit_address' },
bindModal: {
step1: true,
@ -173,34 +181,48 @@ export default {
zoom: 12
},
},
valid: {
from: null,
to: null
},
errorMsg: []
}
},
computed: {
// TODO improve: props options must override data default
step1WithModal() {
return (this.options.bindModal) ? this.options.bindModal.step1 : this.default.bindModal.step1;
return (this.options.bindModal !== null && typeof this.options.bindModal.step1 !== 'undefined') ?
this.options.bindModal.step1 : this.default.bindModal.step1;
},
step2WithModal() {
return (this.options.bindModal) ? this.options.bindModal.step2 : this.default.bindModal.step2;
},
let step2 = (this.options.bindModal !== null && typeof this.options.bindModal.step2 !== 'undefined') ?
this.options.bindModal.step2 : this.default.bindModal.step2;
getTextButton() {
if (this.options.textButton) {
return (this.context.edit) ? this.options.textButton.edit : this.options.textButton.create;
if (step2 === false && this.step1WithModal === true) {
console.log("step2 must open in a Modal");
return true;
}
return (this.context.edit) ? this.default.textButton.edit : this.default.textButton.create;
return step2;
},
getTextTitle() {
if (this.options.title) {
if (this.options.title && (this.options.title.edit !== null || this.options.title.create !== null)) {
return (this.context.edit) ? this.options.title.edit : this.options.title.create;
}
return (this.context.edit) ? this.default.title.edit : this.default.title.create;
return (this.context.edit) ? this.$t(this.default.title.edit) : this.$t(this.default.title.create);
},
getSubmited() {
return this.entity.address;
getTextButton() {
if (this.options.button && this.options.button.text && (this.options.button.text.edit !== null || this.options.button.text.create !== null)) {
return (this.context.edit) ? this.options.button.text.edit : this.options.button.text.create;
}
return (this.context.edit) ? this.default.button.text.edit : this.default.button.text.create;
},
getClassButton() {
let size = (this.options.button && this.options.button.size !== null) ? this.options.button.size : '';
let type = (this.options.button && this.options.button.type !== null) ? this.options.button.type : this.default.button.type;
return size ? size + ' ' + type : type;
},
displayTextButton() {
return (this.options.button !== null && typeof this.options.button.display !== 'undefined') ?
this.options.button.display : this.default.button.display;
}
},
mounted() {
@ -219,13 +241,24 @@ export default {
if (this.context.addressId) {
this.getInitialAddress(this.context.addressId);
}
this.flag.showPane = true;
// when create new address, start first with editPane
if ( this.context.edit === false
&& this.flag.editPane === false
) {
this.openEditPane();
this.flag.editPane = true;
} else {
this.flag.showPane = true;
}
},
openEditPane() {
console.log('open the Edit panel');
this.initForm();
this.getCountries();
},
closeEditPane() {
console.log('close the Edit Panel (with validation)');
this.applyChanges();
@ -373,7 +406,6 @@ export default {
* then update existing address with backend datas when promise is resolved
*/
updateAddress(payload) {
console.log('updateAddress payload', payload);
// TODO change the condition because it writes new postal code in edit mode now: !writeNewPostalCode
this.flag.loading = true;
@ -387,7 +419,6 @@ export default {
body.postcode = {'id': postalCode.id },
patchAddress(payload.addressId, body)
.then(address => new Promise((resolve, reject) => {
console.log('update address');
this.entity.address = address;
this.flag.loading = false;
resolve();
@ -401,7 +432,6 @@ export default {
} else {
patchAddress(payload.addressId, payload.newAddress)
.then(address => new Promise((resolve, reject) => {
console.log('update address');
this.entity.address = address;
this.flag.loading = false;
resolve();
@ -418,7 +448,6 @@ export default {
* creating new address, and receive backend datas when promise is resolved
*/
addAddress(payload) {
console.log('addAddress payload', payload);
this.flag.loading = true;
if('newPostcode' in payload){
@ -431,7 +460,6 @@ export default {
body.postcode = {'id': postalCode.id},
postAddress(body)
.then(address => new Promise((resolve, reject) => {
console.log('add address');
this.entity.address = address;
resolve();
this.flag.loading = false;
@ -445,7 +473,6 @@ export default {
} else {
postAddress(payload)
.then(address => new Promise((resolve, reject) => {
console.log('add address');
this.entity.address = address;
resolve();
this.flag.loading = false;
@ -455,9 +482,55 @@ export default {
this.flag.loading = false;
});
}
},
/*
* When submited
*/
submitAddress() {
console.log('@@@ CLICK button submitAddress');
this.addDateToAddressAndAddressToPerson({
personId: this.context.personId,
addressId: this.entity.address.address_id,
body: { validFrom: { datetime: `${this.$refs.showAddress.validFrom}T00:00:00+0100`}},
backUrl: this.context.backUrl
});
this.initForm();
this.flag.showPane = false;
},
addDateToAddressAndAddressToPerson(payload) {
console.log('addDateToAddressAndAddressToPerson payload', payload);
this.flag.loading = true;
patchAddress(payload.addressId, payload.body)
.then(address => new Promise((resolve, reject) => {
this.valid.from = address.validFrom;
resolve();
}).then(
postAddressToPerson(payload.personId, payload.addressId)
.then(person => new Promise((resolve, reject) => {
console.log('commit addAddressToPerson !!!', person);
this.$props.result = person;
this.flag.loading = false;
this.flag.success = true;
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>

View File

@ -20,41 +20,63 @@
{{ address.country.name.fr }}
</p>
</div>
<div>
<div v-if="address.floor">
<span class="floor">
<b>{{ $t('floor') }}</b>: {{ address.floor }}
</span>
</div>
<div v-if="address.corridor">
<span class="corridor">
<b>{{ $t('corridor') }}</b>: {{ address.corridor }}
</span>
</div>
<div v-if="address.steps">
<span class="steps">
<b>{{ $t('steps') }}</b>: {{ address.steps }}
</span>
</div>
<div v-if="address.flat">
<span class="flat">
<b>{{ $t('flat') }}</b>: {{ address.flat }}
</span>
</div>
<div v-if="address.buildingName">
<span class="buildingName">
<b>{{ $t('buildingName') }}</b>: {{ address.buildingName }}
</span>
</div>
<div v-if="address.extra">
<span class="extra">
<b>{{ $t('extra') }}</b>: {{ address.extra }}
</span>
</div>
<div v-if="address.distribution">
<span class="distribution">
<b>{{ $t('distribution') }}</b>: {{ address.distribution }}
</span>
</div>
</div>
<div v-if="address.floor">
<span class="floor">
<b>{{ $t('floor') }}</b>: {{ address.floor }}
</span>
</div>
<div v-if="address.corridor">
<span class="corridor">
<b>{{ $t('corridor') }}</b>: {{ address.corridor }}
</span>
</div>
<div v-if="address.steps">
<span class="steps">
<b>{{ $t('steps') }}</b>: {{ address.steps }}
</span>
</div>
<div v-if="address.flat">
<span class="flat">
<b>{{ $t('flat') }}</b>: {{ address.flat }}
</span>
</div>
<div v-if="address.buildingName">
<span class="buildingName">
<b>{{ $t('buildingName') }}</b>: {{ address.buildingName }}
</span>
</div>
<div v-if="address.extra">
<span class="extra">
<b>{{ $t('extra') }}</b>: {{ address.extra }}
</span>
</div>
<div v-if="address.distribution">
<span class="distribution">
<b>{{ $t('distribution') }}</b>: {{ address.distribution }}
</span>
<div v-if="!context.edit" class='address-valid date-since'>
<h3>{{ $t('date') }}</h3>
<div class="input-group mb-3">
<span class="input-group-text" id="validFrom"><i class="fa fa-fw fa-calendar"></i></span>
<input type="date" class="form-control form-control-lg" name="validFrom"
v-bind:placeholder="$t('validFrom')"
v-model="validFrom"
aria-describedby="validFrom"
/>
</div>
<!--div v-if="errorMsg.length > 0">
{{ errors }}
</div-->
<div v-if="flag.loading">
{{ $t('loading') }}
</div>
<div v-if="flag.success">
{{ $t('person_address_creation_success') }}
</div>
</div>
</div>
@ -93,7 +115,7 @@ export default {
'errorMsg',
'insideModal'
],
emits: ['openEditPane', 'submitAddress'],
emits: ['openEditPane', 'submitAddress', 'validFrom'],
computed: {
address() {
return this.entity.address;
@ -109,6 +131,9 @@ export default {
},
getSubmited() {
return this.entity.address;
},
validFrom() {
return new Date().toISOString().split('T')[0];
}
}
};

View File

@ -71,12 +71,12 @@
{% macro validity(address, options) %}
{%- if options['with_valid_from'] == true -%}
<span class="address-valid address-since">
<span class="address-valid date-since">
{{ 'Since %date%'|trans( { '%date%' : address.validFrom|format_date('long') } ) }}
</span>
{%- endif -%}
{%- if options['with_valid_to'] == true -%}
<span class="address-valid address-until">
<span class="address-valid date-until">
{{ 'Until %date%'|trans( { '%date%' : address.validTo|format_date('long') } ) }}
</span>
{%- endif -%}

View File

@ -0,0 +1,39 @@
<div id="address"></div>{# <== vue_address component #}
<script type="text/javascript">
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 %}
window.modalTitle = '{{ modalTitle|trans|e('js') }}';
{% endif %}
window.button = {};
{% if buttonText is defined %}
window.buttonText = '{{ buttonText|trans|e('js') }}';
{% endif %}
{% if buttonType is defined %}
window.button.type = '{{ buttonType|e('js') }}';
{% endif %}
{% if buttonSize is defined %}
window.button.size = '{{ buttonSize|e('js') }}';
{% endif %}
{% if buttonDisplay is defined and buttonDisplay == false %}
window.button.display = false;
{% endif %}
{% if binModalStep1 is defined and binModalStep1 == false %}
window.binModalStep1 = false;
{% endif %}
{% if binModalStep2 is defined and binModalStep2 == false %}
window.binModalStep2 = false;
{% endif %}
</script>
{{ encore_entry_script_tags('vue_address') }}
{{ encore_entry_link_tags('vue_address') }}

View File

@ -25,22 +25,15 @@
{% block content %}
<h1>{{ block('title') }}</h1>
<div id="address"></div>
{% include '@ChillPerson/Address/_insert_vue_address.html.twig' with {
address_id: person.lastAddress.id,
mode: 'edit',
binModalStep1: false,
binModalStep2: false,
} %}
{% endblock %}
</div>
{% endblock %}
{% block css %}
{{ encore_entry_link_tags('vue_address') }}
{% endblock %}
{% block js %}
<script type="text/javascript">
window.personId = {{ person.id|e('js') }};
window.addressId = {{ address.id|e('js') }};
window.mode = 'edit';
window.vueRootComponent = 'app';
</script>
{{ encore_entry_script_tags('vue_address') }}
{% endblock %}

View File

@ -27,10 +27,16 @@
<ul class="record_actions my-3">
<li style="margin: auto;">
<a class="btn btn-lg btn-create"
href="{{ path('chill_person_address_new', { 'person_id' : person.id } ) }}">
{{ 'Add an address'|trans }}
</a>
{# include vue_address component #}
{% include '@ChillPerson/Address/_insert_vue_address.html.twig' with {
address_id: person.lastAddress.id,
mode: 'new',
buttonSize: 'btn-lg',
buttonText: 'Add an address',
modalTitle: 'Add an address',
} %}
</li>
</ul>
@ -58,10 +64,16 @@
{% set row = row + 1 %}
{% if address.validTo is not empty and address.validTo < previousRowFrom %}
<div class="{{ 'row' ~ row ~ ' ' }}col-a action">
{# include vue_address component #}
<a href="" class="btn btn-sm btn-create">{{ 'Insert an address'|trans }}</a></div>
<div class="{{ 'row' ~ row ~ ' ' }}col-b"></div>
<div class="{{ 'row' ~ row ~ ' ' }}col-c action">
{# include vue_address component #}
<a href="" class="btn btn-sm btn-create">{{ 'Insert an address'|trans }}</a></div>
<div class="date">
{% if address.validTo is not empty %}
{{ address.validTo|format_date('short') }}
@ -81,7 +93,10 @@
'has_no_address': true
}) }}
<ul class="record_actions">
{# include vue_address component #}
<li><a href="{{ path('chill_person_address_edit', { 'person_id': person.id, 'address_id' : address.id } ) }}" class="btn btn-edit"></a></li>
</ul>
</div>
{# endif #}
@ -130,6 +145,7 @@
</a>
</li>
<li>
{# include vue_address component #}
<a class="btn btn-create"
href="{{ path('chill_person_address_new', { 'person_id' : person.id } ) }}">
{{ 'Add an address'|trans }}

View File

@ -25,21 +25,16 @@
{% block content %}
<h1>{{ block('title') }}</h1>
<div id="address"></div>
{% include '@ChillPerson/Address/_insert_vue_address.html.twig' with {
address_id: person.lastAddress.id,
mode: 'new',
binModalStep1: false,
binModalStep2: false,
} %}
{% endblock %}
</div>
{% endblock %}
{% block css %}
{{ encore_entry_link_tags('vue_address') }}
{% endblock %}
{% block js %}
<script type="text/javascript">
window.personId = {{ person.id|e('js') }};
window.mode = 'new';
window.vueRootComponent = 'app';
</script>
{{ encore_entry_script_tags('vue_address') }}
{% endblock %}

View File

@ -200,10 +200,17 @@ This view should receive those arguments:
<ul class="list-inline text-right mt-2">
<li class="list-inline-item">
<a class="btn btn-warning btn-sm" title="{{ 'Edit'|trans }}"
href="{{ path('chill_person_address_edit', { 'person_id': person.id, 'address_id' : person.lastAddress.id } ) }}">
<i class="fa fa-pencil fa-fw"></i>
</a>
{# include vue_address component #}
{% include '@ChillPerson/Address/_insert_vue_address.html.twig' with {
address_id: person.lastAddress.id,
backUrl: path('chill_person_view', { 'person_id': person.id }),
mode: 'edit',
modalTitle: 'Edit address',
buttonText: 'Edit address',
buttonType: 'btn-update',
buttonSize: 'btn-sm',
buttonDisplay: false
} %}
</li>
<li class="list-inline-item">
<a class="btn btn-secondary btn-sm" title="{{ "Addresses history"|trans }}"

View File

@ -208,6 +208,7 @@ The address has been successfully updated: L'adresse a été mise à jour avec s
Update address for %name%: Mettre à jour une adresse pour %name%
Modify address for %name%: Modifier une adresse pour %name%
Edit an address: Modifier une adresse
Edit address: Modifier l'adresse
Addresses history for %name%: Historique des adresses de %name%
Addresses history: Historique des adresses
New address : Nouvelle adresse