-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8cec864
commit 0459c2a
Showing
6 changed files
with
261 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ButtonBase, Menu, MenuItem } from "@mui/material"; | ||
import { MoreVertOutlined } from "@mui/icons-material"; | ||
import { useState } from "react"; | ||
|
||
const PostCommentDropdown = () => { | ||
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null); | ||
const open = Boolean(anchorEl); | ||
|
||
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
setAnchorEl(event.currentTarget); | ||
}; | ||
|
||
const handleClose = () => { | ||
setAnchorEl(null); | ||
}; | ||
|
||
return ( | ||
<> | ||
<ButtonBase aria-label="settings" sx={{ p: 0 }} onClick={handleClick}> | ||
<MoreVertOutlined /> | ||
</ButtonBase> | ||
<Menu open={open} anchorEl={anchorEl} onClose={handleClose}> | ||
<MenuItem | ||
onClick={() => { | ||
if (confirm("정말 삭제하시겠습니까?")) { | ||
console.log("눌림"); | ||
} | ||
}} | ||
> | ||
삭제 | ||
</MenuItem> | ||
<MenuItem>수정</MenuItem> | ||
</Menu> | ||
</> | ||
); | ||
}; | ||
|
||
export default PostCommentDropdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
"use client"; | ||
import { InputAdornment, Paper, TextField } from "@mui/material"; | ||
import { useCallback, useContext, useState } from "react"; | ||
import SubmitCommentIcon from "@/assets/icons/comment/SubmitCommentIcon.svg"; | ||
import { useGlobalNavbarVisibility } from "@/store/useGlobalNavbarVisibility"; | ||
import PostDetailPageContext from "@/store/post/PostDetailPageContext"; | ||
import useNewPostCommentMutation from "@/queries/post/comment/useNewPostCommentMutation"; | ||
|
||
const PostCommentInput = () => { | ||
const setIsShowingNavbar = useGlobalNavbarVisibility( | ||
({ setIsVisible }) => setIsVisible | ||
); | ||
const { data: currentData } = useContext(PostDetailPageContext); | ||
const [isEditing, setIsEditing] = useState(false); | ||
const [inputValue, setInputValue] = useState(""); | ||
|
||
const { mutateAsync: submitForm } = useNewPostCommentMutation( | ||
currentData?.postNo ? String(currentData?.postNo) : undefined | ||
); | ||
const submitHandler = useCallback( | ||
(content: string) => { | ||
submitForm(content).then(() => { | ||
setInputValue(""); | ||
}); | ||
}, | ||
[submitForm, setInputValue] | ||
); | ||
|
||
return ( | ||
<Paper | ||
sx={{ | ||
backgroundColor: "gray.primary", | ||
border: "1px solid", | ||
borderColor: "gray.secondary", | ||
position: "fixed", | ||
bottom: isEditing ? 0 : "44px", | ||
borderRadius: 1.5, | ||
left: 0, | ||
right: 0, | ||
p: 2, | ||
pb: isEditing ? 2 : 3.5, | ||
}} | ||
> | ||
<TextField | ||
fullWidth | ||
onFocus={() => { | ||
setIsShowingNavbar(false); | ||
setIsEditing(true); | ||
}} | ||
onBlur={() => { | ||
setIsShowingNavbar(true); | ||
setIsEditing(false); | ||
}} | ||
size="small" | ||
autoComplete="off" | ||
placeholder="회원님의 생각을 올려보세요" | ||
multiline | ||
value={inputValue} | ||
onChange={(e) => setInputValue(e.target.value)} | ||
rows={isEditing ? 5 : 1} | ||
InputProps={{ | ||
endAdornment: ( | ||
<InputAdornment | ||
position="end" | ||
onClick={(e) => { | ||
e.stopPropagation(); | ||
submitHandler(inputValue) | ||
}} | ||
sx={{ | ||
color: inputValue.length > 0 ? "primary.main" : "text.disabled", | ||
}} | ||
> | ||
<SubmitCommentIcon /> | ||
</InputAdornment> | ||
), | ||
}} | ||
sx={{ backgroundColor: "background.paper" }} | ||
/> | ||
</Paper> | ||
); | ||
}; | ||
|
||
export default PostCommentInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { POST_COMMENT } from "@/const/serverPath"; | ||
import axios from "@/libs/axios"; | ||
import PostCommentListInterface from "@/types/post/PostCommentInterface"; | ||
import { useSuspenseQuery } from "@tanstack/react-query"; | ||
|
||
interface CommentQueryInterface { | ||
postNo: string; | ||
} | ||
|
||
const useGetCommentQuery = ({ postNo }: CommentQueryInterface) => { | ||
return useSuspenseQuery({ | ||
queryKey: commentQueryKey.byId(postNo), | ||
queryFn: async () => await getCommentListQueryFn(postNo), | ||
}); | ||
}; | ||
|
||
export const getCommentListQueryFn = async ( | ||
id: CommentQueryInterface["postNo"] | ||
) => { | ||
const { data } = await axios.get<PostCommentListInterface>(POST_COMMENT(id)); | ||
return data; | ||
}; | ||
|
||
export const commentQueryKey = { | ||
all: ["comment"] as const, | ||
byId: (id?: string) => ["comment", { id }] as const, | ||
}; | ||
|
||
export default useGetCommentQuery; |
71 changes: 71 additions & 0 deletions
71
client/src/queries/post/comment/useNewPostCommentMutation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { POST_COMMENT } from "@/const/serverPath"; | ||
import useAxiosPrivate from "@/hooks/useAxiosPrivate"; | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import { commentQueryKey } from "./useGetCommentQuery"; | ||
import PostCommentListInterface from "@/types/post/PostCommentInterface"; | ||
import { useErrorHandler } from "@/utils/errorHandler"; | ||
import { useMyInfoQuery } from "@/queries/auth/useMyInfoQuery"; | ||
|
||
const useNewPostCommentMutation = (id?: string) => { | ||
const queryClient = useQueryClient(); | ||
const errorHandler = useErrorHandler(); | ||
const { data: myInfo } = useMyInfoQuery(); | ||
|
||
return useMutation({ | ||
mutationFn: async (content: string) => { | ||
if (!id) { | ||
return Promise.reject("id가 제공되지않음"); | ||
} else return postComment(id, content); | ||
}, | ||
onMutate: (content) => { | ||
queryClient.cancelQueries({ queryKey: commentQueryKey.byId(id) }); | ||
|
||
const querySnapShot = queryClient.getQueryData<PostCommentListInterface>( | ||
commentQueryKey.byId(id) | ||
); | ||
|
||
queryClient.setQueryData<PostCommentListInterface>( | ||
commentQueryKey.byId(id), | ||
(prev) => { | ||
return { | ||
list: [ | ||
{ | ||
commentNo: Number.MAX_SAFE_INTEGER, | ||
commentContent: content, | ||
createdDate: String(new Date()), | ||
lastModifiedDate: String(new Date()), | ||
createdBy: myInfo?.nickname, | ||
userId: myInfo?.id, | ||
nickname: myInfo?.nickname, | ||
profileImgUrls: myInfo?.profileImages, | ||
}, | ||
...(prev?.list ?? []), | ||
] as PostCommentListInterface["list"], | ||
totalCount: (prev?.totalCount ?? 0) + 1, | ||
}; | ||
} | ||
); | ||
return { querySnapShot }; | ||
}, | ||
onError: (err, queryFnParams, context) => { | ||
errorHandler(err); | ||
queryClient.setQueryData( | ||
commentQueryKey.byId(queryFnParams), | ||
context?.querySnapShot | ||
); | ||
}, | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: commentQueryKey.byId(id) }); | ||
}, | ||
}); | ||
}; | ||
export const postComment = async (postNo: string, content: string) => { | ||
const axiosPrivate = useAxiosPrivate(); | ||
const { data } = await axiosPrivate.post<{ commentNo: string }>( | ||
POST_COMMENT(postNo), | ||
{ commentContent: content } | ||
); | ||
return data; | ||
}; | ||
|
||
export default useNewPostCommentMutation; |