@@ -11,7 +11,7 @@ import { fontsObject } from '@sopt-makers/fonts';
1111import { FormType , schema } from '@type/form' ;
1212import dynamic from 'next/dynamic' ;
1313import { useRouter } from 'next/router' ;
14- import { useEffect } from 'react' ;
14+ import { useEffect , useRef , useState } from 'react' ;
1515import { FormProvider , SubmitHandler , useForm } from 'react-hook-form' ;
1616import { styled } from 'stitches.config' ;
1717
@@ -21,7 +21,7 @@ const DevTool = dynamic(() => import('@hookform/devtools').then(module => module
2121
2222const MakePage = ( ) => {
2323 const router = useRouter ( ) ;
24- const { draftFormValues } = useDraftCreateMeeting ( ) ;
24+ const { draftFormValues, removeDraftCreateMeeting } = useDraftCreateMeeting ( ) ;
2525 const formMethods = useForm < FormType > ( {
2626 mode : 'onChange' ,
2727 reValidateMode : 'onChange' ,
@@ -34,6 +34,8 @@ const MakePage = () => {
3434 } ) ;
3535 const { isValid, errors, isDirty } = formMethods . formState ;
3636 const { mutate : mutateCreateMeeting , isPending : isSubmitting } = usePostMeetingMutation ( ) ;
37+ const submittedRef = useRef ( false ) ;
38+ const [ hasDraftLoaded , setHasDraftLoaded ] = useState ( false ) ;
3739
3840 const handleChangeImage = ( index : number , url : string ) => {
3941 const files = formMethods . getValues ( ) . files . slice ( ) ;
@@ -51,30 +53,43 @@ const MakePage = () => {
5153 mutateCreateMeeting ( formData , {
5254 onSuccess : data => {
5355 ampli . completedMakeGroup ( ) ;
56+ submittedRef . current = true ;
57+ removeDraftCreateMeeting ( ) ;
5458 alert ( '모임을 개설했습니다.' ) ;
5559 router . push ( `/detail?id=${ data . meetingId } ` ) ;
5660 } ,
5761 } ) ;
5862 } ;
5963
6064 useEffect ( ( ) => {
61- return ( ) => {
62- if ( isDirty ) {
65+ const persistDraft = ( ) => {
66+ if ( isDirty && ! submittedRef . current ) {
6367 LocalStorage . setItem ( LocalStorageKey . DraftCreateMeeting , {
6468 dateTime : Date . now ( ) ,
6569 formValues : formMethods . getValues ( ) ,
6670 } ) ;
6771 }
6872 } ;
73+
74+ window . addEventListener ( 'beforeunload' , persistDraft ) ;
75+ window . addEventListener ( 'pagehide' , persistDraft ) ;
76+
77+ return ( ) => {
78+ window . removeEventListener ( 'beforeunload' , persistDraft ) ;
79+ window . removeEventListener ( 'pagehide' , persistDraft ) ;
80+ persistDraft ( ) ;
81+ } ;
6982 } , [ formMethods , isDirty ] ) ;
7083
7184 useEffect ( ( ) => {
7285 if ( draftFormValues ) {
7386 formMethods . reset ( draftFormValues ) ;
87+ setHasDraftLoaded ( true ) ;
7488 }
75- } , [ draftFormValues ] ) ;
89+ } , [ draftFormValues , formMethods ] ) ;
7690
7791 const handleSubmit = formMethods . handleSubmit ( onSubmit ) ;
92+ const isSubmitDisabled = isSubmitting || ! isValid || Object . keys ( errors ) . length > 0 || ( ! isDirty && ! hasDraftLoaded ) ;
7893
7994 return (
8095 < FormProvider { ...formMethods } >
@@ -87,14 +102,14 @@ const MakePage = () => {
87102 handleChangeImage = { handleChangeImage }
88103 handleDeleteImage = { handleDeleteImage }
89104 onSubmit = { handleSubmit }
90- disabled = { isSubmitting || ! isValid || Object . keys ( errors ) . length > 0 || ! isDirty }
105+ disabled = { isSubmitDisabled }
91106 />
92107 </ SFormContainer >
93108 < TableOfContents
94109 label = "작성 항목"
95110 onSubmit = { handleSubmit }
96111 submitButtonLabel = "개설하기"
97- disabled = { isSubmitting || ! isValid || Object . keys ( errors ) . length > 0 || ! isDirty }
112+ disabled = { isSubmitDisabled }
98113 />
99114 </ SContainer >
100115 { /* eslint-disable-next-line @typescript-eslint/ban-ts-comment */ }
0 commit comments