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