Skip to content

Commit

Permalink
Merge pull request #155 from Team-INSERT/feat/post
Browse files Browse the repository at this point in the history
리스트페이지에서 글쓰기페이지로 넘어갈 때 선택한 카테고리 유지되게
  • Loading branch information
jyh071116 authored Dec 27, 2023
2 parents d0fd89b + cf22f36 commit ca1965c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/templates/post/context/currentCategory.context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { atom } from "jotai";
import { PostCategoryType } from "../types";
import { CATEGORY } from "../constants";

const currentCategoryContext = atom<PostCategoryType>(CATEGORY.COMMON);

export default currentCategoryContext;
1 change: 1 addition & 0 deletions src/templates/post/context/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as currentCategoryContext } from "./currentCategory.context";
6 changes: 3 additions & 3 deletions src/templates/post/hooks/usePost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { useRouter } from "next/navigation";
import dayjs from "dayjs";
import React from "react";
import Swal from "sweetalert2";
import { useAtom } from "jotai";
import { useUser } from "@/@user/hooks";
import { KEY, ROUTER } from "@/constants";
import { CATEGORY } from "../constants";
import { defaultPostData } from "../assets/data";
import { useDeletePostMutation } from "../services/post/mutation.service";
import { Post, PostCategoryType } from "../types";
import { currentCategoryContext } from "../context";

const usePost = (defaultPostDataState?: Post) => {
const [postData, setPostData] = React.useState<Post>(defaultPostData);
const [currentCategory, setCurrentCategory] =
React.useState<PostCategoryType>(CATEGORY.COMMON);
const [currentCategory, setCurrentCategory] = useAtom(currentCategoryContext);
const { mutate: deletePostMutate } = useDeletePostMutation();
const { user } = useUser();

Expand Down
13 changes: 12 additions & 1 deletion src/templates/post/hooks/usePostWritable.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import React from "react";
import { useAtom } from "jotai";
import { useImageUpload } from "@/hooks";
import { useUser } from "@/@user/hooks";
import { getFilteredPostDataByCategory, getPostIsValid } from "../helpers";
import {
useCreatePostMutation,
useUpdatePostMutation,
} from "../services/post/mutation.service";
import { Post, PostCategoryType, PostData } from "../types";
import { defaultPostData } from "../assets/data";
import { currentCategoryContext } from "../context";

// edit과 write를 동시에 처리하는 훅
const usePostWritable = (defaultPostDataState?: Post) => {
const [postData, setPostData] = React.useState<Post>(defaultPostData);
const { isAdmin } = useUser();
const [currentCategory] = useAtom(currentCategoryContext);

const is유저가공지사항접근 = !isAdmin && currentCategory === "NOTICE";

const [postData, setPostData] = React.useState<Post>({
...defaultPostData,
category: is유저가공지사항접근 ? "COMMON" : currentCategory,
});
const [lostImageUrl, setLostImageUrl] = React.useState();
const { mutate: updatePostMutate } = useUpdatePostMutation();
const { mutate: createPostMutate } = useCreatePostMutation();
Expand Down

0 comments on commit ca1965c

Please sign in to comment.