feat(customer): add contact duplicate.

This commit is contained in:
elforjani3
2021-03-03 16:39:44 +02:00
parent 30f66480d0
commit 7eca87215c
5 changed files with 56 additions and 18 deletions

View File

@@ -25,7 +25,7 @@ export default function CustomerFloatingActions() {
const history = useHistory(); const history = useHistory();
// Customer form context. // Customer form context.
const { customerId, setSubmitPayload } = useCustomerFormContext(); const { isNewMode,setSubmitPayload } = useCustomerFormContext();
// Formik context. // Formik context.
const { resetForm, submitForm, isSubmitting } = useFormikContext(); const { resetForm, submitForm, isSubmitting } = useFormikContext();
@@ -61,7 +61,7 @@ export default function CustomerFloatingActions() {
intent={Intent.PRIMARY} intent={Intent.PRIMARY}
type="submit" type="submit"
onClick={handleSubmitBtnClick} onClick={handleSubmitBtnClick}
text={customerId ? <T id={'edit'} /> : <T id={'save'} />} text={!isNewMode ? <T id={'edit'} /> : <T id={'save'} />}
/> />
<Popover <Popover
content={ content={
@@ -88,7 +88,7 @@ export default function CustomerFloatingActions() {
className={'ml1'} className={'ml1'}
disabled={isSubmitting} disabled={isSubmitting}
onClick={handleClearBtnClick} onClick={handleClearBtnClick}
text={customerId ? <T id={'reset'} /> : <T id={'clear'} />} text={!isNewMode ? <T id={'reset'} /> : <T id={'clear'} />}
/> />
{/* ----------- Cancel ----------- */} {/* ----------- Cancel ----------- */}
<Button <Button

View File

@@ -67,11 +67,13 @@ function CustomerForm({
customer, customer,
customerId, customerId,
submitPayload, submitPayload,
contactDuplicate,
editCustomerMutate, editCustomerMutate,
createCustomerMutate, createCustomerMutate,
isNewMode,
} = useCustomerFormContext(); } = useCustomerFormContext();
const isNewMode = !customerId; // const isNewMode = !customerId;
const history = useHistory(); const history = useHistory();
const { formatMessage } = useIntl(); const { formatMessage } = useIntl();
@@ -82,11 +84,11 @@ function CustomerForm({
() => ({ () => ({
...defaultInitialValues, ...defaultInitialValues,
currency_code: baseCurrency, currency_code: baseCurrency,
...transformToForm(customer, defaultInitialValues), ...transformToForm(customer || contactDuplicate, defaultInitialValues),
}), }),
[customer, baseCurrency], [customer, contactDuplicate, baseCurrency],
); );
//Handles the form submit. //Handles the form submit.
const handleFormSubmit = ( const handleFormSubmit = (
values, values,
@@ -97,9 +99,9 @@ function CustomerForm({
const onSuccess = () => { const onSuccess = () => {
AppToaster.show({ AppToaster.show({
message: formatMessage({ message: formatMessage({
id: customer id: isNewMode
? 'the_item_customer_has_been_edited_successfully' ? 'the_customer_has_been_created_successfully'
: 'the_customer_has_been_created_successfully', : 'the_item_customer_has_been_edited_successfully',
}), }),
intent: Intent.SUCCESS, intent: Intent.SUCCESS,
}); });
@@ -115,12 +117,12 @@ function CustomerForm({
setSubmitting(false); setSubmitting(false);
}; };
if (customer && customer.id) { if (isNewMode) {
createCustomerMutate(formValues).then(onSuccess).catch(onError);
} else {
editCustomerMutate([customer.id, formValues]) editCustomerMutate([customer.id, formValues])
.then(onSuccess) .then(onSuccess)
.catch(onError); .catch(onError);
} else {
createCustomerMutate(formValues).then(onSuccess).catch(onError);
} }
}; };

View File

@@ -1,4 +1,5 @@
import React, { useState, createContext } from 'react'; import React, { useState, createContext } from 'react';
import { useLocation } from 'react-router-dom';
import DashboardInsider from 'components/Dashboard/DashboardInsider'; import DashboardInsider from 'components/Dashboard/DashboardInsider';
import { import {
useCustomers, useCustomers,
@@ -6,17 +7,30 @@ import {
useCurrencies, useCurrencies,
useCreateCustomer, useCreateCustomer,
useEditCustomer, useEditCustomer,
useContact,
} from 'hooks/query'; } from 'hooks/query';
const CustomerFormContext = createContext(); const CustomerFormContext = createContext();
function CustomerFormProvider({ customerId, ...props }) { function CustomerFormProvider({ customerId, ...props }) {
const { state } = useLocation();
const contactId = state?.action;
// Handle fetch customer details. // Handle fetch customer details.
const { data: customer, isFetching: isCustomerLoading } = useCustomer( const { data: customer, isFetching: isCustomerLoading } = useCustomer(
customerId, customerId,
{ {
enabled: !!customerId, enabled: !!customerId,
} },
);
// Handle fetch contact duplicate details.
const { data: contactDuplicate, isFetching: isContactLoading } = useContact(
contactId,
{
enabled: !!contactId,
},
); );
// Handle fetch customers data table // Handle fetch customers data table
@@ -34,12 +48,17 @@ function CustomerFormProvider({ customerId, ...props }) {
const { mutateAsync: editCustomerMutate } = useEditCustomer(); const { mutateAsync: editCustomerMutate } = useEditCustomer();
const { mutateAsync: createCustomerMutate } = useCreateCustomer(); const { mutateAsync: createCustomerMutate } = useCreateCustomer();
// determines whether the form new or duplicate mode.
const isNewMode = contactId || !customerId;
const provider = { const provider = {
customerId, customerId,
customer, customer,
customers, customers,
currencies, currencies,
contactDuplicate,
submitPayload, submitPayload,
isNewMode,
isCustomerLoading, isCustomerLoading,
isCustomersLoading, isCustomersLoading,
@@ -47,12 +66,17 @@ function CustomerFormProvider({ customerId, ...props }) {
setSubmitPayload, setSubmitPayload,
editCustomerMutate, editCustomerMutate,
createCustomerMutate createCustomerMutate,
}; };
return ( return (
<DashboardInsider <DashboardInsider
loading={isCustomerLoading || isCustomerLoading || isCurrenciesLoading} loading={
isCustomerLoading ||
isCustomerLoading ||
isCurrenciesLoading ||
isContactLoading
}
name={'customer-form'} name={'customer-form'}
> >
<CustomerFormContext.Provider value={provider} {...props} /> <CustomerFormContext.Provider value={provider} {...props} />

View File

@@ -29,6 +29,8 @@ function CustomersTable({
// #withAlerts // #withAlerts
openAlert, openAlert,
// #withDialogActions
openDialog,
}) { }) {
const history = useHistory(); const history = useHistory();
@@ -66,6 +68,10 @@ function CustomersTable({
history.push(`/customers/${customer.id}/edit`); history.push(`/customers/${customer.id}/edit`);
}; };
const handleContactDuplicate = ({ id }) => {
openDialog('contact-duplicate', { contactId: id });
};
if (isEmptyStatus) { if (isEmptyStatus) {
return <CustomersEmptyStatus />; return <CustomersEmptyStatus />;
} }
@@ -95,6 +101,7 @@ function CustomersTable({
payload={{ payload={{
onDelete: handleCustomerDelete, onDelete: handleCustomerDelete,
onEdit: handleCustomerEdit, onEdit: handleCustomerEdit,
onDuplicate: handleContactDuplicate,
}} }}
ContextMenu={ActionsMenu} ContextMenu={ActionsMenu}
/> />

View File

@@ -18,7 +18,7 @@ import { useIntl } from 'react-intl';
*/ */
export function ActionsMenu({ export function ActionsMenu({
row: { original }, row: { original },
payload: { onEdit, onDelete }, payload: { onEdit, onDelete ,onDuplicate },
}) { }) {
const { formatMessage } = useIntl(); const { formatMessage } = useIntl();
@@ -34,6 +34,11 @@ export function ActionsMenu({
text={formatMessage({ id: 'edit_customer' })} text={formatMessage({ id: 'edit_customer' })}
onClick={safeCallback(onEdit, original)} onClick={safeCallback(onEdit, original)}
/> />
<MenuItem
icon={<Icon icon="duplicate-18" />}
text={formatMessage({ id: 'duplicate' })}
onClick={safeCallback(onDuplicate, original)}
/>
<MenuItem <MenuItem
icon={<Icon icon="trash-16" iconSize={16} />} icon={<Icon icon="trash-16" iconSize={16} />}
text={formatMessage({ id: 'delete_customer' })} text={formatMessage({ id: 'delete_customer' })}