mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-22 20:01:59 +00:00
Hot fix and optimized code
This commit is contained in:
@@ -2,7 +2,6 @@ const STATUS_CODE = {
|
|||||||
OK: 200,
|
OK: 200,
|
||||||
CREATE: 201,
|
CREATE: 201,
|
||||||
NOT_FOUND: 404,
|
NOT_FOUND: 404,
|
||||||
INTERNAL_SERVER_ERROR: 500,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const RESPONSE_MESSAGE = {
|
const RESPONSE_MESSAGE = {
|
||||||
|
|||||||
@@ -13,12 +13,10 @@ import Input from '../../commons/styles/Input.ts';
|
|||||||
import { Nullable, TRoom } from '../../globals/types.ts';
|
import { Nullable, TRoom } from '../../globals/types.ts';
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
import { ROOM_PATH } from '../../constants/path.ts';
|
|
||||||
import { STATUS_CODE } from '../../constants/responseStatus.ts';
|
import { STATUS_CODE } from '../../constants/responseStatus.ts';
|
||||||
import {
|
import {
|
||||||
ADD_SUCCESS,
|
ADD_SUCCESS,
|
||||||
EDIT_SUCCESS,
|
EDIT_SUCCESS,
|
||||||
errorMsg,
|
|
||||||
} from '../../constants/messages.ts';
|
} from '../../constants/messages.ts';
|
||||||
import {
|
import {
|
||||||
INVALID_DISCOUNT,
|
INVALID_DISCOUNT,
|
||||||
@@ -27,7 +25,6 @@ import {
|
|||||||
} from '../../constants/formValidateMessage.ts';
|
} from '../../constants/formValidateMessage.ts';
|
||||||
|
|
||||||
// Helpers
|
// Helpers
|
||||||
import { sendRequest } from '../../helpers/sendRequest.ts';
|
|
||||||
import {
|
import {
|
||||||
isValidDiscount,
|
isValidDiscount,
|
||||||
isValidNumber,
|
isValidNumber,
|
||||||
@@ -37,6 +34,7 @@ import {
|
|||||||
// Components
|
// Components
|
||||||
import FormRow from '../../components/LabelControl/index.tsx';
|
import FormRow from '../../components/LabelControl/index.tsx';
|
||||||
import Form from '../../components/Form/index.tsx';
|
import Form from '../../components/Form/index.tsx';
|
||||||
|
import { addRoom, updateRoom } from '../../services/roomServices.ts';
|
||||||
|
|
||||||
const FormBtn = styled(Button)`
|
const FormBtn = styled(Button)`
|
||||||
width: 100%;
|
width: 100%;
|
||||||
@@ -86,30 +84,17 @@ const RoomForm = ({
|
|||||||
try {
|
try {
|
||||||
if (isAdd) {
|
if (isAdd) {
|
||||||
// Add request
|
// Add request
|
||||||
|
const response = await addRoom(room);
|
||||||
|
|
||||||
const response = await sendRequest(
|
if(response?.statusCode == STATUS_CODE.CREATE) {
|
||||||
ROOM_PATH,
|
|
||||||
'POST',
|
|
||||||
JSON.stringify(room)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.statusCode === STATUS_CODE.CREATE) {
|
|
||||||
toast.success(ADD_SUCCESS);
|
toast.success(ADD_SUCCESS);
|
||||||
} else {
|
|
||||||
throw new Error(errorMsg(response.statusCode, response.msg));
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Edit request
|
// Edit request
|
||||||
const response = await sendRequest(
|
const response = await updateRoom(room);
|
||||||
ROOM_PATH + `/${room!.id}`,
|
|
||||||
'PUT',
|
|
||||||
JSON.stringify(room)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.statusCode == STATUS_CODE.OK) {
|
if (response?.statusCode == STATUS_CODE.OK) {
|
||||||
toast.success(EDIT_SUCCESS);
|
toast.success(EDIT_SUCCESS);
|
||||||
} else {
|
|
||||||
throw new Error(errorMsg(response.statusCode, response.msg));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Reload table data
|
// Reload table data
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import FormRow from '../../components/LabelControl/index.tsx';
|
|||||||
import Select, { ISelectOptions } from '../../components/Select';
|
import Select, { ISelectOptions } from '../../components/Select';
|
||||||
|
|
||||||
// Helpers
|
// Helpers
|
||||||
import { sendRequest } from '../../helpers/sendRequest.ts';
|
|
||||||
import {
|
import {
|
||||||
isEmptyObj,
|
isEmptyObj,
|
||||||
isValidName,
|
isValidName,
|
||||||
@@ -32,9 +31,7 @@ import { STATUS_CODE } from '../../constants/responseStatus.ts';
|
|||||||
import {
|
import {
|
||||||
ADD_SUCCESS,
|
ADD_SUCCESS,
|
||||||
EDIT_SUCCESS,
|
EDIT_SUCCESS,
|
||||||
errorMsg,
|
|
||||||
} from '../../constants/messages.ts';
|
} from '../../constants/messages.ts';
|
||||||
import { USER_PATH } from '../../constants/path.ts';
|
|
||||||
import {
|
import {
|
||||||
INVALID_FIELD,
|
INVALID_FIELD,
|
||||||
INVALID_PHONE,
|
INVALID_PHONE,
|
||||||
@@ -50,6 +47,7 @@ import { getAllRoom, updateRoomStatus } from '../../services/roomServices.ts';
|
|||||||
|
|
||||||
// Types
|
// Types
|
||||||
import { Nullable, TRoom, TUser } from '../../globals/types.ts';
|
import { Nullable, TRoom, TUser } from '../../globals/types.ts';
|
||||||
|
import { createUser, updateUser } from '../../services/userServices.ts';
|
||||||
|
|
||||||
interface IUserFormProp {
|
interface IUserFormProp {
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
@@ -121,32 +119,20 @@ const UserForm = ({
|
|||||||
try {
|
try {
|
||||||
if (isAdd) {
|
if (isAdd) {
|
||||||
// Add request
|
// Add request
|
||||||
const response = await sendRequest(
|
const response = await createUser(newUser);
|
||||||
USER_PATH,
|
|
||||||
'POST',
|
|
||||||
JSON.stringify(newUser)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.statusCode === STATUS_CODE.CREATE) {
|
if (response?.statusCode === STATUS_CODE.CREATE) {
|
||||||
toast.success(ADD_SUCCESS);
|
toast.success(ADD_SUCCESS);
|
||||||
} else {
|
|
||||||
throw new Error(errorMsg(response.statusCode, response.msg));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update room status
|
// Update room status
|
||||||
updateRoomStatus(newUser.roomId, true);
|
updateRoomStatus(newUser.roomId, true);
|
||||||
} else {
|
} else {
|
||||||
// Edit request
|
// Edit request
|
||||||
const response = await sendRequest(
|
const response = await updateUser(newUser);
|
||||||
USER_PATH + `/${newUser.id}`,
|
|
||||||
'PUT',
|
|
||||||
JSON.stringify(newUser)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.statusCode == STATUS_CODE.OK) {
|
if (response?.statusCode == STATUS_CODE.OK) {
|
||||||
toast.success(EDIT_SUCCESS);
|
toast.success(EDIT_SUCCESS);
|
||||||
} else {
|
|
||||||
throw new Error(errorMsg(response.statusCode, response.msg));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update room status
|
// Update room status
|
||||||
|
|||||||
@@ -44,13 +44,13 @@ const getRoom = async (roomId: number): Promise<Nullable<TRoom>> => {
|
|||||||
try {
|
try {
|
||||||
const response = await sendRequest<TRoom>(ROOM_PATH + '/' + roomId);
|
const response = await sendRequest<TRoom>(ROOM_PATH + '/' + roomId);
|
||||||
|
|
||||||
if (response.statusCode === STATUS_CODE.OK) {
|
if (response.statusCode !== STATUS_CODE.OK) {
|
||||||
const rooms = response.data!;
|
|
||||||
|
|
||||||
return rooms;
|
|
||||||
} else {
|
|
||||||
throw new Error(errorMsg(response.statusCode, response.msg));
|
throw new Error(errorMsg(response.statusCode, response.msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const rooms = response.data!;
|
||||||
|
|
||||||
|
return rooms;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
toast.error(error.message);
|
toast.error(error.message);
|
||||||
@@ -73,10 +73,6 @@ const updateRoom = async (room: TRoom): Promise<Nullable<TResponse<TRoom>>> => {
|
|||||||
JSON.stringify(room)
|
JSON.stringify(room)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.statusCode !== STATUS_CODE.OK) {
|
|
||||||
throw new Error(errorMsg(response.statusCode, response.msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
@@ -99,44 +95,77 @@ const updateRoomStatus = async (
|
|||||||
status: boolean,
|
status: boolean,
|
||||||
roomIdNew?: number
|
roomIdNew?: number
|
||||||
): Promise<Nullable<TResponse<TRoom>>> => {
|
): Promise<Nullable<TResponse<TRoom>>> => {
|
||||||
if (!roomIdNew) {
|
try {
|
||||||
const response = await sendRequest<TRoom>(
|
if (!roomIdNew) {
|
||||||
ROOM_PATH + '/' + roomId,
|
const response = await sendRequest<TRoom>(
|
||||||
|
ROOM_PATH + '/' + roomId,
|
||||||
|
'PATCH',
|
||||||
|
JSON.stringify({ status: status })
|
||||||
|
);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update new room status
|
||||||
|
const resNewRoom = await sendRequest<TRoom>(
|
||||||
|
ROOM_PATH + '/' + roomIdNew,
|
||||||
'PATCH',
|
'PATCH',
|
||||||
JSON.stringify({ status: status })
|
JSON.stringify({ status: status })
|
||||||
);
|
);
|
||||||
|
|
||||||
return response;
|
// Update old room status;
|
||||||
|
const resOldRoom = await sendRequest<TRoom>(
|
||||||
|
ROOM_PATH + '/' + roomId,
|
||||||
|
'PATCH',
|
||||||
|
JSON.stringify({ status: !status })
|
||||||
|
);
|
||||||
|
|
||||||
|
if (
|
||||||
|
resNewRoom.statusCode === STATUS_CODE.OK &&
|
||||||
|
resOldRoom.statusCode === STATUS_CODE.OK
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
statusCode: STATUS_CODE.OK,
|
||||||
|
msg: RESPONSE_MESSAGE.UPDATE_SUCCESS,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
toast.error(error.message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update new room status
|
return null;
|
||||||
const resNewRoom = await sendRequest<TRoom>(
|
|
||||||
ROOM_PATH + '/' + roomIdNew,
|
|
||||||
'PATCH',
|
|
||||||
JSON.stringify({ status: status })
|
|
||||||
);
|
|
||||||
|
|
||||||
// Update old room status;
|
|
||||||
const resOldRoom = await sendRequest<TRoom>(
|
|
||||||
ROOM_PATH + '/' + roomId,
|
|
||||||
'PATCH',
|
|
||||||
JSON.stringify({ status: !status })
|
|
||||||
);
|
|
||||||
|
|
||||||
if (
|
|
||||||
resNewRoom.statusCode === STATUS_CODE.OK &&
|
|
||||||
resOldRoom.statusCode === STATUS_CODE.OK
|
|
||||||
) {
|
|
||||||
return {
|
|
||||||
statusCode: STATUS_CODE.OK,
|
|
||||||
msg: RESPONSE_MESSAGE.UPDATE_SUCCESS,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
statusCode: STATUS_CODE.INTERNAL_SERVER_ERROR,
|
|
||||||
msg: 'Something went wrong!',
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export { getRoom, updateRoom, updateRoomStatus, getAllRoom };
|
/**
|
||||||
|
* Add room to server
|
||||||
|
* @param room The room object need to be add
|
||||||
|
* @returns The response object if complete or null
|
||||||
|
*/
|
||||||
|
const addRoom = async (room: TRoom): Promise<Nullable<TResponse<TRoom>>> => {
|
||||||
|
try {
|
||||||
|
// Set default status room
|
||||||
|
room.status = false;
|
||||||
|
|
||||||
|
const response = await sendRequest<TRoom>(
|
||||||
|
ROOM_PATH,
|
||||||
|
'POST',
|
||||||
|
JSON.stringify(room)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.statusCode !== STATUS_CODE.OK) {
|
||||||
|
throw new Error(errorMsg(response.statusCode, response.msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
} catch (error: unknown) {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
toast.error(error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { getRoom, updateRoom, updateRoomStatus, getAllRoom, addRoom };
|
||||||
|
|||||||
@@ -11,6 +11,38 @@ import { Nullable, TResponse, TUser } from '../globals/types';
|
|||||||
// Helpers
|
// Helpers
|
||||||
import { sendRequest } from '../helpers/sendRequest';
|
import { sendRequest } from '../helpers/sendRequest';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create user to the server
|
||||||
|
* @param user The user object need to be created
|
||||||
|
* @returns The TResponse object if success or null
|
||||||
|
*/
|
||||||
|
const createUser = async (user: TUser): Promise<Nullable<TResponse<TUser>>> => {
|
||||||
|
try {
|
||||||
|
const response = await sendRequest<TUser>(
|
||||||
|
USER_PATH,
|
||||||
|
'POST',
|
||||||
|
JSON.stringify(user)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.statusCode !== STATUS_CODE.CREATE) {
|
||||||
|
throw new Error(errorMsg(response.statusCode, response.msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
} catch (error: unknown) {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
toast.error(error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the user to the server
|
||||||
|
* @param user The user object need to be updated
|
||||||
|
* @returns The TResponse object if update success or null
|
||||||
|
*/
|
||||||
const updateUser = async (user: TUser): Promise<Nullable<TResponse<TUser>>> => {
|
const updateUser = async (user: TUser): Promise<Nullable<TResponse<TUser>>> => {
|
||||||
try {
|
try {
|
||||||
const response = await sendRequest<TUser>(
|
const response = await sendRequest<TUser>(
|
||||||
@@ -33,6 +65,11 @@ const updateUser = async (user: TUser): Promise<Nullable<TResponse<TUser>>> => {
|
|||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checkout user
|
||||||
|
* @param user The user need to be checkout
|
||||||
|
* @returns The TResponse object if checkout success or not
|
||||||
|
*/
|
||||||
const checkOutUser = async (user: TUser): Promise<Nullable<TResponse<TUser>>> => {
|
const checkOutUser = async (user: TUser): Promise<Nullable<TResponse<TUser>>> => {
|
||||||
const tempUser = user;
|
const tempUser = user;
|
||||||
|
|
||||||
@@ -46,4 +83,4 @@ const checkOutUser = async (user: TUser): Promise<Nullable<TResponse<TUser>>> =>
|
|||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export { updateUser, checkOutUser };
|
export { updateUser, checkOutUser, createUser };
|
||||||
|
|||||||
Reference in New Issue
Block a user