|
| 1 | +import { getApi } from '@/app/api/config/appConfig'; |
1 | 2 | import Button from '@/shared/components/button/Button'; |
2 | 3 | import TextButton from '@/shared/components/button/TextButton'; |
3 | 4 | import Input from '@/shared/components/Input-box/Input'; |
4 | 5 | import ModalLayout from '@/shared/components/modal-pop/ModalLayout'; |
| 6 | +import { useToast } from '@/shared/hook/useToast'; |
| 7 | +import { Dispatch, SetStateAction } from 'react'; |
5 | 8 |
|
6 | 9 | interface Props { |
7 | 10 | open: boolean; |
8 | 11 | onClose: () => void; |
| 12 | + setNickName: (v: string) => void; |
| 13 | + setIsOpen: Dispatch<SetStateAction<boolean>>; |
| 14 | + editNickName: string; |
| 15 | + setEditNickName: Dispatch<SetStateAction<string>>; |
9 | 16 | } |
10 | 17 |
|
11 | | -function EditNickName({ open, onClose }: Props) { |
| 18 | +function EditNickName({ |
| 19 | + open, |
| 20 | + onClose, |
| 21 | + setNickName, |
| 22 | + setIsOpen, |
| 23 | + editNickName, |
| 24 | + setEditNickName, |
| 25 | +}: Props) { |
| 26 | + const { toastError } = useToast(); |
| 27 | + const handlesave = async () => { |
| 28 | + if (editNickName.length <= 1) { |
| 29 | + toastError('닉네임은 2글자 이상 입력해야합니다'); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + await setNickName(editNickName); |
| 34 | + |
| 35 | + await fetch(`${getApi}/me/profile`, { |
| 36 | + method: 'PATCH', |
| 37 | + credentials: 'include', |
| 38 | + headers: { |
| 39 | + 'Content-Type': 'application/json', |
| 40 | + }, |
| 41 | + body: JSON.stringify({ |
| 42 | + nickname: editNickName, |
| 43 | + }), |
| 44 | + }); |
| 45 | + await setIsOpen(false); |
| 46 | + }; |
| 47 | + |
| 48 | + const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 49 | + setEditNickName(e.target.value); |
| 50 | + }; |
| 51 | + |
12 | 52 | return ( |
13 | 53 | <ModalLayout |
14 | 54 | title="닉네임 수정" |
15 | 55 | description="닉네임을 수정해주세요." |
16 | 56 | open={open} |
17 | 57 | onClose={onClose} |
18 | | - buttons={<Button>저장</Button>} |
| 58 | + buttons={ |
| 59 | + <Button type="submit" onClick={handlesave}> |
| 60 | + 저장 |
| 61 | + </Button> |
| 62 | + } |
19 | 63 | > |
20 | 64 | <div className="flex flex-col gap-3 items-end"> |
21 | 65 | <label htmlFor="editNickName" className="sr-only"> |
22 | 66 | 닉네임변경 |
23 | 67 | </label> |
24 | | - <Input placeholder="8글자 이내로 입력해주세요" id="editNicName" className="w-full" /> |
| 68 | + <Input |
| 69 | + onChange={(e) => handleChange(e)} |
| 70 | + placeholder="8글자 이내로 입력해주세요" |
| 71 | + id="editNickName" |
| 72 | + className="w-full" |
| 73 | + /> |
25 | 74 | <TextButton onClick={onClose}>기존 이름으로 돌아가기</TextButton> |
26 | 75 | </div> |
27 | 76 | </ModalLayout> |
|
0 commit comments