mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-23 04:19:48 +00:00
Fix bug and optimzed code
This commit is contained in:
@@ -2,24 +2,24 @@ import toast from 'react-hot-toast';
|
||||
|
||||
// Types
|
||||
import { Nullable } from '../types/common';
|
||||
import { TRoom } from '../types/rooms';
|
||||
import { TResponse } from '../types/response';
|
||||
import { IResponse } from '../types/responses';
|
||||
import { IRoom } from '../types/rooms';
|
||||
|
||||
// Helpers
|
||||
import { sendRequest } from '../helpers/sendRequest';
|
||||
import { errorMsg } from '../helpers/helper';
|
||||
|
||||
// Constants
|
||||
import { STATUS_CODE, RESPONSE_MESSAGE } from '../constants/responseStatus';
|
||||
import { errorMsg } from '../constants/messages';
|
||||
import { ROOM_PATH } from '../constants/path';
|
||||
|
||||
/**
|
||||
* Get all rooms from server
|
||||
* @returns Return all rooms in server
|
||||
*/
|
||||
const getAllRoom = async (): Promise<Nullable<TRoom[]>> => {
|
||||
const getAllRoom = async (): Promise<Nullable<IRoom[]>> => {
|
||||
try {
|
||||
const response = await sendRequest<TRoom[]>(ROOM_PATH);
|
||||
const response = await sendRequest<IRoom[]>(ROOM_PATH);
|
||||
|
||||
if (response.statusCode === STATUS_CODE.OK) {
|
||||
const rooms = response.data!;
|
||||
@@ -42,17 +42,17 @@ const getAllRoom = async (): Promise<Nullable<TRoom[]>> => {
|
||||
* @param roomId The id room need to be get
|
||||
* @returns Return the room object depend on room id
|
||||
*/
|
||||
const getRoom = async (roomId: number): Promise<Nullable<TRoom>> => {
|
||||
const getRoom = async (roomId: number): Promise<Nullable<IRoom>> => {
|
||||
try {
|
||||
const response = await sendRequest<TRoom>(ROOM_PATH + '/' + roomId);
|
||||
const response = await sendRequest<IRoom>(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);
|
||||
@@ -67,18 +67,14 @@ const getRoom = async (roomId: number): Promise<Nullable<TRoom>> => {
|
||||
* @param room Room object need to be updated
|
||||
* @returns The response object
|
||||
*/
|
||||
const updateRoom = async (room: TRoom): Promise<Nullable<TResponse<TRoom>>> => {
|
||||
const updateRoom = async (room: IRoom): Promise<Nullable<IResponse<IRoom>>> => {
|
||||
try {
|
||||
const response = await sendRequest<TRoom>(
|
||||
const response = await sendRequest<IRoom>(
|
||||
ROOM_PATH + '/' + room.id,
|
||||
'PUT',
|
||||
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) {
|
||||
@@ -100,45 +96,78 @@ const updateRoomStatus = async (
|
||||
roomId: number,
|
||||
status: boolean,
|
||||
roomIdNew?: number
|
||||
): Promise<Nullable<TResponse<TRoom>>> => {
|
||||
if (!roomIdNew) {
|
||||
const response = await sendRequest<TRoom>(
|
||||
ROOM_PATH + '/' + roomId,
|
||||
): Promise<Nullable<IResponse<IRoom>>> => {
|
||||
try {
|
||||
if (!roomIdNew) {
|
||||
const response = await sendRequest<IRoom>(
|
||||
ROOM_PATH + '/' + roomId,
|
||||
'PATCH',
|
||||
JSON.stringify({ status: status })
|
||||
);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
// Update new room status
|
||||
const resNewRoom = await sendRequest<IRoom>(
|
||||
ROOM_PATH + '/' + roomIdNew,
|
||||
'PATCH',
|
||||
JSON.stringify({ status: status })
|
||||
);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
// Update new room status
|
||||
const resNewRoom = await sendRequest<TRoom>(
|
||||
ROOM_PATH + '/' + roomIdNew,
|
||||
'PATCH',
|
||||
JSON.stringify({ status: status })
|
||||
);
|
||||
// Update old room status;
|
||||
const resOldRoom = await sendRequest<IRoom>(
|
||||
ROOM_PATH + '/' + roomId,
|
||||
'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,
|
||||
};
|
||||
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: IRoom): Promise<Nullable<IResponse<IRoom>>> => {
|
||||
try {
|
||||
// Set default status room
|
||||
room.status = false;
|
||||
|
||||
const response = await sendRequest<IRoom>(
|
||||
ROOM_PATH,
|
||||
'POST',
|
||||
JSON.stringify(room)
|
||||
);
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
export { getRoom, updateRoom, updateRoomStatus, getAllRoom, addRoom };
|
||||
|
||||
Reference in New Issue
Block a user