1
1
import DialogButton from '@/components/common/DialogButton' ;
2
2
import { InputRef } from '../common/InputForm.tsx' ;
3
- import { useState , useRef } from 'react' ;
3
+ import { useState , useRef , useEffect } from 'react' ;
4
4
import { Events } from '@/utils/index.ts' ;
5
- import { addPersonalSchedule } from '@/apis/personalScheduleApi.ts' ;
5
+ import { addPersonalSchedule , updatePersonalSchedule } from '@/apis/personalScheduleApi.ts' ;
6
6
7
- const CreateEventButton = ( ) => {
7
+ export interface eventProps {
8
+ id ?: number ;
9
+ title ?: string ;
10
+ start_date ?: string ;
11
+ end_date ?: string ;
12
+ }
13
+
14
+ const CreateEventButton = ( props : eventProps ) => {
8
15
const [ eventTitle , setTitle ] = useState ( '' ) ;
9
16
const [ startDate , setStartDate ] = useState ( '' ) ;
10
17
const [ endDate , setEndDate ] = useState ( '' ) ;
11
18
const titleRef = useRef < HTMLInputElement > ( null ) ;
12
19
const startRef = useRef < HTMLInputElement > ( null ) ;
13
20
const endRef = useRef < HTMLInputElement > ( null ) ;
14
21
22
+ useEffect ( ( ) => {
23
+ console . log ( props ) ;
24
+
25
+ if ( props . start_date ) {
26
+ setStartDate ( props . start_date ) ;
27
+ startRef ! . current ! . value = props . start_date ;
28
+ }
29
+ if ( props . end_date ) {
30
+ setEndDate ( props . end_date ) ;
31
+ endRef ! . current ! . value = props . end_date ;
32
+ }
33
+ if ( props . title ) {
34
+ setTitle ( props . title ) ;
35
+ titleRef ! . current ! . value = props . title ;
36
+ }
37
+ } , [ props ] ) ;
38
+
15
39
const onTitleChanged = ( event : React . ChangeEvent < HTMLInputElement > ) => {
16
40
setTitle ( event . target . value ) ;
17
41
} ;
@@ -31,7 +55,15 @@ const CreateEventButton = () => {
31
55
start : startDate ,
32
56
end : endDate === '' ? startDate : endDate ,
33
57
} ;
34
- addPersonalSchedule ( newEvent ) ;
58
+ if ( props . id ) {
59
+ updatePersonalSchedule ( props . id , newEvent ) . catch ( ( err ) => {
60
+ console . log ( err ) ;
61
+ } ) ;
62
+ } else {
63
+ addPersonalSchedule ( newEvent ) . catch ( ( err ) => {
64
+ console . log ( err ) ;
65
+ } ) ;
66
+ }
35
67
}
36
68
37
69
setTitle ( '' ) ;
@@ -56,16 +88,16 @@ const CreateEventButton = () => {
56
88
< InputRef type = "date" title = "끝 날짜" placeholder = "YYYY-MM-DD" onChange = { onEndDateChanged } ref = { endRef } />
57
89
< hr className = "mb-2 mt-2" />
58
90
< button className = "btn w-full bg-primary text-base-100" onClick = { onCreateClicked } >
59
- 추가하기
91
+ { props . id ? '수정하기' : ' 추가하기' }
60
92
</ button >
61
93
</ div >
62
94
) ;
63
95
return (
64
96
< div className = "p-8 pl-0 pr-0" >
65
97
< DialogButton
66
98
classname = "btn bg-primary text-base-100 w-full"
67
- name = { '새 일정 추가하기' }
68
- title = { '일정 추가' }
99
+ name = { props . id ? '수정' : '새 일정 추가하기' }
100
+ title = { props . id ? '일정 수정' : '일정 추가' }
69
101
desc = { '' }
70
102
children = { eventForm }
71
103
/>
0 commit comments