diff --git a/hotel-management/src/constants/responseStatus.ts b/hotel-management/src/constants/responseStatus.ts index d279258..a9b1210 100644 --- a/hotel-management/src/constants/responseStatus.ts +++ b/hotel-management/src/constants/responseStatus.ts @@ -2,7 +2,6 @@ const STATUS_CODE = { OK: 200, CREATE: 201, NOT_FOUND: 404, - INTERNAL_SERVER_ERROR: 500, }; const RESPONSE_MESSAGE = { diff --git a/hotel-management/src/pages/Room/Form.tsx b/hotel-management/src/pages/Room/Form.tsx index 8bb6ff0..13dd071 100644 --- a/hotel-management/src/pages/Room/Form.tsx +++ b/hotel-management/src/pages/Room/Form.tsx @@ -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 sendRequest( - ROOM_PATH, - 'POST', - JSON.stringify(room) - ); - - if (response.statusCode === STATUS_CODE.CREATE) { + const response = await addRoom(room); + + 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 diff --git a/hotel-management/src/pages/User/Form.tsx b/hotel-management/src/pages/User/Form.tsx index 37a6d4e..aefd45d 100644 --- a/hotel-management/src/pages/User/Form.tsx +++ b/hotel-management/src/pages/User/Form.tsx @@ -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 diff --git a/hotel-management/src/services/roomServices.ts b/hotel-management/src/services/roomServices.ts index 5500165..4571fb1 100644 --- a/hotel-management/src/services/roomServices.ts +++ b/hotel-management/src/services/roomServices.ts @@ -44,13 +44,13 @@ const getRoom = async (roomId: number): Promise> => { try { const response = await sendRequest(ROOM_PATH + '/' + roomId); - if (response.statusCode === STATUS_CODE.OK) { - const rooms = response.data!; - - return rooms; - } else { + if (response.statusCode !== STATUS_CODE.OK) { throw new Error(errorMsg(response.statusCode, response.msg)); } + + const rooms = response.data!; + + return rooms; } catch (error) { if (error instanceof Error) { toast.error(error.message); @@ -73,10 +73,6 @@ const updateRoom = async (room: TRoom): Promise>> => { 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,44 +95,77 @@ const updateRoomStatus = async ( status: boolean, roomIdNew?: number ): Promise>> => { - if (!roomIdNew) { - const response = await sendRequest( - ROOM_PATH + '/' + roomId, + try { + if (!roomIdNew) { + const response = await sendRequest( + ROOM_PATH + '/' + roomId, + 'PATCH', + JSON.stringify({ status: status }) + ); + + return response; + } + + // Update new room status + const resNewRoom = await sendRequest( + ROOM_PATH + '/' + roomIdNew, 'PATCH', JSON.stringify({ status: status }) ); - return response; - } - - // Update new room status - const resNewRoom = await sendRequest( - ROOM_PATH + '/' + roomIdNew, - 'PATCH', - JSON.stringify({ status: status }) - ); + // Update old room status; + const resOldRoom = await sendRequest( + ROOM_PATH + '/' + roomId, + 'PATCH', + JSON.stringify({ status: !status }) + ); - // Update old room status; - const resOldRoom = await sendRequest( - 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, - }; + 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); + } } - 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>> => { + try { + // Set default status room + room.status = false; + + const response = await sendRequest( + 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 }; diff --git a/hotel-management/src/services/userServices.ts b/hotel-management/src/services/userServices.ts index 7bd9027..9edbaf6 100644 --- a/hotel-management/src/services/userServices.ts +++ b/hotel-management/src/services/userServices.ts @@ -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>> => { + try { + const response = await sendRequest( + 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>> => { try { const response = await sendRequest( @@ -33,6 +65,11 @@ const updateUser = async (user: TUser): Promise>> => { 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>> => { const tempUser = user; @@ -46,4 +83,4 @@ const checkOutUser = async (user: TUser): Promise>> => return null; }; -export { updateUser, checkOutUser }; +export { updateUser, checkOutUser, createUser };