Add comments and optimized code

This commit is contained in:
2023-10-30 09:34:14 +07:00
parent 367b24f4fa
commit 04469af993
6 changed files with 22 additions and 12 deletions
@@ -1,3 +1,4 @@
// Types
import { TUser } from './types'; import { TUser } from './types';
interface IMenusContext { interface IMenusContext {
+3
View File
@@ -1,4 +1,7 @@
// Constants
import { invalidFormatMsg } from '../constants/messages'; import { invalidFormatMsg } from '../constants/messages';
// Types
import { TKeyValue, TPropValues, TStateSchema, TUser } from '../globals/types'; import { TKeyValue, TPropValues, TStateSchema, TUser } from '../globals/types';
const VALUE = 'value'; const VALUE = 'value';
+3 -1
View File
@@ -37,6 +37,8 @@ const useForm = (
setInitialErrorState(initialValue); setInitialErrorState(initialValue);
setDisable(true); setDisable(true);
// If initial value true, setValues again from stateSchema
// and enabled button
if (initialValue) { if (initialValue) {
setValues({}); setValues({});
setValues(getPropValues(stateSchema, VALUE)); setValues(getPropValues(stateSchema, VALUE));
@@ -83,7 +85,7 @@ const useForm = (
Object.keys(errors).map((name) => Object.keys(errors).map((name) =>
setErrors((prevState) => ({ setErrors((prevState) => ({
...prevState, ...prevState,
[name]: !initialValue [name]: !initialValue // Skip error when initialValue have values
? validateFormFields(name, values[name] as string) ? validateFormFields(name, values[name] as string)
: '', : '',
})), })),
+1 -1
View File
@@ -150,7 +150,7 @@ const UserForm = ({
onClose(); onClose();
}; };
// Close form // Close and reset form
const closeAndReset = () => { const closeAndReset = () => {
onClose(); onClose();
onResetForm(); onResetForm();
+6 -4
View File
@@ -1,4 +1,5 @@
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import toast from 'react-hot-toast';
// Components // Components
import { HiSquare2Stack } from 'react-icons/hi2'; import { HiSquare2Stack } from 'react-icons/hi2';
@@ -14,14 +15,15 @@ import { TUser } from '../../globals/types';
// Constants // Constants
import { useFetch } from '../../hooks/useFetch'; import { useFetch } from '../../hooks/useFetch';
import { USER_PATH } from '../../constants/path';
import { STATUS_CODE } from '../../constants/statusCode';
import { CONFIRM_DELETE, DELETE_SUCCESS } from '../../constants/messages';
// Styled // Styled
import Spinner from '../../commons/styles/Spinner'; import Spinner from '../../commons/styles/Spinner';
// Utils
import { sendRequest } from '../../helpers/sendRequest'; import { sendRequest } from '../../helpers/sendRequest';
import { USER_PATH } from '../../constants/path';
import toast from 'react-hot-toast';
import { STATUS_CODE } from '../../constants/statusCode';
import { CONFIRM_DELETE, DELETE_SUCCESS } from '../../constants/messages';
interface IUserRow { interface IUserRow {
user: TUser; user: TUser;
+8 -6
View File
@@ -8,6 +8,8 @@ import UserTable from './Table';
// Styled // Styled
import { StyledUser, Title } from './styled'; import { StyledUser, Title } from './styled';
import UserDialog from './Dialog'; import UserDialog from './Dialog';
// Types
import { TUser } from '../../globals/types'; import { TUser } from '../../globals/types';
const User = () => { const User = () => {
@@ -16,12 +18,12 @@ const User = () => {
const [user, setUser] = useState<TUser | null>(null); const [user, setUser] = useState<TUser | null>(null);
const [isAdd, setIsAdd] = useState(false); const [isAdd, setIsAdd] = useState(false);
const openDialog = (isAdd: boolean) => { const openFormDialog = (isAddForm: boolean = false) => {
setIsAdd(isAdd); setIsAdd(isAddForm);
dialogRef.current?.showModal(); dialogRef.current?.showModal();
}; };
const closeDialog = () => { const closeFormDialog = () => {
dialogRef.current?.close(); dialogRef.current?.close();
}; };
@@ -30,19 +32,19 @@ const User = () => {
<StyledUser> <StyledUser>
<Direction type="horizontal"> <Direction type="horizontal">
<Title>List User</Title> <Title>List User</Title>
<Button onClick={() => openDialog(true)}>Add user</Button> <Button onClick={() => openFormDialog(true)}>Add user</Button>
</Direction> </Direction>
<UserTable <UserTable
reload={reload} reload={reload}
setReload={setReload} setReload={setReload}
openFormDialog={() => openDialog(false)} openFormDialog={() => openFormDialog()}
setUser={setUser} setUser={setUser}
/> />
</StyledUser> </StyledUser>
<UserDialog <UserDialog
onClose={closeDialog} onClose={closeFormDialog}
ref={dialogRef} ref={dialogRef}
setReload={setReload} setReload={setReload}
reload={reload} reload={reload}