diff --git a/src/pages/employer/shops/[shopId]/notices/[noticeId]/edit.tsx b/src/pages/employer/shops/[shopId]/notices/[noticeId]/edit.tsx index e69de29..9917ef7 100644 --- a/src/pages/employer/shops/[shopId]/notices/[noticeId]/edit.tsx +++ b/src/pages/employer/shops/[shopId]/notices/[noticeId]/edit.tsx @@ -0,0 +1,178 @@ +import { Button, DateInput, Input, Modal, TimeInput } from '@/components/ui'; +import useAuth from '@/hooks/useAuth'; +import axiosInstance from '@/lib/axios'; +import { useRouter } from 'next/router'; +import { useEffect, useState } from 'react'; + +interface NoticePayload { + hourlyPay: number; + startsAt: string; + workhour: number; + description: string; +} + +const EmployerNoticeEditPage = () => { + const router = useRouter(); + const { user } = useAuth(); + const { noticeId } = router.query; + + const [wage, setWage] = useState(''); + const [date, setDate] = useState(null); + const [time, setTime] = useState(null); + const [workhour, setWorkhour] = useState(); + const [description, setDescription] = useState(''); + + const [loading, setLoading] = useState(true); + const [modalOpen, setModalOpen] = useState(false); + + useEffect(() => { + if (!user) return; + if (!user.shop) { + alert('접근 권한이 없습니다.'); + router.replace('/'); + return; + } + + const fetchNotice = async () => { + try { + const response = await axiosInstance.get( + `/shops/${user.shop?.item.id}/notices/${noticeId}` + ); + const notice = response.data.item; + + setWage(notice.hourlyPay.toString()); + setWorkhour(notice.workhour); + setDescription(notice.description); + + const startDate = new Date(notice.startsAt); + setDate(startDate); + setTime(startDate); + } catch { + alert('공고 정보를 불러오는 중 오류가 발생했습니다.'); + router.back(); + } finally { + setLoading(false); + } + }; + + if (noticeId) fetchNotice(); + }, [noticeId, user, router]); + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + + if (!date || !time || !wage || !workhour || !description) return; + if (!user?.shop || !noticeId) return; + + const combinedDateTime = new Date(date); + combinedDateTime.setHours(time.getHours(), time.getMinutes(), 0, 0); + + const payload: NoticePayload = { + hourlyPay: Number(wage), + startsAt: combinedDateTime.toISOString(), + workhour, + description, + }; + + try { + await axiosInstance.put(`/shops/${user.shop.item.id}/notices/${noticeId}`, payload); + setModalOpen(true); + } catch (error) { + alert(error instanceof Error ? error.message : '공고 수정 중 오류 발생'); + } + }; + + const handleModalClose = () => { + setModalOpen(false); + if (user?.shop) { + router.push(`/employer/shops/${user.shop.item.id}/notices/${noticeId}`); + } + }; + + if (loading) return; + if (!user?.shop) return null; + + return ( +
+

공고 편집

+ +
+
+ setWage(e.currentTarget.value.replace(/\D+/g, ''))} + /> + + setWorkhour(Number(e.currentTarget.value))} + /> + + + setDate(selectedDate instanceof Date ? selectedDate : new Date(selectedDate)) + } + /> + + setTime(selectedTime)} + /> +
+ +
+ +