11import { useState , useEffect , useCallback , useRef } from 'react' ;
2- import { Link , useNavigate } from 'react-router-dom' ;
2+ import { Link , useNavigate , useParams } from 'react-router-dom' ;
33import { getUser } from '@/api/userApi' ;
4- import { postShopNotice , type NoticeUpsertRequest } from '@/api/noticeApi' ;
4+ import {
5+ getShopNotice ,
6+ postShopNotice ,
7+ putShopNotice ,
8+ type NoticeUpsertRequest ,
9+ } from '@/api/noticeApi' ;
510import Input from '@/components/common/Input' ;
611import Button from '@/components/common/Button' ;
712import Modal from '@/components/common/Modal' ;
@@ -15,8 +20,9 @@ export default function StoreForm() {
1520 workhour : undefined ,
1621 description : '' ,
1722 } ) ;
18- const shopId = useRef ( '' ) ;
19- const noticeId = useRef ( '' ) ;
23+ const params = useParams ( ) ;
24+ const shopId = useRef ( params . shopId ?? '' ) ;
25+ const noticeId = useRef ( params . noticeId ?? '' ) ;
2026
2127 const [ modal , setModal ] = useState ( {
2228 isOpen : false ,
@@ -36,8 +42,23 @@ export default function StoreForm() {
3642
3743 ( async ( ) => {
3844 try {
39- const userInfo = await getUser ( userId ) ;
40- shopId . current = userInfo . item . shop ?. item . id ?? '' ;
45+ if ( shopId . current ) {
46+ const { item } = await getShopNotice (
47+ shopId . current ,
48+ noticeId . current ,
49+ ) ;
50+ setNoticeInfo ( {
51+ ...item ,
52+ startsAt : new Date (
53+ new Date ( item . startsAt ) . getTime ( ) + 1000 * 60 * 60 * 9 ,
54+ )
55+ . toISOString ( )
56+ . slice ( 0 , 16 ) ,
57+ } ) ;
58+ } else {
59+ const userInfo = await getUser ( userId ) ;
60+ shopId . current = userInfo . item . shop ?. item . id ?? '' ;
61+ }
4162 } catch ( error ) {
4263 setModal ( {
4364 isOpen : true ,
@@ -82,13 +103,18 @@ export default function StoreForm() {
82103
83104 try {
84105 const startsTime = new Date ( noticeInfo ?. startsAt ) . toISOString ( ) ;
85- const { item } = await postShopNotice ( shopId . current , {
106+ const body = {
86107 hourlyPay : noticeInfo ?. hourlyPay ?? 0 ,
87108 startsAt : startsTime ,
88109 workhour : noticeInfo ?. workhour ?? 0 ,
89110 description : noticeInfo ?. description ?? '' ,
90- } ) ;
91- noticeId . current = item . id ;
111+ } ;
112+ if ( noticeId . current ) {
113+ await putShopNotice ( shopId . current , noticeId . current , body ) ;
114+ } else {
115+ const { item } = await postShopNotice ( shopId . current , body ) ;
116+ noticeId . current = item . id ;
117+ }
92118 setModal ( {
93119 isOpen : true ,
94120 message : '등록이 완료되었습니다.' ,
@@ -117,8 +143,14 @@ export default function StoreForm() {
117143 < div className = "min-h-[calc(100vh-102px)] bg-gray-5 md:min-h-[calc(100vh-70px)]" >
118144 < div className = "mx-12 flex flex-col gap-24 pt-40 pb-80 md:mx-32 md:gap-32 md:py-60 lg:mx-auto lg:w-964" >
119145 < div className = "flex items-center justify-between" >
120- < h1 className = "text-h3/24 font-bold md:text-h1/34" > 내 프로필</ h1 >
121- < Link to = "/owner/store" >
146+ < h1 className = "text-h3/24 font-bold md:text-h1/34" > 공고 등록</ h1 >
147+ < Link
148+ to = {
149+ noticeId . current
150+ ? `/owner/post/${ shopId . current } /${ noticeId . current } `
151+ : '/owner/store'
152+ }
153+ >
122154 < img src = { close } alt = "닫기" className = "md:size-32" />
123155 </ Link >
124156 </ div >
0 commit comments