aboutsummaryrefslogtreecommitdiff
path: root/src/components/editors/multi-address-editor.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/editors/multi-address-editor.js')
-rw-r--r--src/components/editors/multi-address-editor.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/components/editors/multi-address-editor.js b/src/components/editors/multi-address-editor.js
new file mode 100644
index 0000000..a0182f2
--- /dev/null
+++ b/src/components/editors/multi-address-editor.js
@@ -0,0 +1,62 @@
+/* OpenBills-web - Web based libre billing software
+ * Copyright (C) 2022 Vidhu Kant Sharma <vidhukant@vidhukant.xyz>
+
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+import { Address } from './../../classes/client';
+import AddressEditor from './address-editor';
+
+import { useState, useEffect } from 'react';
+
+const MultiAddressEditor = ({addresses, setAddresses, setShipToBillingAddress}) => {
+ const handleChange = (id, data) => {
+ console.log(id, data)
+
+ const newAddresses = [...addresses];
+ newAddresses[id] = {
+ ...newAddresses[id], ...data
+ }
+
+ setAddresses(newAddresses)
+ }
+
+ const handleDelete = (id) => {
+ // deleting the last address sets
+ // shipToBillingAddress to true
+ if (addresses.length === 1) {
+ setShipToBillingAddress(true);
+ } else {
+ setAddresses([
+ ...addresses.slice(0, id),
+ ...addresses.slice(id+1)
+ ]);
+ }
+ }
+
+ return (
+ <>
+ {addresses.map((i, id) =>
+ <AddressEditor
+ key={id}
+ heading={`Shipping Address ${addresses.length === 1 ? '' : id + 1}`}
+ address={i}
+ deleteAddress={() => handleDelete(id)}
+ setAddress={(data) => {handleChange(id, data)}} />
+ )}
+ </>
+ );
+}
+
+export default MultiAddressEditor;