mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-22 20:01:59 +00:00
Update create room method
This commit is contained in:
@@ -13,7 +13,6 @@ import Input from '../../commons/styles/Input.ts';
|
|||||||
import { Nullable, TRoom } from '../../globals/types.ts';
|
import { Nullable, TRoom } from '../../globals/types.ts';
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
import { STATUS_CODE } from '../../constants/responseStatus.ts';
|
|
||||||
import {
|
import {
|
||||||
ADD_SUCCESS,
|
ADD_SUCCESS,
|
||||||
EDIT_SUCCESS,
|
EDIT_SUCCESS,
|
||||||
@@ -86,14 +85,14 @@ const RoomForm = ({
|
|||||||
// Add request
|
// Add request
|
||||||
const response = await addRoom(room);
|
const response = await addRoom(room);
|
||||||
|
|
||||||
if(response?.statusCode == STATUS_CODE.CREATE) {
|
if(response) {
|
||||||
toast.success(ADD_SUCCESS);
|
toast.success(ADD_SUCCESS);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Edit request
|
// Edit request
|
||||||
const response = await updateRoom(room);
|
const response = await updateRoom(room);
|
||||||
|
|
||||||
if (response?.statusCode == STATUS_CODE.OK) {
|
if (response) {
|
||||||
toast.success(EDIT_SUCCESS);
|
toast.success(EDIT_SUCCESS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ import {
|
|||||||
} from '../../helpers/validators.ts';
|
} from '../../helpers/validators.ts';
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
import { STATUS_CODE } from '../../constants/responseStatus.ts';
|
|
||||||
import {
|
import {
|
||||||
ADD_SUCCESS,
|
ADD_SUCCESS,
|
||||||
EDIT_SUCCESS,
|
EDIT_SUCCESS,
|
||||||
@@ -83,6 +82,12 @@ const UserForm = ({
|
|||||||
|
|
||||||
// Load and set default options room
|
// Load and set default options room
|
||||||
if (rooms.length > 0) {
|
if (rooms.length > 0) {
|
||||||
|
// Init first options
|
||||||
|
options.push({
|
||||||
|
label: '---Select---',
|
||||||
|
value: '0'
|
||||||
|
})
|
||||||
|
|
||||||
rooms.forEach((item) => {
|
rooms.forEach((item) => {
|
||||||
if (!item.status || tempUser?.roomId === item.id)
|
if (!item.status || tempUser?.roomId === item.id)
|
||||||
options.push({
|
options.push({
|
||||||
@@ -94,16 +99,11 @@ const UserForm = ({
|
|||||||
|
|
||||||
if (options.length > 0) {
|
if (options.length > 0) {
|
||||||
setOptions(options);
|
setOptions(options);
|
||||||
reset({ roomId: +options[0].value });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isEmptyObj(tempUser)) {
|
if (!isEmptyObj(tempUser)) {
|
||||||
|
|
||||||
// Init value
|
// Init value
|
||||||
// Set default value when user not have room yet.
|
|
||||||
if (!tempUser.roomId) {
|
|
||||||
tempUser.roomId = +options[0].value;
|
|
||||||
}
|
|
||||||
|
|
||||||
reset(tempUser);
|
reset(tempUser);
|
||||||
} else {
|
} else {
|
||||||
reset(INIT_VALUE_USER_FORM);
|
reset(INIT_VALUE_USER_FORM);
|
||||||
@@ -121,7 +121,7 @@ const UserForm = ({
|
|||||||
// Add request
|
// Add request
|
||||||
const response = await createUser(newUser);
|
const response = await createUser(newUser);
|
||||||
|
|
||||||
if (response?.statusCode === STATUS_CODE.CREATE) {
|
if (response) {
|
||||||
toast.success(ADD_SUCCESS);
|
toast.success(ADD_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ const UserForm = ({
|
|||||||
// Edit request
|
// Edit request
|
||||||
const response = await updateUser(newUser);
|
const response = await updateUser(newUser);
|
||||||
|
|
||||||
if (response?.statusCode == STATUS_CODE.OK) {
|
if (response) {
|
||||||
toast.success(EDIT_SUCCESS);
|
toast.success(EDIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,6 +224,7 @@ const UserForm = ({
|
|||||||
optionsConfigForm={{
|
optionsConfigForm={{
|
||||||
valueAsNumber: true,
|
valueAsNumber: true,
|
||||||
onChange: () => trigger('roomId'),
|
onChange: () => trigger('roomId'),
|
||||||
|
validate: (v) => v !== 0
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ const addRoom = async (room: TRoom): Promise<Nullable<TResponse<TRoom>>> => {
|
|||||||
JSON.stringify(room)
|
JSON.stringify(room)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.statusCode !== STATUS_CODE.OK) {
|
if (response.statusCode !== STATUS_CODE.CREATE) {
|
||||||
throw new Error(errorMsg(response.statusCode, response.msg));
|
throw new Error(errorMsg(response.statusCode, response.msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user