Optimized performance and fix type of object not correctly

This commit is contained in:
2023-11-01 21:53:37 +07:00
parent 8491755cc3
commit 4e7e731fbd
10 changed files with 60 additions and 52 deletions
+3 -2
View File
@@ -1,3 +1,4 @@
import { memo } from 'react';
import { useSearchParams } from 'react-router-dom'; import { useSearchParams } from 'react-router-dom';
import styled, { css } from 'styled-components'; import styled, { css } from 'styled-components';
@@ -41,7 +42,7 @@ interface IOrderProps {
}[]; }[];
} }
const OrderBy = ({ options }: IOrderProps) => { const OrderBy = memo(({ options }: IOrderProps) => {
const field = 'orderBy'; const field = 'orderBy';
const [searchParams, setSearchParams] = useSearchParams(); const [searchParams, setSearchParams] = useSearchParams();
@@ -67,6 +68,6 @@ const OrderBy = ({ options }: IOrderProps) => {
))} ))}
</StyledOrder> </StyledOrder>
); );
}; });
export default OrderBy; export default OrderBy;
+4 -4
View File
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react'; import { memo, useEffect, useState } from 'react';
import styled from 'styled-components'; import styled from 'styled-components';
const StyledSearch = styled.input` const StyledSearch = styled.input`
@@ -10,10 +10,10 @@ const StyledSearch = styled.input`
interface ISearch { interface ISearch {
setPlaceHolder: string; setPlaceHolder: string;
setValueSearch: React.Dispatch<React.SetStateAction<string>>; setValueSearch: (phone: string) => void;
} }
const Search = ({ setValueSearch, setPlaceHolder }: ISearch) => { const Search = memo(({ setValueSearch, setPlaceHolder }: ISearch) => {
const [query, setQuery] = useState(''); const [query, setQuery] = useState('');
useEffect(() => { useEffect(() => {
@@ -30,6 +30,6 @@ const Search = ({ setValueSearch, setPlaceHolder }: ISearch) => {
placeholder={setPlaceHolder} placeholder={setPlaceHolder}
/> />
); );
}; });
export default Search; export default Search;
+3 -2
View File
@@ -2,6 +2,7 @@ import { useSearchParams } from 'react-router-dom';
// Components // Components
import Select from './Select'; import Select from './Select';
import { memo } from 'react';
interface ISortByProps { interface ISortByProps {
options: { options: {
@@ -10,7 +11,7 @@ interface ISortByProps {
}[]; }[];
} }
const SortBy = ({ options }: ISortByProps) => { const SortBy = memo(({ options }: ISortByProps) => {
const [searchParams, setSearchParams] = useSearchParams(); const [searchParams, setSearchParams] = useSearchParams();
const sortBy = searchParams.get('sortBy') || ''; const sortBy = searchParams.get('sortBy') || '';
@@ -27,6 +28,6 @@ const SortBy = ({ options }: ISortByProps) => {
onChange={handleChange} onChange={handleChange}
/> />
); );
}; });
export default SortBy; export default SortBy;
+6 -6
View File
@@ -1,18 +1,18 @@
type TUser = { type TUser = {
id: string; id: number;
name: string; name: string;
identifiedCode: string; identifiedCode: string;
address: string; address: string;
phone: string; phone: string;
roomId: string; roomId: number;
}; };
type TRoom = { type TRoom = {
id: string; id: number;
name: string; name: string;
amount: string; amount: number;
price: string; price: number;
discount: string; discount: number;
description: string; description: string;
status: boolean; status: boolean;
}; };
+9 -6
View File
@@ -86,12 +86,15 @@ const getValueFromObj = <T>(obj: T | null = null): TKeyString => {
if (obj) { if (obj) {
for (const key of Object.keys(obj)) { for (const key of Object.keys(obj)) {
const tempValue = obj[key as keyof typeof obj]; const tempValue = obj[key as keyof typeof obj];
const value: string | boolean | number = let value: string | boolean = '';
typeof tempValue === 'boolean' ||
typeof tempValue === 'string' || if (typeof tempValue === 'string' || typeof tempValue === 'boolean') {
typeof tempValue === 'number' value = tempValue;
? tempValue }
: '';
if (typeof tempValue === 'number') {
value = tempValue.toString();
}
result = { ...result, [`${key}Value`]: value }; result = { ...result, [`${key}Value`]: value };
} }
+1 -16
View File
@@ -1,12 +1,3 @@
/**
* Check value is valid name or not
* @param value Values need to be checked
* @returns A boolean indicating whether or not the argument has valid
*/
const isValidName = (value: string): boolean => {
return /^[a-zA-Z]{2,}(?: [a-zA-Z]+){0,2}$/gm.test(value);
};
/** /**
* Check value is valid number or not * Check value is valid number or not
* @param value Value need to be checked * @param value Value need to be checked
@@ -40,10 +31,4 @@ const isValidString = (value: string): boolean => {
*/ */
const skipCheck = () => true; const skipCheck = () => true;
export { export { isValidNumber, isValidPhoneNumber, isValidString, skipCheck };
isValidName,
isValidNumber,
isValidPhoneNumber,
isValidString,
skipCheck,
};
+14 -5
View File
@@ -75,7 +75,7 @@ const RoomForm = ({
const initialValue: string = isAdd const initialValue: string = isAdd
? '' ? ''
: room! : room!
&& room.id; && room.id.toString();
const { const {
idValue, idValue,
@@ -150,12 +150,23 @@ const RoomForm = ({
// Submit form // Submit form
const onSubmitForm = async (state: TKeyValue) => { const onSubmitForm = async (state: TKeyValue) => {
// Convert to room type
const data: TRoom = {
id: +state.id!,
name: '' + state.name,
amount: +state.amount!,
discount: +state.discount!,
price: +state.price!,
status: !!state.status,
description: '' + state.description,
};
try { try {
if (isAdd) { if (isAdd) {
// Add request // Add request
const response = await sendRequest( const response = await sendRequest(
ROOM_PATH, ROOM_PATH,
JSON.stringify(state), JSON.stringify(data),
'POST', 'POST',
); );
@@ -170,7 +181,7 @@ const RoomForm = ({
// Edit request // Edit request
const response = await sendRequest( const response = await sendRequest(
ROOM_PATH + `/${room!.id}`, ROOM_PATH + `/${room!.id}`,
JSON.stringify(state), JSON.stringify(data),
'PUT', 'PUT',
); );
@@ -197,8 +208,6 @@ const RoomForm = ({
onResetForm(); onResetForm();
}; };
console.log(initialValue);
// prettier-ignore // prettier-ignore
const { const {
values, values,
+4 -4
View File
@@ -71,8 +71,8 @@ const RoomRow = ({
// prettier-ignore // prettier-ignore
const statusText = status const statusText = status
? 'Valid' ? 'Invalid'
: 'Invalid'; : 'Valid';
return ( return (
<Table.Row> <Table.Row>
@@ -84,9 +84,9 @@ const RoomRow = ({
<div>{statusText}</div> <div>{statusText}</div>
<Menus.Menu> <Menus.Menu>
<Menus.Toggle id={id} /> <Menus.Toggle id={id.toString()} />
<Menus.List id={id}> <Menus.List id={id.toString()}>
<Menus.Button <Menus.Button
icon={<HiSquare2Stack />} icon={<HiSquare2Stack />}
onClick={() => handleOnEdit(room)} onClick={() => handleOnEdit(room)}
+14 -5
View File
@@ -27,7 +27,6 @@ import {
isValidString, isValidString,
isValidNumber, isValidNumber,
isValidPhoneNumber, isValidPhoneNumber,
isValidName,
} from '../../helpers/validators'; } from '../../helpers/validators';
import { addValidator, getValueFromObj } from '../../helpers/utils.ts'; import { addValidator, getValueFromObj } from '../../helpers/utils.ts';
import { sendRequest } from '../../helpers/sendRequest.ts'; import { sendRequest } from '../../helpers/sendRequest.ts';
@@ -76,7 +75,7 @@ const UserForm = ({
const initialValue: string = isAdd const initialValue: string = isAdd
? '' ? ''
: user! : user!
&& user.id; && user.id.toLocaleString();
const { const {
idValue, idValue,
@@ -116,7 +115,7 @@ const UserForm = ({
// prettier-ignore // prettier-ignore
const stateValidatorSchema: TValidator = { const stateValidatorSchema: TValidator = {
name: addValidator({ name: addValidator({
validatorFunc: isValidName, validatorFunc: isValidString,
prop: 'full name' prop: 'full name'
}), }),
identifiedCode: addValidator({ identifiedCode: addValidator({
@@ -139,12 +138,22 @@ const UserForm = ({
// Submit form // Submit form
const onSubmitForm = async (state: TKeyValue) => { const onSubmitForm = async (state: TKeyValue) => {
// Convert to user type
const data: TUser = {
id: +state.id!,
name: '' + state.name,
identifiedCode: '' + state.identifiedCode,
phone: '' + state.phone,
roomId: +state.roomId!,
address: '' + state.address,
};
try { try {
if (isAdd) { if (isAdd) {
// Add request // Add request
const response = await sendRequest( const response = await sendRequest(
USER_PATH, USER_PATH,
JSON.stringify(state), JSON.stringify(data),
'POST', 'POST',
); );
@@ -159,7 +168,7 @@ const UserForm = ({
// Edit request // Edit request
const response = await sendRequest( const response = await sendRequest(
USER_PATH + `/${user!.id}`, USER_PATH + `/${user!.id}`,
JSON.stringify(state), JSON.stringify(data),
'PUT', 'PUT',
); );
+2 -2
View File
@@ -78,9 +78,9 @@ const UserRow = ({
<div>{roomId}</div> <div>{roomId}</div>
<Menus.Menu> <Menus.Menu>
<Menus.Toggle id={id} /> <Menus.Toggle id={id.toString()} />
<Menus.List id={id}> <Menus.List id={id.toString()}>
<Menus.Button <Menus.Button
icon={<HiSquare2Stack />} icon={<HiSquare2Stack />}
onClick={() => handleOnEdit(user)} onClick={() => handleOnEdit(user)}