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
? {
...pick(customer, Object.keys(defaultInitialValues)),
}
: {
...defaultInitialValues, ...defaultInitialValues,
...transformToForm(customerDetail, defaultInitialValues),
}), }),
}), [customerDetail, defaultInitialValues],
[customer, 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,16 +87,32 @@ function CustomersList({
setDeleteCustomer(false); setDeleteCustomer(false);
}, [setDeleteCustomer]); }, [setDeleteCustomer]);
const transformErrors = (errors) => {
if (errors.some((e) => e.type === 'CUSTOMER.HAS.SALES_INVOICES')) {
AppToaster.show({
message: formatMessage({
id: 'customer_has_sales_invoices',
}),
intent: Intent.DANGER,
});
}
};
// handle confirm delete customer. // handle confirm delete customer.
const handleConfirmDeleteCustomer = useCallback(() => { const handleConfirmDeleteCustomer = useCallback(() => {
requestDeleteCustomer(deleteCustomer.id).then(() => { requestDeleteCustomer(deleteCustomer.id)
.then(() => {
setDeleteCustomer(false);
AppToaster.show({ AppToaster.show({
message: formatMessage({ message: formatMessage({
id: 'the_customer_has_been_successfully_deleted', id: 'the_customer_has_been_successfully_deleted',
}), }),
intent: Intent.SUCCESS, intent: Intent.SUCCESS,
}); });
})
.catch((errors) => {
setDeleteCustomer(false); setDeleteCustomer(false);
transformErrors(errors);
}); });
}, [requestDeleteCustomer, deleteCustomer, formatMessage]); }, [requestDeleteCustomer, deleteCustomer, formatMessage]);

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

@@ -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',
}; };