Fix :data types variables.

This commit is contained in:
elforjani3
2020-11-21 20:51:13 +02:00
parent b9e61461ae
commit 4b1f562587
17 changed files with 309 additions and 179 deletions

View File

@@ -0,0 +1,21 @@
import * as Yup from 'yup';
import { formatMessage } from 'services/intl';
import { DATATYPES_LENGTH } from 'common/dataTypes';
const Schema = Yup.object().shape({
name: Yup.string()
.required()
.min(3)
.max(DATATYPES_LENGTH.STRING)
.label(formatMessage({ id: 'account_name_' })),
code: Yup.string().digits().min(3).max(6),
account_type_id: Yup.number()
.nullable()
.required()
.label(formatMessage({ id: 'account_type_id' })),
description: Yup.string().min(3).max(DATATYPES_LENGTH.TEXT).nullable().trim(),
parent_account_id: Yup.number().nullable(),
});
export const CreateAccountFormSchema = Schema;
export const EditAccountFormSchema = Schema;

View File

@@ -30,7 +30,10 @@ import withAccountsActions from 'containers/Accounts/withAccountsActions';
import withAccountDetail from 'containers/Accounts/withAccountDetail';
import withAccounts from 'containers/Accounts/withAccounts';
import withDialogActions from 'containers/Dialog/withDialogActions';
import {
EditAccountFormSchema,
CreateAccountFormSchema,
} from './AccountForm.schema';
import { compose } from 'utils';
/**
@@ -61,19 +64,12 @@ function AccountFormDialogContent({
accountTypeId,
}) {
const { formatMessage } = useIntl();
const validationSchema = Yup.object().shape({
name: Yup.string()
.required()
.min(3)
.max(255)
.label(formatMessage({ id: 'account_name_' })),
code: Yup.string().digits().min(3).max(6),
account_type_id: Yup.number().nullable()
.required()
.label(formatMessage({ id: 'account_type_id' })),
description: Yup.string().min(3).max(512).nullable().trim(),
parent_account_id: Yup.number().nullable(),
});
const isNewMode = !accountId;
const validationSchema = isNewMode
? CreateAccountFormSchema
: EditAccountFormSchema;
const initialValues = useMemo(
() => ({
account_type_id: '',
@@ -194,18 +190,14 @@ function AccountFormDialogContent({
}, [closeDialog, dialogName]);
// Fetches accounts list.
const fetchAccountsList = useQuery(
'accounts-list',
() => requestFetchAccounts(),
const fetchAccountsList = useQuery('accounts-list', () =>
requestFetchAccounts(),
);
// Fetches accounts types.
const fetchAccountsTypes = useQuery(
'accounts-types-list',
async () => {
await requestFetchAccountTypes();
},
);
const fetchAccountsTypes = useQuery('accounts-types-list', async () => {
await requestFetchAccountTypes();
});
// Fetch the given account id on edit mode.
const fetchAccount = useQuery(

View File

@@ -1,11 +1,11 @@
import React, { useMemo, useCallback } from 'react';
import { Intent } from '@blueprintjs/core';
import * as Yup from 'yup';
import { useQuery, queryCache } from 'react-query';
import { FormattedMessage as T, useIntl } from 'react-intl';
import { Formik } from 'formik';
import { AppToaster, DialogContent } from 'components';
import ItemCategoryForm from './ItemCategoryForm';
import withItemCategories from 'containers/Items/withItemCategories';
import withItemCategoryDetail from 'containers/Items/withItemCategoryDetail';
import withItemCategoriesActions from 'containers/Items/withItemCategoriesActions';
@@ -14,8 +14,11 @@ import withAccounts from 'containers/Accounts/withAccounts';
import withAccountsActions from 'containers/Accounts/withAccountsActions';
import withDialogActions from 'containers/Dialog/withDialogActions';
import {
EditItemCategoryFormSchema,
CreateItemCategoryFormSchema,
} from './itemCategoryForm.schema';
import { compose, transformToForm } from 'utils';
import ItemCategoryForm from './ItemCategoryForm';
const defaultInitialValues = {
name: '',
@@ -70,17 +73,6 @@ function ItemCategoryFormDialogContent({
requestFetchAccounts(),
);
const validationSchema = Yup.object().shape({
name: Yup.string()
.required()
.label(formatMessage({ id: 'category_name_' })),
parent_category_id: Yup.number().nullable(),
cost_account_id: Yup.number().nullable(),
sell_account_id: Yup.number().nullable(),
inventory_account_id: Yup.number().nullable(),
description: Yup.string().trim().nullable(),
});
const initialValues = useMemo(
() => ({
...defaultInitialValues,
@@ -131,7 +123,9 @@ function ItemCategoryFormDialogContent({
isLoading={fetchCategoriesList.isFetching || fetchAccountsList.isFetching}
>
<Formik
validationSchema={validationSchema}
validationSchema={
isNewMode ? CreateItemCategoryFormSchema : EditItemCategoryFormSchema
}
initialValues={initialValues}
onSubmit={handleFormSubmit}
>

View File

@@ -0,0 +1,18 @@
import * as Yup from 'yup';
import { formatMessage } from 'services/intl';
import { DATATYPES_LENGTH } from 'common/dataTypes';
const Schema = Yup.object().shape({
name: Yup.string()
.required()
.max(DATATYPES_LENGTH.STRING)
.label(formatMessage({ id: 'category_name_' })),
parent_category_id: Yup.number().nullable(),
cost_account_id: Yup.number().nullable(),
sell_account_id: Yup.number().nullable(),
inventory_account_id: Yup.number().nullable(),
description: Yup.string().trim().max(DATATYPES_LENGTH.TEXT).nullable(),
});
export const CreateItemCategoryFormSchema = Schema;
export const EditItemCategoryFormSchema = Schema;