From 328e30e8746b905df3a6e0a4c6c02cdb6f191729 Mon Sep 17 00:00:00 2001 From: HenriT Date: Wed, 14 Apr 2021 11:52:32 +0300 Subject: [PATCH] Update team and client when updating custom field on invoice. --- src/components/clients/ClientForm.vue | 10 ++++++++-- .../invoices/InvoiceClientFields.vue | 4 ++++ src/components/invoices/InvoiceTeamFields.vue | 4 ++++ src/components/team/TeamFields.vue | 11 ----------- src/store/client-fields.js | 12 ++++++++---- src/store/clients.js | 19 +++++++------------ src/store/invoices.js | 3 ++- src/store/models/invoice-team-field.js | 1 + 8 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/components/clients/ClientForm.vue b/src/components/clients/ClientForm.vue index 1f0f25b..50e1f61 100644 --- a/src/components/clients/ClientForm.vue +++ b/src/components/clients/ClientForm.vue @@ -123,11 +123,17 @@ export default { }, updateProp(props) { if (this.isNew) { - return this.$store.dispatch('clients/clientProps', props); + return this.$store.dispatch('clients/clientProps', { + props, + clientId: this.client.id, + }); } this.errors.clear(); - this.$store.dispatch('clients/updateClient', props) + this.$store.dispatch('clients/updateClient', { + props, + clientId: this.client.id, + }) .then(() => { NotificationService.success('Updated'); }) diff --git a/src/components/invoices/InvoiceClientFields.vue b/src/components/invoices/InvoiceClientFields.vue index 5537c99..75bfe2c 100644 --- a/src/components/invoices/InvoiceClientFields.vue +++ b/src/components/invoices/InvoiceClientFields.vue @@ -24,6 +24,10 @@ export default { fieldId: field.id, invoiceId: this.invoice.id, }); + this.$store.dispatch('clientFields/updateClientField', { + fieldId: field.client_field_id, + props, + }); }, }, }; diff --git a/src/components/invoices/InvoiceTeamFields.vue b/src/components/invoices/InvoiceTeamFields.vue index 4d21645..ef91da0 100644 --- a/src/components/invoices/InvoiceTeamFields.vue +++ b/src/components/invoices/InvoiceTeamFields.vue @@ -24,6 +24,10 @@ export default { fieldId: field.id, invoiceId: this.invoice.id, }); + this.$store.dispatch('teamFields/updateTeamField', { + fieldId: field.team_field_id, + props, + }); }, }, }; diff --git a/src/components/team/TeamFields.vue b/src/components/team/TeamFields.vue index dd837e6..45b6a07 100644 --- a/src/components/team/TeamFields.vue +++ b/src/components/team/TeamFields.vue @@ -27,11 +27,6 @@ export default { AppEditable, AppInput, }, - computed: { - isNew() { - return this.team && this.team.$isNew; - }, - }, methods: { addNewField() { this.$store.dispatch('teamFields/addNewField', this.team.id); @@ -54,12 +49,6 @@ export default { } }, updateFieldProp(props, field) { - if (this.isNew) { - return this.$store.dispatch('teamFields/teamFieldProps', { - props, - fieldId: field.id, - }); - } this.$store.dispatch('teamFields/updateTeamField', { props, fieldId: field.id, diff --git a/src/store/client-fields.js b/src/store/client-fields.js index 0429cf4..f84718f 100644 --- a/src/store/client-fields.js +++ b/src/store/client-fields.js @@ -14,8 +14,10 @@ export default { }); }, async updateClientField({ dispatch }, payload) { - await dispatch('clientFieldProps', payload); - return dispatch('clients/updateClient', null, { root: true }); // TODO: pass clientId to make generic + const field = await dispatch('clientFieldProps', payload); + return dispatch('clients/updateClient', { + clientId: field.client_id, + }, { root: true }); }, async addNewField(store, clientId) { const field = await ClientField.createNew(); @@ -38,8 +40,10 @@ export default { })); }, async deleteClientField({ dispatch }, fieldId) { - await ClientField.delete(fieldId); - return dispatch('clients/updateClient', null, { root: true }); // TODO: pass clientId to make generic + const field = await ClientField.delete(fieldId); + return dispatch('clients/updateClient', { + clientId: field.client_id, + }, { root: true }); // TODO: pass clientId to make generic }, }, }; diff --git a/src/store/clients.js b/src/store/clients.js index ab0b5f4..e1d7cbc 100644 --- a/src/store/clients.js +++ b/src/store/clients.js @@ -48,22 +48,17 @@ export default { await Client.insert({ data: res }); return getClientById(res.id); }, - clientProps({ state }, props) { + clientProps(store, payload) { return Client.update({ - where: state.clientId, - data: props, + where: payload.clientId, + data: payload.props, }); }, - async updateClient({ getters, dispatch }, props) { - if (props) { - await dispatch('clientProps', props); + async updateClient({ dispatch }, payload) { + if (payload.props) { + await dispatch('clientProps', payload.props); } - return ClientService.updateClient(getters.client); - }, - async updateClientById(store, payload) { - const client = getClientById(payload.clientId); - client.$update(payload.props); - return ClientService.updateClient(client); + return ClientService.updateClient(getClientById(payload.clientId)); }, async openNewClientModal({ commit }) { const client = await Client.createNew(); diff --git a/src/store/invoices.js b/src/store/invoices.js index 89f90ad..408420f 100644 --- a/src/store/invoices.js +++ b/src/store/invoices.js @@ -82,7 +82,7 @@ export default { const invoice = getInvoice(payload.invoiceId); if (Object.keys(clientProps).length > 0 && invoice.client_id) { - dispatch('clients/updateClientById', { + dispatch('clients/updateClient', { props: clientProps, clientId: invoice.client_id, }, { root: true }); @@ -189,6 +189,7 @@ export default { props: { label: field.label, value: field.value, + team_field_id: field.id, }, }, { root: true }); }); diff --git a/src/store/models/invoice-team-field.js b/src/store/models/invoice-team-field.js index 410f204..02d781a 100644 --- a/src/store/models/invoice-team-field.js +++ b/src/store/models/invoice-team-field.js @@ -9,6 +9,7 @@ export default class InvoiceTeamField extends Model { return { id: this.attr(() => uuidv4()), invoice_id: this.attr(null), + team_field_id: this.attr(null), label: this.attr(''), value: this.attr(''), };