1
1
'use client' ;
2
2
3
- import { useState } from 'react' ;
3
+ import { useEffect , useState } from 'react' ;
4
4
import Toast from '@/utils/notification' ;
5
- import { postBoard } from '@/service/board ' ;
5
+ import usePostBoard from '@/hooks/mutations/usePostBoard ' ;
6
6
import Avatar from '../../../../ui/Avatar' ;
7
7
import Button from '../../../../ui/Button' ;
8
8
import FlexBox from '../../../../ui/FlexBox' ;
@@ -15,38 +15,17 @@ export default function Upload({
15
15
nickname : string | undefined ;
16
16
} ) {
17
17
const [ postText , setPostText ] = useState ( '' ) ;
18
- const [ isUploading , setIsUploading ] = useState ( false ) ;
18
+ const { mutate : postBoard , isLoading , isSuccess } = usePostBoard ( postText ) ;
19
19
20
20
const maxCharacters = 100 ;
21
21
const isOverMaxChar = postText . length > maxCharacters ;
22
22
23
23
const handleTextChange = ( event : React . ChangeEvent < HTMLTextAreaElement > ) => {
24
24
setPostText ( event . target . value ) ;
25
25
} ;
26
-
27
- // 데이터 전송을 위한 함수
28
- const onUploadBoard = async ( ) => {
29
- if ( ! postText || isOverMaxChar ) {
30
- return ;
31
- }
32
- setIsUploading ( true ) ;
33
- try {
34
- const response = await postBoard ( {
35
- title : 'title' ,
36
- content : postText ,
37
- } ) ;
38
- if ( response . content ) {
39
- Toast . success ( '게시물이 성공적으로 업로드되었습니다.' ) ;
40
- setPostText ( '' ) ;
41
- } else {
42
- Toast . error ( '업로드에 실패했습니다. 잠시 후 다시 시도해주세요.' ) ;
43
- }
44
- } catch ( error ) {
45
- Toast . error ( '오류가 발생했습니다. 잠시 후 다시 시도해주세요.' ) ;
46
- } finally {
47
- setIsUploading ( false ) ;
48
- }
49
- } ;
26
+ useEffect ( ( ) => {
27
+ if ( isSuccess ) setPostText ( '' ) ;
28
+ } , [ isSuccess ] ) ;
50
29
51
30
return (
52
31
< form className = "flex flex-col bg-primary-50 p-5 border-[1px] border-primary-300 rounded-[10px] w-full" >
@@ -82,18 +61,14 @@ export default function Upload({
82
61
>
83
62
파일
84
63
</ Button >
85
- { isUploading ? (
86
- < div > 업로드 중입니다</ div >
87
- ) : (
88
- < Button
89
- size = "lg"
90
- disabled = { isOverMaxChar }
91
- className = "w-40"
92
- onClickAction = { ( ) => onUploadBoard ( ) }
93
- >
94
- 업로드
95
- </ Button >
96
- ) }
64
+ < Button
65
+ size = "lg"
66
+ disabled = { postText . length === 0 || isOverMaxChar || isLoading }
67
+ className = "w-40"
68
+ onClickAction = { ( ) => postBoard ( ) }
69
+ >
70
+ { isLoading ? '업로드 중입니다' : '업로드' }
71
+ </ Button >
97
72
< div className = "mt-4" id = "renderedText" />
98
73
</ FlexBox >
99
74
</ form >
0 commit comments