feat(payment receive & made): handle error.

This commit is contained in:
elforjani13
2022-03-17 14:34:24 +02:00
parent 7f3a494c8d
commit b0407168a0
10 changed files with 105 additions and 30 deletions

View File

@@ -2,7 +2,8 @@ import React from 'react';
import moment from 'moment';
import intl from 'react-intl-universal';
import { first, isEqual } from 'lodash';
import { Intent } from '@blueprintjs/core';
import { AppToaster } from 'components';
import { useFormikContext } from 'formik';
import { useQuickPaymentMadeContext } from './QuickPaymentMadeFormProvider';
@@ -31,6 +32,12 @@ export const transformErrors = (errors, { setFieldError }) => {
intl.get('the_payment_amount_bigger_than_invoice_due_amount'),
);
}
if (getError('WITHDRAWAL_ACCOUNT_CURRENCY_INVALID')) {
AppToaster.show({
message: intl.get('payment_made.error.withdrawal_account_currency_invalid'),
intent: Intent.DANGER,
});
}
};
export const useSetPrimaryBranchToForm = () => {

View File

@@ -2,6 +2,8 @@ import React from 'react';
import moment from 'moment';
import intl from 'react-intl-universal';
import { first } from 'lodash';
import { Intent } from '@blueprintjs/core';
import { AppToaster } from 'components';
import { useFormikContext } from 'formik';
import { useQuickPaymentReceiveContext } from './QuickPaymentReceiveFormProvider';
@@ -39,6 +41,12 @@ export const transformErrors = (errors, { setFieldError }) => {
intl.get('the_payment_amount_bigger_than_invoice_due_amount'),
);
}
if (getError('PAYMENT_ACCOUNT_CURRENCY_INVALID')) {
AppToaster.show({
message: intl.get('payment_Receive.error.payment_account_currency_invalid'),
intent: Intent.DANGER,
});
}
};
export const useSetPrimaryBranchToForm = () => {

View File

@@ -27,7 +27,7 @@ import { usePaymentMadeFormContext } from './PaymentMadeFormProvider';
import {
defaultPaymentMade,
transformToEditForm,
ERRORS,
transformErrors,
transformFormToRequest,
} from './utils';
@@ -111,14 +111,10 @@ function PaymentMadeForm({
data: { errors },
},
}) => {
const getError = (errorType) => errors.find((e) => e.type === errorType);
if (getError(ERRORS.PAYMENT_NUMBER_NOT_UNIQUE)) {
setFieldError(
'payment_number',
intl.get('payment_number_is_not_unique'),
);
if (errors) {
transformErrors(errors, { setFieldError });
}
setSubmitting(false);
};

View File

@@ -1,5 +1,7 @@
import React, { createContext, useContext } from 'react';
import { isEqual, isUndefined } from 'lodash';
import { Features } from 'common';
import { useFeatureCan } from 'hooks/state';
import {
useAccounts,
useVendors,
@@ -18,11 +20,21 @@ const PaymentMadeFormContext = createContext();
/**
* Payment made form provider.
*/
function PaymentMadeFormProvider({ paymentMadeId, baseCurrency, ...props }) {
function PaymentMadeFormProvider({
query,
paymentMadeId,
baseCurrency,
...props
}) {
const [submitPayload, setSubmitPayload] = React.useState({});
const [paymentVendorId, setPaymentVendorId] = React.useState(null);
const [selectVendor, setSelectVendor] = React.useState(null);
// Features guard.
const { featureCan } = useFeatureCan();
const isBranchFeatureCan = featureCan(Features.Branches);
// Handle fetch accounts data.
const { data: accounts, isLoading: isAccountsLoading } = useAccounts();
@@ -53,7 +65,8 @@ function PaymentMadeFormProvider({ paymentMadeId, baseCurrency, ...props }) {
data: branches,
isLoading: isBranchesLoading,
isSuccess: isBranchesSuccess,
} = useBranches();
} = useBranches(query, { enabled: isBranchFeatureCan });
// Fetch payment made settings.
useSettings();

View File

@@ -1,7 +1,10 @@
import React from 'react';
import moment from 'moment';
import intl from 'react-intl-universal';
import { pick, first } from 'lodash';
import { useFormikContext } from 'formik';
import { Intent } from '@blueprintjs/core';
import { AppToaster } from 'components';
import { usePaymentMadeFormContext } from './PaymentMadeFormProvider';
import {
defaultFastFieldShouldUpdate,
@@ -111,3 +114,20 @@ export const useSetPrimaryBranchToForm = () => {
}
}, [isBranchesSuccess, setFieldValue, branches]);
};
/**
* Transformes the response errors types.
*/
export const transformErrors = (errors, { setFieldError }) => {
const getError = (errorType) => errors.find((e) => e.type === errorType);
if (getError('PAYMENT_NUMBER_NOT_UNIQUE')) {
setFieldError('payment_number', intl.get('payment_number_is_not_unique'));
}
if (getError('WITHDRAWAL_ACCOUNT_CURRENCY_INVALID')) {
AppToaster.show({
message: intl.get('payment_made.error.withdrawal_account_currency_invalid'),
intent: Intent.DANGER,
});
}
};

View File

@@ -33,6 +33,7 @@ import {
defaultPaymentReceive,
transformToEditForm,
transformFormToRequest,
transformErrors,
} from './utils';
/**
@@ -135,19 +136,8 @@ function PaymentReceiveForm({
data: { errors },
},
}) => {
const getError = (errorType) => errors.find((e) => e.type === errorType);
if (getError('PAYMENT_RECEIVE_NO_EXISTS')) {
setFieldError(
'payment_receive_no',
intl.get('payment_number_is_not_unique'),
);
}
if (getError('PAYMENT_RECEIVE_NO_REQUIRED')) {
setFieldError(
'payment_receive_no',
intl.get('payment_receive.field.error.payment_receive_no_required'),
);
if (errors) {
transformErrors(errors, { setFieldError });
}
setSubmitting(false);
};

View File

@@ -1,6 +1,7 @@
import React, { createContext, useContext } from 'react';
import { isEmpty, pick, isEqual, isUndefined } from 'lodash';
import {isEqual, isUndefined } from 'lodash';
import { Features } from 'common';
import { useFeatureCan } from 'hooks/state';
import { DashboardInsider } from 'components';
import {
useSettingsPaymentReceives,
@@ -19,6 +20,7 @@ const PaymentReceiveFormContext = createContext();
* Payment receive form provider.
*/
function PaymentReceiveFormProvider({
query,
paymentReceiveId,
baseCurrency,
...props
@@ -28,6 +30,10 @@ function PaymentReceiveFormProvider({
const [selectCustomer, setSelectCustomer] = React.useState(null);
// Features guard.
const { featureCan } = useFeatureCan();
const isBranchFeatureCan = featureCan(Features.Branches);
// Fetches payment recevie details.
const {
data: {
@@ -56,7 +62,7 @@ function PaymentReceiveFormProvider({
data: branches,
isLoading: isBranchesLoading,
isSuccess: isBranchesSuccess,
} = useBranches();
} = useBranches(query, { enabled: isBranchFeatureCan });
// Detarmines whether the new mode.
const isNewMode = !paymentReceiveId;

View File

@@ -1,7 +1,10 @@
import React from 'react';
import { useFormikContext } from 'formik';
import moment from 'moment';
import intl from 'react-intl-universal';
import { omit, pick, first } from 'lodash';
import { useFormikContext } from 'formik';
import { Intent } from '@blueprintjs/core';
import { AppToaster } from 'components';
import { usePaymentReceiveFormContext } from './PaymentReceiveFormProvider';
import {
defaultFastFieldShouldUpdate,
@@ -182,3 +185,31 @@ export const useSetPrimaryBranchToForm = () => {
}
}, [isBranchesSuccess, setFieldValue, branches]);
};
/**
* Transformes the response errors types.
*/
export const transformErrors = (errors, { setFieldError }) => {
const getError = (errorType) => errors.find((e) => e.type === errorType);
if (getError('PAYMENT_RECEIVE_NO_EXISTS')) {
setFieldError(
'payment_receive_no',
intl.get('payment_number_is_not_unique'),
);
}
if (getError('PAYMENT_RECEIVE_NO_REQUIRED')) {
setFieldError(
'payment_receive_no',
intl.get('payment_receive.field.error.payment_receive_no_required'),
);
}
if (getError('PAYMENT_ACCOUNT_CURRENCY_INVALID')) {
AppToaster.show({
message: intl.get(
'payment_Receive.error.payment_account_currency_invalid',
),
intent: Intent.DANGER,
});
}
};

View File

@@ -1914,5 +1914,7 @@
"warehouse.error.warehouse_code_not_unique": "رمز المخزن ليس فريدًا ",
"warehouse.error.warehouse_has_associated_transactions": "لا يمكنك حذف هذا المخزن لأنه لديه معاملات مرتبطة به. ",
"branche.error.warehouse_code_not_unique": "رمز الفرع ليس فريدًا ",
"branche.error.branch_has_associated_transactions": "لا يمكنك حذف هذا الفرع لأنه لديه معاملات مرتبطة به."
"branche.error.branch_has_associated_transactions": "لا يمكنك حذف هذا الفرع لأنه لديه معاملات مرتبطة به.",
"payment_Receive.error.payment_account_currency_invalid":"يجب أن تكون عملة حساب الإيداع هي نفس عملة العميل أو العملة الأساسية للمنشأة.",
"payment_made.error.withdrawal_account_currency_invalid":"يجب أن تكون عملة حساب السحب هي نفس عملة المورد أو العملة الأساسية للمنشأة."
}

View File

@@ -1909,5 +1909,7 @@
"warehouse.error.warehouse_code_not_unique": "Warehouse code not unique",
"warehouse.error.warehouse_has_associated_transactions": "You could not delete the warehouse that has associated transactions.",
"branche.error.warehouse_code_not_unique": "Branch code not unique",
"branche.error.branch_has_associated_transactions": "You could not delete the branch that has associated transactions."
"branche.error.branch_has_associated_transactions": "You could not delete the branch that has associated transactions.",
"payment_Receive.error.payment_account_currency_invalid":"The deposit account currency should be same customer currency or organization base currency.",
"payment_made.error.withdrawal_account_currency_invalid":"The withdrawal account currency should be same vendor currency or organization base currency."
}