Skip to content

Commit a756403

Browse files
committed
[refactor]알림 컨펌기능
1 parent de135a2 commit a756403

File tree

6 files changed

+96
-41
lines changed

6 files changed

+96
-41
lines changed

src/domains/mypage/components/EditNickName.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Dispatch, SetStateAction, useEffect, useState } from 'react';
99
interface Props {
1010
open: boolean;
1111
onClose: () => void;
12-
nickname:string
12+
nickname: string;
1313
setNickName: (v: string) => void;
1414
setIsOpen: Dispatch<SetStateAction<boolean>>;
1515
editNickName: string;
@@ -25,14 +25,14 @@ function EditNickName({
2525
editNickName,
2626
setEditNickName,
2727
}: Props) {
28-
const [defaultNickname,setDefaultNickname] = useState(nickname)
29-
const { toastSuccess,toastError } = useToast();
28+
const [defaultNickname, setDefaultNickname] = useState(nickname);
29+
const { toastSuccess, toastError } = useToast();
3030

3131
useEffect(() => {
32-
setEditNickName(nickname)
33-
setDefaultNickname(nickname)
34-
}, [nickname,setEditNickName])
35-
32+
setEditNickName(nickname);
33+
setDefaultNickname(nickname);
34+
}, [nickname, setEditNickName]);
35+
3636
const handlesave = async () => {
3737
if (editNickName.length <= 1 || editNickName.length >= 8) {
3838
toastError('닉네임은 2글자 이상 입력해야합니다');
@@ -57,12 +57,11 @@ function EditNickName({
5757

5858
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
5959
setEditNickName(e.target.value);
60-
6160
};
6261

6362
const handleDefaultNickname = () => {
64-
setEditNickName(defaultNickname)
65-
}
63+
setEditNickName(defaultNickname);
64+
};
6665

6766
return (
6867
<ModalLayout
Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use client';
22
import { getApi } from '@/app/api/config/appConfig';
3-
import { useToast } from '@/shared/hook/useToast';
43
import clsx from 'clsx';
54
import { useEffect, useState } from 'react';
5+
import AlarmConfirm from './pages/my-alarm/AlarmConfirm';
66

77
function ToggleBtn() {
8-
const [isClick, setIsClick] = useState<boolean | null>(null);
9-
const { toastSuccess } = useToast();
8+
const [isAlarm, setIsAlarm] = useState<boolean | null>(null);
9+
const [isClick, setIsClick] = useState<boolean>(false);
1010

1111
useEffect(() => {
1212
const fetchToggle = async () => {
@@ -16,7 +16,8 @@ function ToggleBtn() {
1616
credentials: 'include',
1717
});
1818
const json = await res.json();
19-
setIsClick(json.data.enabled);
19+
console.log(json);
20+
setIsAlarm(json.data.enabled);
2021
} catch {
2122
console.error();
2223
}
@@ -25,36 +26,36 @@ function ToggleBtn() {
2526
}, []);
2627

2728
const handleClick = async () => {
28-
if (isClick === null) return;
29-
const next = !isClick;
30-
setIsClick(next);
31-
32-
await fetch(`${getApi}/me/notification-setting`, {
33-
method: 'PATCH',
34-
credentials: 'include',
35-
headers: { 'Content-Type': 'application/json' },
36-
body: JSON.stringify({
37-
enabled: next,
38-
}),
39-
});
40-
next ? toastSuccess('알림이 설정되었습니다.') : toastSuccess('알림이 해제되었습니다');
29+
setIsClick(true);
4130
};
4231

4332
return (
44-
<button
45-
className={clsx(
46-
'rounded-full flex py-0.5 pl-[2px] w-17 h-7 duration-300',
47-
isClick ? 'bg-tertiary' : 'bg-white'
33+
<div>
34+
{isClick && (
35+
<AlarmConfirm
36+
open={isClick}
37+
onClose={() => setIsClick(false)}
38+
state={isAlarm}
39+
cancle={() => setIsClick(isClick)}
40+
setIsAlarm={setIsAlarm}
41+
setIsClick={setIsClick}
42+
/>
4843
)}
49-
onClick={handleClick}
50-
>
51-
<div
44+
<button
5245
className={clsx(
53-
'rounded-full w-6 h-6 duration-300',
54-
isClick ? 'bg-secondary translate-x-10' : 'bg-gray-dark'
46+
'rounded-full flex py-0.5 pl-[2px] w-17 h-7 duration-300',
47+
isAlarm ? 'bg-tertiary' : 'bg-white'
5548
)}
56-
></div>
57-
</button>
49+
onClick={handleClick}
50+
>
51+
<div
52+
className={clsx(
53+
'rounded-full w-6 h-6 duration-300',
54+
isAlarm ? 'bg-secondary translate-x-10' : 'bg-gray-dark'
55+
)}
56+
></div>
57+
</button>
58+
</div>
5859
);
5960
}
6061
export default ToggleBtn;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useConfirm } from '@/domains/mypage/hook/useConfirm';
2+
import ConfirmModal from '@/shared/components/modal-pop/ConfirmModal';
3+
import { Dispatch, SetStateAction } from 'react';
4+
5+
interface Props {
6+
open: boolean;
7+
onClose: () => void;
8+
state: boolean | null;
9+
cancle: () => void;
10+
setIsAlarm: Dispatch<SetStateAction<boolean | null>>;
11+
setIsClick: Dispatch<SetStateAction<boolean>>;
12+
}
13+
14+
function AlarmConfirm({ open, onClose, state, cancle, setIsAlarm, setIsClick }: Props) {
15+
const { patchAlarm } = useConfirm(state, setIsAlarm, setIsClick);
16+
17+
return (
18+
<ConfirmModal
19+
open={open}
20+
onClose={onClose}
21+
description={state ? '알림을 해제하시겠습니까?' : '알림을 설정하시겠습니까?'}
22+
onConfirm={patchAlarm}
23+
onCancel={cancle}
24+
></ConfirmModal>
25+
);
26+
}
27+
export default AlarmConfirm;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { getApi } from '@/app/api/config/appConfig';
2+
import { useToast } from '@/shared/hook/useToast';
3+
import { Dispatch, SetStateAction } from 'react';
4+
5+
export function useConfirm(
6+
state: boolean | null,
7+
setIsAlarm: Dispatch<SetStateAction<boolean | null>>,
8+
setIsClick: Dispatch<SetStateAction<boolean>>
9+
) {
10+
const { toastSuccess } = useToast();
11+
const next = !state;
12+
const patchAlarm = async () => {
13+
await fetch(`${getApi}/me/notification-setting`, {
14+
method: 'PATCH',
15+
credentials: 'include',
16+
headers: { 'Content-Type': 'application/json' },
17+
body: JSON.stringify({
18+
enabled: next,
19+
}),
20+
});
21+
setIsAlarm(next);
22+
setIsClick(false);
23+
24+
(await next) ? toastSuccess('알림이 설정되었습니다.') : toastSuccess('알림이 해제되었습니다');
25+
};
26+
return { patchAlarm };
27+
}

src/domains/mypage/main/MySetting.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import WithdrawModal from '@/domains/mypage/components/WithdrawModal';
55
import TextButton from '@/shared/components/button/TextButton';
66
import { useEffect, useState } from 'react';
77
import useFetchProfile from '../api/fetchProfile';
8+
import AlarmConfirm from '../components/pages/my-alarm/AlarmConfirm';
89

910
function MySetting() {
1011
const { profile } = useFetchProfile();
@@ -42,6 +43,7 @@ function MySetting() {
4243
</div>
4344
<div className="flex justify-between py-5">
4445
<h2>알람설정</h2>
46+
4547
<ToggleBtn />
4648
</div>
4749
</div>

src/shared/components/Input-box/Input.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Button from '../button/Button';
1313

1414
interface Props {
1515
placeholder: string;
16-
value?: string
16+
value?: string;
1717

1818
type?: HTMLInputTypeAttribute;
1919
ref?: Ref<HTMLInputElement | null>;
@@ -44,7 +44,7 @@ function Input({
4444
type,
4545
ref,
4646
value,
47-
47+
4848
size,
4949
variant = 'default',
5050
className,
@@ -62,7 +62,6 @@ function Input({
6262
className={`outline-none w-full flex-1 leading-${size}`}
6363
ref={ref}
6464
value={value}
65-
6665
onChange={onChange}
6766
{...rest}
6867
/>

0 commit comments

Comments
 (0)