Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/team-moabam/moabam-FE into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
bbearcookie committed Feb 27, 2024
2 parents 3297a0d + 7f07f22 commit fb46d48
Showing 1 changed file with 45 additions and 41 deletions.
86 changes: 45 additions & 41 deletions src/domain/RoomSetting/hooks/useRoomForm.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import z from 'zod';
import { SubmitErrorHandler, SubmitHandler, useForm } from 'react-hook-form';
import { useMutation } from '@tanstack/react-query';
import { zodResolver } from '@hookform/resolvers/zod';
import { SubmitErrorHandler, SubmitHandler, useForm } from 'react-hook-form';
import z from 'zod';
import roomAPI from '@/core/api/functions/roomAPI';
import { roomOptions } from '@/core/api/options';
import { useMoveRoute } from '@/core/hooks';
import { Toast } from '@/shared/Toast';
import { FORM_LITERAL as L } from '@/domain/RoomForm/constants/literals';
import {
FORM_LITERAL as L,
TIME_RANGE
} from '@/domain/RoomForm/constants/literals';

export const formSchema = z.object({
title: z
.string()
.trim()
.min(L.title.min.value, L.title.min.message)
.max(L.title.max.value, L.title.max.message),
announcement: z
.string()
.trim()
.min(L.announcement.min.value)
.max(L.announcement.max.value, L.announcement.max.message),
roomType: z.enum(L.roomType.value, {
required_error: L.roomType.message
}),
certifyTime: z.number(),
routines: z.array(
z.object({
Expand All @@ -29,6 +24,11 @@ export const formSchema = z.object({
.max(L.routines.item.max.value, L.routines.item.max.message)
})
),
title: z
.string()
.trim()
.min(L.title.min.value, L.title.min.message)
.max(L.title.max.value, L.title.max.message),
userCount: z
.number()
.gte(L.userCount.min.value, L.userCount.min.message)
Expand All @@ -46,63 +46,67 @@ export const formSchema = z.object({

export type Inputs = z.infer<typeof formSchema>;

interface useRoomFormProps {
roomId: string;
defaultValues: Inputs;
}
const useRoomForm = () => {
const moveTo = useMoveRoute();

const useRoomForm = ({ roomId, defaultValues }: useRoomFormProps) => {
const mutation = useMutation({
mutationFn: roomAPI.putRoom
mutationFn: roomAPI.postRoom
});
const queryClient = useQueryClient();

const moveTo = useMoveRoute();

const form = useForm<Inputs>({
defaultValues,
defaultValues: {
roomType: undefined,
certifyTime: TIME_RANGE['MORNING'][0],
routines: [{ value: '' }],
userCount: 5,
title: '',
password: ''
},
mode: 'onBlur',
resolver: zodResolver(formSchema)
});

const onSubmit: SubmitHandler<Inputs> = (data) => {
mutation.mutate(
{
roomId,
title: data.title,
announcement: data.announcement,
password: data.password,
roomType: data.roomType,
routines: data.routines.map((r) => r.value),
certifyTime: data.certifyTime % 24,
maxUserCount: data.userCount,
password: data.password
maxUserCount: data.userCount
},
{
onSuccess: (data) => {
queryClient.invalidateQueries({
queryKey: roomOptions.detail(roomId).queryKey
});

Toast.show({
message: '방 정보를 수정했어요.',
message: '새로운 방을 만들었어요.',
status: 'confirm'
});

moveTo('roomDetail', { roomId });
moveTo('roomDetail', { roomId: data });
},
onError: (error) => {
const { setError } = form;

Toast.show({
message: error.response?.data?.message || '오류가 발생했어요.',
message: error.response?.data?.message ?? '오류가 발생했어요.',
status: 'danger'
});

if (error.response?.data?.validation) {
const { title, announcement, password, certifyTime, maxUserCount } =
error.response.data.validation;
const { setError } = form;

const {
title,
password,
roomType,
routines,
certifyTime,
maxUserCount
} = error.response.data.validation;

setError('title', { message: title });
setError('announcement', { message: announcement });
setError('password', { message: password });
setError('roomType', { message: roomType });
setError('routines', { message: routines });
setError('certifyTime', { message: certifyTime });
setError('userCount', { message: maxUserCount });
}
Expand Down

0 comments on commit fb46d48

Please sign in to comment.