Skip to content

Commit 561a24a

Browse files
authored
feat : 금칙어 수정, 활성/비활성, 삭제 api 연결 완료 (#141)
* feat : 금칙어 수정, 활성/비활성, 삭제 api 연결 완료 * feat : 관리자 페이지 로그아웃 추가 + 관리자 이메일 [email protected]으로 하드코딩
1 parent 3b83467 commit 561a24a

File tree

6 files changed

+81
-15
lines changed

6 files changed

+81
-15
lines changed

src/apis/admin.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ const postBadWords = async (badWordsRequest: BadWords) => {
6363
}
6464
};
6565

66-
// 내 상상대로 만든 필터링 단어 취소 버튼
67-
const patchBadWordsUsed = async (badWordId: string) => {
66+
const patchBadWordsUsed = async (badWordId: string, isUsed: string) => {
67+
const reverseIsUsed = isUsed === 'true' ? false : true;
68+
6869
try {
69-
const res = await client.patch(`/api/bad-words/${badWordId}/status`, { isUsed: false });
70-
if (!res) throw new Error('검열 단어 삭제 도중 에러가 발생했습니다.');
70+
const res = await client.patch(`/api/bad-words/${badWordId}/status`, { isUsed: reverseIsUsed });
71+
if (!res) throw new Error('검열 활성화/비활성화 도중 에러가 발생했습니다.');
7172
console.log(res);
7273
return res;
7374
} catch (error) {
@@ -78,7 +79,18 @@ const patchBadWordsUsed = async (badWordId: string) => {
7879
const patchBadWords = async (badWordId: string, word: string) => {
7980
try {
8081
const res = await client.patch(`/api/bad-words/${badWordId}`, { word: word });
81-
if (!res) throw new Error('검열 단어 삭제 도중 에러가 발생했습니다.');
82+
if (!res) throw new Error('금칙어 수정중 에러가 발생했습니다.');
83+
console.log(res);
84+
return res;
85+
} catch (error) {
86+
console.error(error);
87+
}
88+
};
89+
90+
const deleteBadWords = async (id: string) => {
91+
try {
92+
const res = await client.delete(`/api/bad-words/${id}`);
93+
if (!res) throw new Error('금칙어 삭제 도중 에러가 발생했습니다.');
8294
console.log(res);
8395
return res;
8496
} catch (error) {
@@ -94,4 +106,5 @@ export {
94106
postBadWords,
95107
patchBadWordsUsed,
96108
patchBadWords,
109+
deleteBadWords,
97110
};

src/pages/Admin/Filtering.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect, useState } from 'react';
22

3-
import { getBadWords, patchBadWordsUsed } from '@/apis/admin';
4-
import { AddIcon, AlarmIcon, CancelIcon } from '@/assets/icons';
3+
import { getBadWords } from '@/apis/admin';
4+
import { AddIcon, AlarmIcon } from '@/assets/icons';
55

66
import AddInputButton from './components/AddInputButton';
77
import AdminPageTitle from './components/AdminPageTitle';

src/pages/Admin/components/AddInputButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default function AddInputButton({
2323
if (inputText.word === '') return setAddInputShow(false);
2424
const res = await postBadWords(inputText);
2525
if (res?.status === 200) {
26-
setBadWords((cur) => [...cur, res.data.data]);
26+
setBadWords((cur) => [...cur, { ...res.data.data, isUsed: `${res.data.data.isUsed}` }]);
2727
setAddInputShow(false);
2828
}
2929
};

src/pages/Admin/components/FilterTextItem.tsx

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { patchBadWordsUsed } from '@/apis/admin';
1+
import { deleteBadWords, patchBadWordsUsed } from '@/apis/admin';
22
import { DeleteIcon, PencilIcon, ToggleOff, ToggleOn } from '@/assets/icons';
33
import { useState } from 'react';
44
import PatchInput from './PatchInput';
5+
import { twMerge } from 'tailwind-merge';
56

67
export default function FilterTextItem({
78
badWord,
@@ -12,8 +13,47 @@ export default function FilterTextItem({
1213
}) {
1314
const [patchInputShow, setPatchInputShow] = useState<boolean>(false);
1415

16+
const handleDeleteBadWords = async (id: string) => {
17+
const res = await deleteBadWords(id);
18+
if (res?.status === 200) {
19+
setBadWords((cur) =>
20+
cur.filter((e) => {
21+
if (e.id === id) {
22+
return null;
23+
}
24+
return e;
25+
}),
26+
);
27+
}
28+
};
29+
30+
const handlePatchBadWordsUsed = async (id: string, isUsed: string) => {
31+
const res = await patchBadWordsUsed(id, isUsed);
32+
if (res?.status === 200) {
33+
setBadWords((cur) =>
34+
cur.map((e) => {
35+
if (e.id === id) {
36+
let reverseIsUsed: string;
37+
if (e.isUsed === 'true') {
38+
reverseIsUsed = 'false';
39+
} else {
40+
reverseIsUsed = 'true';
41+
}
42+
return { ...e, isUsed: reverseIsUsed };
43+
}
44+
return e;
45+
}),
46+
);
47+
}
48+
};
49+
50+
const buttonStyle = twMerge(
51+
`flex items-center gap-1.5 rounded-2xl px-4 py-1.5`,
52+
badWord.isUsed === 'true' ? 'bg-primary-3' : 'bg-[#c1c1c1]',
53+
);
54+
1555
return (
16-
<span className="bg-primary-3 flex items-center gap-1.5 rounded-2xl px-4 py-1.5">
56+
<span className={buttonStyle}>
1757
{patchInputShow ? (
1858
<PatchInput
1959
badWordId={badWord.id}
@@ -28,12 +68,20 @@ export default function FilterTextItem({
2868
<PencilIcon className="h-4 w-4" />
2969
</button>
3070

31-
<button onClick={() => {}}>
71+
<button
72+
onClick={() => {
73+
handleDeleteBadWords(badWord.id);
74+
}}
75+
>
3276
<DeleteIcon className="h-5 w-5" />
3377
</button>
3478

35-
<button onClick={() => patchBadWordsUsed(badWord.id)}>
36-
{true ? <ToggleOn className="h-5 w-5" /> : <ToggleOff className="h-5 w-5" />}
79+
<button onClick={() => handlePatchBadWordsUsed(badWord.id, badWord.isUsed)}>
80+
{badWord.isUsed === 'true' ? (
81+
<ToggleOn className="h-5 w-5" />
82+
) : (
83+
<ToggleOff className="h-5 w-5" />
84+
)}
3785
</button>
3886
</span>
3987
);

src/pages/Admin/components/Sidebar.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { twMerge } from 'tailwind-merge';
44
import { AlarmIcon, ArrowDownIcon } from '@/assets/icons';
55

66
import { ADMIN_MENU_LIST } from '../constants';
7+
import useAuthStore from '@/stores/authStore';
78

89
export default function Sidebar() {
910
const location = useLocation();
11+
const logout = useAuthStore((state) => state.logout);
1012

1113
return (
1214
<section className="border-gray-10 flex w-65 shrink-0 flex-col border-r">
@@ -16,7 +18,7 @@ export default function Sidebar() {
1618
</h1>
1719
<section className="mt-2 flex flex-col px-5 py-4">
1820
<h2 className="body-l-b py-2">현재 로그인 계정</h2>
19-
<p className="body-l-r py-2">{'admin123@test.com'}</p>
21+
<p className="body-l-r py-2">{'wl990@naver.com'}</p>
2022
</section>
2123
<hr className="border-gray-20 mx-2.5" />
2224
<section className="flex flex-col py-5">
@@ -54,7 +56,9 @@ export default function Sidebar() {
5456
</section>
5557
<button className="mt-auto flex w-full items-center gap-3 px-5 py-3 hover:bg-amber-100">
5658
<AlarmIcon className="text-gray-80 h-5 w-5" />
57-
<span className="text-gray-80 body-l-m">로그아웃</span>
59+
<span className="text-gray-80 body-l-m" onClick={() => logout()}>
60+
로그아웃
61+
</span>
5862
</button>
5963
</section>
6064
);

src/types/admin.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ interface BadWords {
7575

7676
interface BadWordsData extends BadWords {
7777
id: string;
78+
isUsed: string;
7879
}
7980

8081
//

0 commit comments

Comments
 (0)