From 8c01e16a8336a91659db7bf73ffe94077f90e25b Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sat, 1 Oct 2022 20:32:57 +0530 Subject: Bug Fix: Couldn't add shipping address while editing existing client --- src/components/editors/client-editor.js | 18 ++++++++++-------- src/components/editors/multi-address-editor.js | 6 +----- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/components/editors/client-editor.js b/src/components/editors/client-editor.js index e65ab8f..e07c749 100644 --- a/src/components/editors/client-editor.js +++ b/src/components/editors/client-editor.js @@ -35,22 +35,23 @@ const ClientEditor = (props) => { : props.client.ShippingAddresses.length === 0 ); - useEffect(() => { - // will delete existing shipping addresses if false - setShippingAddresses(shipToBillingAddress ? [] : [new Address()]) - }, [shipToBillingAddress]); + // will delete existing shipping addresses if false + useEffect(() => + setShippingAddresses(shipToBillingAddress ? [] : (shippingAddresses.length > 0 ? shippingAddresses : [new Address()])) + , [shipToBillingAddress]); const handleSubmit = (e) => { e.preventDefault(); const client = new Client(); + client.Id = props.client.Id; client.Name = name; client.GSTIN = GSTIN; client.Contact = contact; client.BillingAddress = billingAddress; client.ShippingAddresses = shipToBillingAddress ? [billingAddress] - : shippingAddresses + : shippingAddresses; // remove blank phone numbers/email addresses client.Contact.Phones = client.Contact.Phones.filter(i => i !== ""); @@ -61,20 +62,21 @@ const ClientEditor = (props) => { : saveClient(client, handleSuccess, handleFail); } - const handleSuccess = () => { + const handleSuccess = (res) => { + console.log("Successfully saved client", res) clearAll(); props.successCallback(); props.editing && props.hide(); } const handleFail = (err) => { - alert("fail"); + alert("error while saving client. please check logs"); console.log(err); } const clearAll = () => { setName(""); - setGSTIN("") + setGSTIN(""); setContact(new Contact()); setBillingAddress(new Address()); setShippingAddresses([]); diff --git a/src/components/editors/multi-address-editor.js b/src/components/editors/multi-address-editor.js index a0182f2..bb69e20 100644 --- a/src/components/editors/multi-address-editor.js +++ b/src/components/editors/multi-address-editor.js @@ -18,17 +18,13 @@ import { Address } from './../../classes/client'; import AddressEditor from './address-editor'; -import { useState, useEffect } from 'react'; - const MultiAddressEditor = ({addresses, setAddresses, setShipToBillingAddress}) => { + console.log(addresses) const handleChange = (id, data) => { - console.log(id, data) - const newAddresses = [...addresses]; newAddresses[id] = { ...newAddresses[id], ...data } - setAddresses(newAddresses) } -- cgit v1.2.3