Skip to content

Commit ca1965c

Browse files
authored
Merge pull request #155 from Team-INSERT/feat/post
리스트페이지에서 글쓰기페이지로 넘어갈 때 선택한 카테고리 유지되게
2 parents d0fd89b + cf22f36 commit ca1965c

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { atom } from "jotai";
2+
import { PostCategoryType } from "../types";
3+
import { CATEGORY } from "../constants";
4+
5+
const currentCategoryContext = atom<PostCategoryType>(CATEGORY.COMMON);
6+
7+
export default currentCategoryContext;

src/templates/post/context/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as currentCategoryContext } from "./currentCategory.context";

src/templates/post/hooks/usePost.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import { useRouter } from "next/navigation";
33
import dayjs from "dayjs";
44
import React from "react";
55
import Swal from "sweetalert2";
6+
import { useAtom } from "jotai";
67
import { useUser } from "@/@user/hooks";
78
import { KEY, ROUTER } from "@/constants";
8-
import { CATEGORY } from "../constants";
99
import { defaultPostData } from "../assets/data";
1010
import { useDeletePostMutation } from "../services/post/mutation.service";
1111
import { Post, PostCategoryType } from "../types";
12+
import { currentCategoryContext } from "../context";
1213

1314
const usePost = (defaultPostDataState?: Post) => {
1415
const [postData, setPostData] = React.useState<Post>(defaultPostData);
15-
const [currentCategory, setCurrentCategory] =
16-
React.useState<PostCategoryType>(CATEGORY.COMMON);
16+
const [currentCategory, setCurrentCategory] = useAtom(currentCategoryContext);
1717
const { mutate: deletePostMutate } = useDeletePostMutation();
1818
const { user } = useUser();
1919

src/templates/post/hooks/usePostWritable.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
import React from "react";
2+
import { useAtom } from "jotai";
23
import { useImageUpload } from "@/hooks";
4+
import { useUser } from "@/@user/hooks";
35
import { getFilteredPostDataByCategory, getPostIsValid } from "../helpers";
46
import {
57
useCreatePostMutation,
68
useUpdatePostMutation,
79
} from "../services/post/mutation.service";
810
import { Post, PostCategoryType, PostData } from "../types";
911
import { defaultPostData } from "../assets/data";
12+
import { currentCategoryContext } from "../context";
1013

1114
// edit과 write를 동시에 처리하는 훅
1215
const usePostWritable = (defaultPostDataState?: Post) => {
13-
const [postData, setPostData] = React.useState<Post>(defaultPostData);
16+
const { isAdmin } = useUser();
17+
const [currentCategory] = useAtom(currentCategoryContext);
18+
19+
const is유저가공지사항접근 = !isAdmin && currentCategory === "NOTICE";
20+
21+
const [postData, setPostData] = React.useState<Post>({
22+
...defaultPostData,
23+
category: is유저가공지사항접근 ? "COMMON" : currentCategory,
24+
});
1425
const [lostImageUrl, setLostImageUrl] = React.useState();
1526
const { mutate: updatePostMutate } = useUpdatePostMutation();
1627
const { mutate: createPostMutate } = useCreatePostMutation();

0 commit comments

Comments
 (0)