Skip to content

Commit bf16d46

Browse files
authored
Feat : 토스트 기능 추가 / Fix : 모달 닫힌 상태에서 스크롤 안됨 해결
Feat : 토스트 기능 추가 / Fix : 모달 닫힌 상태에서 스크롤 안됨 해결
2 parents 8d16b7f + 5c45626 commit bf16d46

File tree

19 files changed

+181
-45
lines changed

19 files changed

+181
-45
lines changed

components/Link/AddLinkInput.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { Modal } from "../modal/modalManager/ModalManager";
44
import Image from "next/image";
55
import SubmitButton from "../SubMitButton";
66
import useModalStore from "@/store/useModalStore";
7+
import toast from "react-hot-toast";
8+
import toastMessages from "@/lib/toastMessage";
9+
import { urlRegex } from "@/util/regex";
710

811
const AddLinkInput = ({ folderList }: FolderListData) => {
912
const { isOpen, openModal } = useModalStore();
@@ -14,8 +17,14 @@ const AddLinkInput = ({ folderList }: FolderListData) => {
1417
};
1518

1619
const handleClick = () => {
17-
openModal("AddModal", { list: folderList, link: link });
18-
setLink("");
20+
if (link === "") {
21+
toast.error(toastMessages.error.inputLink);
22+
} else if (!urlRegex.test(link.trim())) {
23+
toast.error(toastMessages.error.invalidLink);
24+
} else {
25+
openModal("AddModal", { list: folderList, link: link });
26+
setLink("");
27+
}
1928
};
2029

2130
const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {

components/Search/SearchInput.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { ChangeEvent, FormEvent, useState } from "react";
22
import { useRouter } from "next/router";
33
import Image from "next/image";
4+
import toast from "react-hot-toast";
5+
import toastMessages from "@/lib/toastMessage";
46

57
export const SearchInput = () => {
68
const router = useRouter();

components/modal/AddFolderModal.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ import ModalContainer from "./modalComponents/ModalContainer";
44
import ModalInput from "./modalComponents/ModalInput";
55
import useModalStore from "@/store/useModalStore";
66
import SubmitButton from "../SubMitButton";
7+
import toast from "react-hot-toast";
8+
import toastMessages from "@/lib/toastMessage";
79

810
const AddFolderModal = ({ folderName }: { folderName: string }) => {
911
const [value, setValue] = useState("");
1012

1113
const { closeModal } = useModalStore();
1214

1315
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
14-
setValue(e.target.value);
16+
const newValue = e.target.value;
17+
if (newValue.length > 5) {
18+
toast.error(toastMessages.error.limitFolderNameLength);
19+
} else {
20+
setValue(newValue);
21+
}
1522
};
1623
const handleSubmit = async () => {
1724
const body = {
@@ -20,8 +27,9 @@ const AddFolderModal = ({ folderName }: { folderName: string }) => {
2027
if (value !== "") {
2128
try {
2229
await postFolders(body);
30+
toast.success(toastMessages.success.addFolder);
2331
} catch (error) {
24-
console.log(error, "폴더 생성 에러");
32+
toast.error(toastMessages.error.addFolder);
2533
}
2634
}
2735
closeModal();

components/modal/AddModal.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,29 @@ import SubmitButton from "../SubMitButton";
55
import { useState } from "react";
66
import { postLink } from "@/lib/api/link";
77
import useModalStore from "@/store/useModalStore";
8+
import toast from "react-hot-toast";
9+
import toastMessages from "@/lib/toastMessage";
10+
import { useRouter } from "next/router";
811

912
const AddModal = ({ list, link }: { list: FolderItemType[]; link: string }) => {
1013
const [selectedId, setSelectedId] = useState<number | null>(null);
1114
const { closeModal } = useModalStore();
15+
const route = useRouter();
16+
1217
const handleSubmit = async () => {
1318
const body = {
1419
folderId: Number(selectedId),
1520
url: link,
1621
};
17-
if (link !== "" && selectedId) {
22+
if (!selectedId) {
23+
toast.error(toastMessages.error.selectFolder);
24+
} else {
1825
try {
1926
await postLink(body);
27+
toast.success(toastMessages.success.addLink);
28+
route.push(`/link?folder=${selectedId}`);
2029
} catch (error) {
21-
console.log(error, "링크 생성 에러");
30+
toast.error(toastMessages.error.addLink);
2231
} finally {
2332
closeModal();
2433
}

components/modal/DeleteFolderModal.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import useModalStore from "@/store/useModalStore";
22
import SubmitButton from "../SubMitButton";
33
import ModalContainer from "./modalComponents/ModalContainer";
44
import { deleteFolder } from "@/lib/api/folder";
5+
import toast from "react-hot-toast";
6+
import toastMessages from "@/lib/toastMessage";
57

68
const DeleteFolderModal = ({
79
folderName,
@@ -11,13 +13,20 @@ const DeleteFolderModal = ({
1113
folderId: number;
1214
}) => {
1315
const { closeModal } = useModalStore();
16+
let linkCount: number;
1417
const handleSubmit = async () => {
15-
// 폴더 내에 링크 개수 0 일때만 삭제 가능 -> 링크 1개 이상이면 error toast 띄우겠습니다.
16-
try {
17-
await deleteFolder(folderId);
18-
} catch (error) {
19-
console.log(error, "DeleteFolderModal 폴더 삭제 에러");
20-
} finally {
18+
// 폴더 내에 링크 개수 0 일때만 폴더 삭제 가능 -> 링크 1개 이상이면 error toast 띄우고 있음 or 전체 링크 삭제 후 폴더 삭제
19+
if (linkCount === 0) {
20+
try {
21+
await deleteFolder(folderId);
22+
toast.success(toastMessages.success.deleteFolder);
23+
} catch (error) {
24+
toast.error(toastMessages.error.deleteFolder);
25+
} finally {
26+
closeModal();
27+
}
28+
} else {
29+
toast.error(toastMessages.error.deleteNonEmptyFolder);
2130
closeModal();
2231
}
2332
};

components/modal/DeleteLinkModal.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import useModalStore from "@/store/useModalStore";
22
import SubmitButton from "../SubMitButton";
33
import ModalContainer from "./modalComponents/ModalContainer";
44
import { useLinkCardStore } from "@/store/useLinkCardStore";
5+
import toast from "react-hot-toast";
6+
import toastMessages from "@/lib/toastMessage";
57

68
const DeleteLinkModal = ({
79
link,
@@ -17,8 +19,9 @@ const DeleteLinkModal = ({
1719
try {
1820
await deleteLink(linkId);
1921
closeModal();
22+
toast.success(toastMessages.success.deleteLink);
2023
} catch (error) {
21-
console.error("Failed to delete the link:", error);
24+
toast.error(toastMessages.error.deleteLink);
2225
}
2326
};
2427

components/modal/EditLink.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import ModalContainer from "./modalComponents/ModalContainer";
44
import ModalInput from "./modalComponents/ModalInput";
55
import useModalStore from "@/store/useModalStore";
66
import SubmitButton from "../SubMitButton";
7+
import toast from "react-hot-toast";
8+
import toastMessages from "@/lib/toastMessage";
9+
import { urlRegex } from "@/util/regex";
10+
import { error } from "console";
711

812
const EditLink = ({
913
folderName,
@@ -25,10 +29,21 @@ const EditLink = ({
2529
const body = {
2630
url: value,
2731
};
28-
if (value !== "") {
29-
await updateLink(linkId, body);
32+
if (value === link) {
33+
toast.error(toastMessages.error.sameLink);
34+
} else if (value === "") {
35+
toast.error(toastMessages.error.inputLink);
36+
} else if (!urlRegex.test(value)) {
37+
toast.error(toastMessages.error.invalidLink);
38+
} else {
39+
try {
40+
await updateLink(linkId, body);
41+
closeModal();
42+
toast.success(toastMessages.success.editLink);
43+
} catch (err) {
44+
toast.error(toastMessages.error.editLink);
45+
}
3046
}
31-
closeModal();
3247
};
3348
return (
3449
<ModalContainer title="링크 주소 변경">

components/modal/EditModal.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import ModalInput from "./modalComponents/ModalInput";
44
import useModalStore from "@/store/useModalStore";
55
import { putFolder } from "@/lib/api/folder";
66
import SubmitButton from "../SubMitButton";
7+
import toast from "react-hot-toast";
8+
import toastMessages from "@/lib/toastMessage";
79

810
const EditModal = ({
911
folderName,
@@ -23,12 +25,16 @@ const EditModal = ({
2325
const body = {
2426
name: value,
2527
};
26-
if (value !== "") {
28+
if (value === folderName) {
29+
toast.error(toastMessages.error.sameFolderName);
30+
} else if (value === "") {
31+
toast.error(toastMessages.error.inputFolderName);
32+
} else {
2733
try {
2834
await putFolder(folderId, body);
29-
console.log("폴더 수정 완료");
35+
toast.success(toastMessages.success.editFolder);
3036
} catch (error) {
31-
console.log(error);
37+
toast.error(toastMessages.error.editFolder);
3238
}
3339
}
3440
closeModal();

components/modal/modalComponents/ModalContainer.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IoIosClose } from "react-icons/io";
22
import { ModalPropType } from "@/types/modalTypes";
33
import useModalStore from "@/store/useModalStore";
4-
import { MouseEvent, useRef } from "react";
4+
import { MouseEvent, useEffect, useRef } from "react";
55

66
const ModalContainer = ({ title, subtitle, children }: ModalPropType) => {
77
const { isOpen, closeModal } = useModalStore();
@@ -10,11 +10,18 @@ const ModalContainer = ({ title, subtitle, children }: ModalPropType) => {
1010
if (modalRef.current && !modalRef.current.contains(e.target as Node))
1111
closeModal();
1212
};
13-
if (isOpen) {
14-
document.body.style.overflow = "hidden";
15-
} else {
16-
document.body.style.overflow = "auto";
17-
}
13+
14+
useEffect(() => {
15+
if (isOpen) {
16+
document.body.style.overflow = "hidden";
17+
} else {
18+
document.body.style.overflow = "auto";
19+
}
20+
21+
return () => {
22+
document.body.style.overflow = "auto";
23+
};
24+
}, [isOpen]);
1825
return (
1926
<div
2027
onClick={onClickBackDrop}

components/modal/modalManager/ModalManager.tsx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,7 @@ export const Modal = () => {
2828
case "AddFolderModal":
2929
return <AddFolderModal folderName={props.folderName || ""} />;
3030
case "AddModal":
31-
return (
32-
<AddModal
33-
list={
34-
props.list || [
35-
{ id: 1, name: "코딩팁", linkCount: 7, createdAt: "" },
36-
{ id: 2, name: "채용 사이트", linkCount: 7, createdAt: "" },
37-
{ id: 3, name: "유용한 글", linkCount: 7, createdAt: "" },
38-
{ id: 4, name: "나만의 장소", linkCount: 7, createdAt: "" },
39-
]
40-
}
41-
link={props.link || ""}
42-
/>
43-
);
31+
return <AddModal list={props.list || []} link={props.link || ""} />;
4432
case "DeleteFolderModal":
4533
return (
4634
<DeleteFolderModal

0 commit comments

Comments
 (0)