From a1534066073f3197623565d597e5126a5c01bff9 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Wed, 5 Oct 2022 22:16:53 +0530 Subject: added client picker --- src/components/pickers/client-picker.js | 145 ++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 src/components/pickers/client-picker.js (limited to 'src/components/pickers/client-picker.js') diff --git a/src/components/pickers/client-picker.js b/src/components/pickers/client-picker.js new file mode 100644 index 0000000..bca8566 --- /dev/null +++ b/src/components/pickers/client-picker.js @@ -0,0 +1,145 @@ +/* OpenBills-web - Web based libre billing software + * Copyright (C) 2022 Vidhu Kant Sharma + + * 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 . + */ + +import { Client, InvoiceClient, getAllClients, Address } from '../../classes/client'; +import './scss/client-picker.scss'; + +import { useState, useEffect } from 'react'; +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' +import { faPhone, faEnvelope, faGlobe } from '@fortawesome/free-solid-svg-icons' + +const ClientPicker = ({ client, setClient, shippingAddressId, setShippingAddressId }) => { + const [clients, setClients] = useState([new Client()]); + + useEffect(() => refreshClients, []); + + const refreshClients = () => + getAllClients(setClients, () => {}); + + const handleClientSelect = (e) => { + const c = clients.filter(i => i.Id === e.target.value )[0]; + setClient(c ? c : new InvoiceClient()); + } + + const shouldShowAddressPicker = () => { + if (client.Id !== null) { + if (client.ShippingAddresses.length > 0) { + // if the only address is same as billing address, dont show + if (client.ShippingAddresses.length === 1) { + return JSON.stringify(client.ShippingAddresses[0]) !== + JSON.stringify(client.BillingAddress); + } + return true; + } + } + return false; + } + + const formatAddress = (addr) => + `${addr.Text.length > 30 ? addr.Text.substring(0, 30) + "..." : addr.Text} | ${addr.City}`; + + const formatClientName = (client) => + `${client.Name.length > 30 ? client.Name.substring(0, 30) + "..." : client.Name} | ${client.BillingAddress.City}`; + + return ( +
+

Invoice Recipient

+
+
+ {clients && clients.length > 0 && + <> + + + {shouldShowAddressPicker() && + + } +

GSTIN: {client.GSTIN === "" ? "URP" : client.GSTIN}

+ + } +
+ +
+

+ Name: {client.Contact.Name}
+ {client.Contact.Phones.length > 0 && + <> + + {client.Contact.Phones.map((i, id) => {` ${i}${id + 1 === client.Contact.Phones.length ? '' : ','}`})} +
+ + } + {client.Contact.Emails.length > 0 && + <> + + {client.Contact.Emails.map((i, id) => {` ${i}${id + 1 === client.Contact.Emails.length ? '' : ','}`})} +
+ + } + {client.Contact.Website.length > 0 && + <> + + {client.Contact.Website} + + + } +

+
+ + {client.Id !== null && // if client is selected + <> +
+

+ Billing Address:
+ {client.BillingAddress.Text}
+ {client.BillingAddress.City}, {client.BillingAddress.State} - {client.BillingAddress.PostalCode} ({client.BillingAddress.Country}) +

+
+ {shouldShowAddressPicker() && shippingAddressId >= 0 && +
+

+ Shipping Address:
+ {client.ShippingAddresses[shippingAddressId].Text}
+ {client.ShippingAddresses[shippingAddressId].City}, {client.ShippingAddresses[shippingAddressId].State} - {client.ShippingAddresses[shippingAddressId].PostalCode} ({client.ShippingAddresses[shippingAddressId].Country}) +

+
+ } + + } +
+
+
+ ); +} + +export default ClientPicker; -- cgit v1.2.3