Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion components/modal/DeleteFolderModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ const DeleteFolderModal = ({
}) => {
const { closeModal } = useModalStore();
const handleSubmit = async () => {
// 폴더 내에 링크 개수 0 일때만 삭제 가능 -> 링크 1개 이상이면 error toast 띄우겠습니다.
try {
await deleteFolder(folderId);
} catch (error) {
console.log(error, "폴더 삭제 에러");
console.log(error, "DeleteFolderModal 폴더 삭제 에러");
} finally {
closeModal();
}
Expand Down
30 changes: 17 additions & 13 deletions components/modal/EditModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,45 @@ import useModalStore from "@/store/useModalStore";
import { putFolder } from "@/lib/api/folder";
import SubmitButton from "../SubMitButton";

const EditModal = ({ folderName }: { folderName: string }) => {
const EditModal = ({
folderName,
folderId,
}: {
folderName: string;
folderId: number;
}) => {
const [value, setValue] = useState("");

const { closeModal } = useModalStore();

// 폴더 정보를 먼저 가져와야함

const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
setValue(e.target.value);
};
const handleSubmit = async () => {
const body = {
name: value,
};
// if (value !== "") {
// try {
// const res = await putFolder(folderId, body);
// console.log(res);
// } catch (error) {
// console.log(error);
// }
// }
if (value !== "") {
try {
await putFolder(folderId, body);
console.log("폴더 수정 완료");
} catch (error) {
console.log(error);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value가 없으면 모달이 닫히고 끝나는 건가요??
"값이 반드시 필요합니다" 와 같은 경고 toast가 뜨면 자연스러울 것 같습니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 요건 toast 브랜치에서 작업해서 따로 올릴게요!

closeModal();
};
return (
<ModalContainer title="폴더 이름 변경">
<ModalInput
placeholder="내용 입력"
placeholder={folderName}
name={folderName}
value={value}
onChange={handleChange}
/>
<SubmitButton
type="button"
// onClick={handleSubmit}
onClick={handleSubmit}
width="w-full"
height="h-[51px]"
color="positive"
Expand Down
7 changes: 6 additions & 1 deletion components/modal/modalManager/ModalManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ export const Modal = () => {
/>
);
case "EditModal":
return <EditModal folderName={props.folderName || "폴더이름"} />;
return (
<EditModal
folderName={props.folderName || "폴더이름"}
folderId={props.folderId || 1}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 설정하면 id가 중복되는 경우가 발생할 수 있는 것 아닌가요??

Copy link
Collaborator Author

@hongggyelim hongggyelim Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

folderId={Number(props.folderId)} 로 수정했습니다
이렇게 하면 값이 없을때는 undefined로 들어가도 에러 발생하지않네요

/>
);
case "SNSModal":
return <SNSModal folderName={props.folderName || "폴더이름"} />;
case "EditLink":
Expand Down
1 change: 1 addition & 0 deletions lib/api/folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const deleteFolder = async (folderId: number) => {
if (res.status >= 200 && res.status < 300) return res.data;
} catch (err) {
console.error("에러 메시지: ", err instanceof Error ? err.message : err);
throw err;
}
};

Expand Down
58 changes: 40 additions & 18 deletions pages/api/folders/[folderId]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,56 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const id = Number(folderId);

console.log("Token:", token);
console.log("folderId:", folderId);

if (!token) {
return res.status(401).json({ error: "사용자 정보를 찾을 수 없습니다." });
}

if (isNaN(id)) {
return res.status(400).json({ error: "Invalid folderId" });
return res.status(400).json({ error: "유효하지 않은 폴더 ID 입니다" });
}

if (req.method === "DELETE") {
try {
const response = await axiosInstance.delete(`/folders/${id}`, {
headers: {
Authorization: `Bearer ${token}`,
},
});
switch (req.method) {
case "DELETE":
try {
await axiosInstance.delete(`/folders/${id}`, {
headers: {
Authorization: `Bearer ${token}`,
},
});
return res.status(204).json({ message: "폴더 삭제 성공" });
} catch (error) {
if (axios.isAxiosError(error)) {
console.error("Axios Error:", error); // 여기서 error 객체 전체를 확인
if (error.response) {
const status = error.response.status;
const message =
error.response.data?.message || "알 수 없는 오류 발생";
console.error("Error Response Data:", error.response.data);
return res.status(status).json({ message: message });
}
console.error("Unknown Error:", error); // Axios 오류가 아닌 경우
throw error;
}
return res.status(500).json({ message: "서버 오류 발생" });
}

return res.status(204).json({ message: "폴더 삭제 성공" });
} catch (error) {
if (axios.isAxiosError(error) && error.response) {
const status = error.response.status;
const message = error.response.data?.message || "알 수 없는 오류 발생";
return res.status(status).json({ message });
case "PUT":
try {
await axiosInstance.put(`/folders/${id}`, req.body, {
headers: {
Authorization: `Bearer ${token}`,
},
});
return res.status(204).json({ message: "폴더 수정 성공" });
} catch (error) {
if (axios.isAxiosError(error) && error.response) {
const status = error.response.status;
const message =
error.response.data?.message || "알 수 없는 오류 발생";
return res.status(status).json({ message });
}
}
}
} else {
return res.status(500).json({ error: "서버 오류 발생" });
}
};
export default handler;
2 changes: 1 addition & 1 deletion pages/api/folders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
console.error(err);
return res
.status(500)
.json({ message: "모든 폴저 조회에 실패했습니다." });
.json({ message: "모든 폴더 조회에 실패했습니다." });
}

case "POST":
Expand Down
68 changes: 0 additions & 68 deletions pages/test/index.tsx

This file was deleted.

Loading