Update name variable and optimzed code

This commit is contained in:
2023-10-27 14:15:47 +07:00
parent e95ac9c4ca
commit cce36788ac
2 changed files with 32 additions and 40 deletions
+12 -3
View File
@@ -28,10 +28,19 @@ const getPropValues = (stateSchema: TStateSchema, prop?: TPropValues) => {
}, {} as TKeyValue);
};
const addValidator = (func: (input: string) => boolean, error: string) => {
type test = {
validatorFunc: (value: string) => boolean;
prop: string;
required?: boolean;
};
const addValidator = ({ validatorFunc, prop, required = true }: test) => {
return {
func: func,
error: invalidFormatMessage(error),
required: required,
validator: {
func: validatorFunc,
error: invalidFormatMessage(prop),
},
};
};
+20 -37
View File
@@ -11,7 +11,7 @@ import FormRow from '../../components/FormRow';
import Button from '../../commons/styles/Button.ts';
// Types
import { TKeyValue, TStateSchema } from '../../globals/types';
import { TKeyValue, TStateSchema, TValidator } from '../../globals/types';
// Hooks
import useForm from '../../hooks/useForm';
@@ -50,42 +50,25 @@ const UserForm = ({ onClose }: IUserFormProp) => {
address: { value: '', error: '' },
};
const stateValidatorSchema = {
fullName: {
required: true,
validator: addValidator(
(input: string) => isValidString(input),
'full name',
),
},
identifiedCode: {
required: true,
validator: addValidator(
(input: string) => isValidNumber(input),
'identified code',
),
},
phone: {
required: true,
validator: addValidator(
(input: string) => isValidPhoneNumber(input),
'phone',
),
},
room: {
required: true,
validator: addValidator(
(input: string) => isValidNumber(input),
'room number',
),
},
address: {
required: true,
validator: addValidator(
(input: string) => isValidAddress(input),
'address',
),
},
// prettier-ignore
const stateValidatorSchema: TValidator = {
fullName: addValidator({
validatorFunc: isValidString,
prop: 'full name' }),
identifiedCode: addValidator({
validatorFunc: isValidNumber,
prop: 'identified code',
}),
phone: addValidator({
validatorFunc: isValidPhoneNumber,
prop: 'phone number',
}),
room: addValidator({
validatorFunc: isValidNumber,
prop: 'room number' }),
address: addValidator({
validatorFunc: isValidAddress,
prop: 'address' }),
};
// Submit form