Skip to content

Commit

Permalink
[#126] 코멘트 삭제 기능 (#130)
Browse files Browse the repository at this point in the history
* [#111] Feat: 코멘트 생성 시 새로고침 안 해도 화면에 렌더링 되도록 atom 추가

* [#126] Feat: login user id와 comment author id 비교해서 삭제 버튼 렌더링

* [#126] Feat: 코멘트 삭제 기능 구현
  • Loading branch information
deprecated-hongbiii committed Jun 30, 2021
1 parent a02d5f1 commit 3e9cd41
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
38 changes: 34 additions & 4 deletions fe/src/components/issue-detail/Comment.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { Box, Button } from '@material-ui/core';
import AuthorAvatar from 'components/common/AuthorAvatar';
import styled from 'styled-components';

import AuthorAvatar from 'components/common/AuthorAvatar';
import { ReactComponent as EditSvg } from 'icons/edit.svg';
import { ReactComponent as DeleteSvg } from 'icons/delete.svg';
import { ReactComponent as EmojiSvg } from 'icons/emoji.svg';
import { instanceWithAuth } from 'api';

import { CommentType } from 'types/issueType';
import { useRecoilValue } from 'recoil';
import { decodedUserDataAtom } from 'stores/userStore';
import { detailIssueAuthorIdAtom } from 'stores/detailIssueStore';
import { clickedIssueIdAtom } from 'stores/issueStore';
import {
commentUpdateAtom,
detailIssueAuthorIdAtom,
} from 'stores/detailIssueStore';

const Comment = ({ commentData }: { commentData: CommentType }) => {
const { id, description, createdTime, author } = commentData;
const issueAuthorId = useRecoilValue(detailIssueAuthorIdAtom);
const loginUser = useRecoilValue(decodedUserDataAtom);
const clickedIssueId = useRecoilValue(clickedIssueIdAtom);
const setCommentUpdate = useSetRecoilState(commentUpdateAtom);

const clickDeleteHandler = () => {
(async () => {
await instanceWithAuth.delete(
`${process.env.REACT_APP_API_URL}/api/issues/${clickedIssueId}/comments/${id}`
);
setCommentUpdate((cur) => ++cur);
})();
};

return (
<Box display="flex">
Expand All @@ -23,7 +42,7 @@ const Comment = ({ commentData }: { commentData: CommentType }) => {
<div className="comment-created-time">{createdTime}분 전</div>
</Box>
<Box display="flex" alignItems="center">
{issueAuthorId === id && (
{issueAuthorId === author.id && (
<IssueAuthorLabel
display="flex"
alignItems="center"
Expand All @@ -35,6 +54,15 @@ const Comment = ({ commentData }: { commentData: CommentType }) => {
{loginUser && loginUser.id === author.id && (
<Button startIcon={<EditIcon />}>편집</Button>
)}
{loginUser && loginUser.id === author.id && (
<Button
onClick={clickDeleteHandler}
startIcon={<DeleteIcon />}
color="secondary"
>
삭제
</Button>
)}
<EmojiButton>
<EmojiSvg />
</EmojiButton>
Expand Down Expand Up @@ -92,6 +120,8 @@ const IssueAuthorLabel = styled(Box)`

const EditIcon = styled(EditSvg)``;

const DeleteIcon = styled(DeleteSvg)``;

const EmojiButton = styled.button`
all: unset;
margin-left: 1rem;
Expand Down
8 changes: 6 additions & 2 deletions fe/src/components/issue-detail/IssueDetailBody.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { Box } from '@material-ui/core';
import styled from 'styled-components';
import { useRecoilState, useRecoilValue } from 'recoil';
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';

import AuthorAvatar from 'components/common/AuthorAvatar';
import CreateButton from 'components/buttons/CreateButton';
Expand All @@ -14,6 +14,7 @@ import { decodedUserDataAtom } from 'stores/userStore';
import {
commentDesctiptionAtom,
commentsQuery,
commentUpdateAtom,
detailIssueAuthorIdAtom,
issueDetailQuery,
} from 'stores/detailIssueStore';
Expand All @@ -28,6 +29,7 @@ const IssueDetailBody = () => {
const [commentDesctiption, setCommentDesctiption] = useRecoilState(
commentDesctiptionAtom
);
const setCommentUpdate = useSetRecoilState(commentUpdateAtom);

const issueDescription = {
// 코멘트처럼 생겼지만 사실 이슈의 본문
Expand All @@ -44,7 +46,7 @@ const IssueDetailBody = () => {
const newCommentHandler = () => {
const token = localStorage.getItem('jwt');
(async function () {
axios.post(
await axios.post(
`${process.env.REACT_APP_API_URL}/api/issues/${clickedIssueId}/comments`,
{
description: commentDesctiption,
Expand All @@ -55,6 +57,8 @@ const IssueDetailBody = () => {
},
}
);
setCommentUpdate((cur) => ++cur);
setCommentDesctiption('');
})();
};
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) =>
Expand Down
7 changes: 7 additions & 0 deletions fe/src/stores/detailIssueStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export const commentDesctiptionAtom = atom<string>({
default: '',
});

export const commentUpdateAtom = atom<number>({
key: 'commentUpdateAtom',
default: 0,
});

export const issueDetailQuery = selector<IssueDetailType>({
key: 'issueDetailQuery',
get: async ({ get }) => {
Expand Down Expand Up @@ -77,6 +82,8 @@ export const commentsQuery = selector({
get: async ({ get }) => {
const token = localStorage.getItem('jwt');
const clickedIssueId = get(clickedIssueIdAtom);
get(commentUpdateAtom);

try {
const { data } = await axios.get(
`${process.env.REACT_APP_API_URL}/api/issues/${clickedIssueId}/comments`,
Expand Down

0 comments on commit 3e9cd41

Please sign in to comment.