Fix : Customers.

This commit is contained in:
elforjani3
2020-11-11 17:27:44 +02:00
parent 77aeaf2c60
commit cda856ef8d
4 changed files with 86 additions and 71 deletions

View File

@@ -4,8 +4,6 @@ import { useFormik } from 'formik';
import moment from 'moment'; import moment from 'moment';
import { Intent } from '@blueprintjs/core'; import { Intent } from '@blueprintjs/core';
import { FormattedMessage as T, useIntl } from 'react-intl'; import { FormattedMessage as T, useIntl } from 'react-intl';
import { useHistory } from 'react-router-dom';
import { pick } from 'lodash';
import classNames from 'classnames'; import classNames from 'classnames';
import { CLASSES } from 'common/classes'; import { CLASSES } from 'common/classes';
@@ -23,7 +21,43 @@ import withMediaActions from 'containers/Media/withMediaActions';
import withCustomers from 'containers/Customers//withCustomers'; import withCustomers from 'containers/Customers//withCustomers';
import useMedia from 'hooks/useMedia'; import useMedia from 'hooks/useMedia';
import { compose } from 'utils'; import { compose, transformToForm } from 'utils';
const defaultInitialValues = {
customer_type: 'business',
salutation: '',
first_name: '',
last_name: '',
company_name: '',
display_name: '',
email: '',
work_phone: '',
personal_phone: '',
website: '',
note: '',
active: true,
billing_address_country: '',
billing_address_1: '',
billing_address_2: '',
billing_address_city: '',
billing_address_state: '',
billing_address_postcode: '',
billing_address_phone: '',
shipping_address_country: '',
shipping_address_1: '',
shipping_address_2: '',
shipping_address_city: '',
shipping_address_state: '',
shipping_address_postcode: '',
shipping_address_phone: '',
opening_balance: '',
currency_code: '',
opening_balance_at: moment(new Date()).format('YYYY-MM-DD'),
};
/** /**
* Customer form. * Customer form.
@@ -36,7 +70,7 @@ function CustomerForm({
customers, customers,
// #withCustomerDetail // #withCustomerDetail
customer, customerDetail,
// #withCustomersActions // #withCustomersActions
requestSubmitCustomer, requestSubmitCustomer,
@@ -47,11 +81,13 @@ function CustomerForm({
requestDeleteMedia, requestDeleteMedia,
// #Props // #Props
customerId,
onFormSubmit, onFormSubmit,
onCancelForm, onCancelForm,
}) { }) {
const isNewMode = !customerId;
const { formatMessage } = useIntl(); const { formatMessage } = useIntl();
const history = useHistory();
const [payload, setPayload] = useState({}); const [payload, setPayload] = useState({});
const { const {
@@ -108,56 +144,15 @@ function CustomerForm({
opening_balance_at: Yup.date(), opening_balance_at: Yup.date(),
}); });
const defaultInitialValues = useMemo( /**
() => ({ * Initial values in create and edit mode.
customer_type: 'business', */
salutation: '',
first_name: '',
last_name: '',
company_name: '',
display_name: '',
email: '',
work_phone: '',
personal_phone: '',
website: '',
note: '',
active: true,
billing_address_country: '',
billing_address_1: '',
billing_address_2: '',
billing_address_city: '',
billing_address_state: '',
billing_address_postcode: null,
billing_address_phone: '',
shipping_address_country: '',
shipping_address_1: '',
shipping_address_2: '',
shipping_address_city: '',
shipping_address_state: '',
shipping_address_postcode: null,
shipping_address_phone: '',
opening_balance: '',
currency_code: '',
opening_balance_at: moment(new Date()).format('YYYY-MM-DD'),
}),
[],
);
const initialValues = useMemo( const initialValues = useMemo(
() => ({ () => ({
...(customer ...defaultInitialValues,
? { ...transformToForm(customerDetail, defaultInitialValues),
...pick(customer, Object.keys(defaultInitialValues)),
}
: {
...defaultInitialValues,
}),
}), }),
[customer, defaultInitialValues], [customerDetail, defaultInitialValues],
); );
const saveInvokeSubmit = useCallback( const saveInvokeSubmit = useCallback(
@@ -169,18 +164,20 @@ function CustomerForm({
); );
useEffect(() => { useEffect(() => {
customer && customer.id !isNewMode
? changePageTitle(formatMessage({ id: 'edit_customer' })) ? changePageTitle(formatMessage({ id: 'edit_customer' }))
: changePageTitle(formatMessage({ id: 'new_customer' })); : changePageTitle(formatMessage({ id: 'new_customer' }));
}, [changePageTitle, customer, formatMessage]); }, [changePageTitle, isNewMode, formatMessage]);
//Handles the form submit.
const handleFormSubmit = ( const handleFormSubmit = (
values, values,
{ setSubmitting, resetForm, setErrors }, { setSubmitting, resetForm, setErrors },
) => { ) => {
const formValues = { ...values, status: payload.publish }; const formValues = { ...values, status: payload.publish };
if (customer && customer.id) {
requestEditCustomer(customer.id, formValues) if (customerDetail && customerDetail.id) {
requestEditCustomer(customerDetail.id, formValues)
.then((response) => { .then((response) => {
AppToaster.show({ AppToaster.show({
message: formatMessage({ message: formatMessage({
@@ -220,6 +217,7 @@ function CustomerForm({
values, values,
touched, touched,
isSubmitting, isSubmitting,
setErrors,
handleSubmit, handleSubmit,
} = useFormik({ } = useFormik({
enableReinitialize: true, enableReinitialize: true,
@@ -231,8 +229,8 @@ function CustomerForm({
}); });
const initialAttachmentFiles = useMemo(() => { const initialAttachmentFiles = useMemo(() => {
return customer && customer.media return customerDetail && customerDetail.media
? customer.media.map((attach) => ({ ? customerDetail.media.map((attach) => ({
preview: attach.attachment_file, preview: attach.attachment_file,
upload: true, upload: true,
metadata: { ...attach }, metadata: { ...attach },
@@ -301,7 +299,7 @@ function CustomerForm({
errors={errors} errors={errors}
values={values} values={values}
touched={touched} touched={touched}
customerId={customer} customerId={customerDetail}
/> />
</div> </div>
</form> </form>
@@ -311,7 +309,7 @@ function CustomerForm({
onSubmitClick={handleSubmitClick} onSubmitClick={handleSubmitClick}
// customer={customer} // customer={customer}
onCancelClick={handleCancelClick} onCancelClick={handleCancelClick}
customerId={customer} customerId={customerDetail}
/> />
</div> </div>
); );

View File

@@ -87,17 +87,33 @@ function CustomersList({
setDeleteCustomer(false); setDeleteCustomer(false);
}, [setDeleteCustomer]); }, [setDeleteCustomer]);
// handle confirm delete customer. const transformErrors = (errors) => {
const handleConfirmDeleteCustomer = useCallback(() => { if (errors.some((e) => e.type === 'CUSTOMER.HAS.SALES_INVOICES')) {
requestDeleteCustomer(deleteCustomer.id).then(() => {
AppToaster.show({ AppToaster.show({
message: formatMessage({ message: formatMessage({
id: 'the_customer_has_been_successfully_deleted', id: 'customer_has_sales_invoices',
}), }),
intent: Intent.SUCCESS, intent: Intent.DANGER,
});
}
};
// handle confirm delete customer.
const handleConfirmDeleteCustomer = useCallback(() => {
requestDeleteCustomer(deleteCustomer.id)
.then(() => {
setDeleteCustomer(false);
AppToaster.show({
message: formatMessage({
id: 'the_customer_has_been_successfully_deleted',
}),
intent: Intent.SUCCESS,
});
})
.catch((errors) => {
setDeleteCustomer(false);
transformErrors(errors);
}); });
setDeleteCustomer(false);
});
}, [requestDeleteCustomer, deleteCustomer, formatMessage]); }, [requestDeleteCustomer, deleteCustomer, formatMessage]);
// Handle fetch data table. // Handle fetch data table.

View File

@@ -2,7 +2,7 @@ import { connect } from 'react-redux';
import { getCustomerById } from 'store/customers/customers.reducer'; import { getCustomerById } from 'store/customers/customers.reducer';
const mapStateToProps = (state, props) => ({ const mapStateToProps = (state, props) => ({
customer: getCustomerById(state, props.customerId), customerDetail: getCustomerById(state, props.customerId),
}); });
export default connect(mapStateToProps); export default connect(mapStateToProps);

View File

@@ -219,7 +219,7 @@ export default {
'The item category has been successfully edited.', 'The item category has been successfully edited.',
the_item_category_has_been_successfully_deleted: the_item_category_has_been_successfully_deleted:
'The item category has been successfully deleted', 'The item category has been successfully deleted',
once_delete_these_item_categories_you_will_not_able_restore_them: once_delete_these_item_categories_you_will_not_able_restore_them:
"Once you delete these categories, you won't be able to retrieve them later. Are you sure you want to delete them?", "Once you delete these categories, you won't be able to retrieve them later. Are you sure you want to delete them?",
once_delete_these_views_you_will_not_able_restore_them: once_delete_these_views_you_will_not_able_restore_them:
"Once you delete the custom view, you won't be able to restore it later. Are you sure you want to delete this view?", "Once you delete the custom view, you won't be able to restore it later. Are you sure you want to delete this view?",
@@ -809,11 +809,12 @@ export default {
notes: 'Notes', notes: 'Notes',
i_purchase_this_item: 'I purchase this item from a vendor.', i_purchase_this_item: 'I purchase this item from a vendor.',
i_sell_this_item: 'I sell this item to a customer.', i_sell_this_item: 'I sell this item to a customer.',
select_display_name_as:'Select display name as', select_display_name_as: 'Select display name as',
opening_date: 'Opening date', opening_date: 'Opening date',
item_code: 'Item code', item_code: 'Item code',
quantity_on_hand: 'Quantity on hand', quantity_on_hand: 'Quantity on hand',
average_rate: 'Average rate', average_rate: 'Average rate',
the_name_used_before: 'The name is already used.', the_name_used_before: 'The name is already used.',
the_item_has_associated_transactions: 'The item has associated transactions.' the_item_has_associated_transactions: 'The item has associated transactions.',
customer_has_sales_invoices: 'Customer has sales invoices',
}; };