-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPopupPortal.tsx
More file actions
134 lines (118 loc) · 3.66 KB
/
PopupPortal.tsx
File metadata and controls
134 lines (118 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { createPortal } from 'react-dom';
import { useState } from 'react';
import { AutoDismissToast, Popup, Toast } from '@pinback/design-system/ui';
import type { PopupState } from '@shared/hooks/useCategoryPopups';
interface Props {
popup: PopupState;
onClose: () => void;
onChange?: (value: string) => void;
onCreateConfirm?: () => void;
onEditConfirm?: (id: number, draft?: string) => void;
onDeleteConfirm?: (id: number) => void;
categoryList?: { id: number; name: string }[];
isToastOpen?: boolean;
onToastClose?: () => void;
}
export default function PopupPortal({
popup,
onClose,
onChange,
onCreateConfirm,
onEditConfirm,
onDeleteConfirm,
categoryList,
isToastOpen,
onToastClose,
}: Props) {
const [draft, setDraft] = useState('');
if (!popup) return null;
const error = (() => {
if (popup.kind === 'delete') return null;
const value = draft.trim();
if (!value) return null;
if (value.length > 10) return '카테고리 이름은 10자 이내로 입력해주세요.';
const isDuplicate = !!categoryList?.some(
(category) =>
category.name === value &&
(popup.kind === 'create' || category.id !== popup.id)
);
return isDuplicate ? '이미 존재하는 카테고리 이름입니다.' : null;
})();
const handleInputChange = (value: string) => {
setDraft(value);
onChange?.(value);
};
const handleCreate = () => {
if (error) return;
onCreateConfirm?.();
};
const handleEdit = () => {
if (error || popup.kind !== 'edit') return;
onEditConfirm?.(popup.id, draft.trim());
};
const handleDelete = () => {
if (popup.kind === 'delete') {
onDeleteConfirm?.(popup.id);
}
};
const actionLabel =
popup.kind === 'create' ? '추가' : popup.kind === 'edit' ? '수정' : '삭제';
return createPortal(
<div className="fixed inset-0 z-[11000]">
<div className="absolute inset-0 bg-black/60" onClick={onClose} />
<div className="absolute inset-0 grid place-items-center p-4">
{popup.kind === 'create' && (
<Popup
type="input"
title="카테고리 추가하기"
left="취소"
right="추가"
isError={Boolean(error)}
helperText={error ?? ''}
onInputChange={handleInputChange}
placeholder="카테고리 제목을 입력해주세요"
onLeftClick={onClose}
onRightClick={handleCreate}
/>
)}
{popup.kind === 'edit' && (
<Popup
type="input"
title="카테고리 수정하기"
left="취소"
right="확인"
isError={Boolean(error)}
helperText={error ?? ''}
onInputChange={handleInputChange}
defaultValue={popup.name}
onLeftClick={onClose}
onRightClick={handleEdit}
/>
)}
{popup.kind === 'delete' && (
<Popup
type="subtext"
title="정말 삭제하시겠어요?"
subtext="저장된 내용이 모두 삭제됩니다."
left="취소"
right="삭제"
onLeftClick={onClose}
onRightClick={handleDelete}
/>
)}
{isToastOpen && (
<div className="absolute bottom-[23.4rem] left-1/2 -translate-x-1/2">
<AutoDismissToast
duration={1000}
fadeMs={1000}
onClose={onToastClose}
>
<Toast text={`${actionLabel}에 실패했어요.\n다시 시도해주세요`} />
</AutoDismissToast>
</div>
)}
</div>
</div>,
document.body
);
}