Skip to content

Commit

Permalink
[FEAT] 댓글 등록 중 유저 닉네임 노출 (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhsung23 authored Apr 5, 2024
1 parent c318d5b commit ec622ec
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/api/vote/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Pages } from '@/types/response';
import { UserInfo } from '@/types/user';
import { SelectionType, VoteReplyType, VoteType } from '@/types/vote';

export type GetVoteByIdRequest = {
Expand All @@ -21,7 +22,11 @@ export interface VoteFormData extends FormData {
append(name: 'voteRequestDto' | 'images', value: string | Blob): void;
}

export type PostVoteReplyRequest = { voteId: VoteType['id']; content: VoteReplyType['content'] };
export type PostVoteReplyRequest = {
voteId: VoteType['id'];
content: VoteReplyType['content'];
user: UserInfo;
};

export type DeleteVoteRequest = {
voteId: VoteType['id'];
Expand Down
6 changes: 3 additions & 3 deletions src/app/vote/[slug]/_component/Replies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Props = {
const Replies = ({ voteId }: Props) => {
const { data: user } = useGetUser();
const { data: replies } = useGetVoteReplies({ voteId });
const { mutateAsync: createVoteReplyAsync } = useCreateVoteReplyMutation();
const { mutate: createVoteReply } = useCreateVoteReplyMutation();
const { mutate: deleteVoteReply } = useDeleteVoteReplyMutation();
const { mutate: toggleLikeVoteReply } = useLikeVoteReplyMutation();

Expand Down Expand Up @@ -81,8 +81,8 @@ const Replies = ({ voteId }: Props) => {
</Suspense>

<ReplyInput
onSubmit={async (content) => {
await createVoteReplyAsync({ voteId, content });
onSubmit={(content) => {
createVoteReply({ voteId, content, user: user! });
scrollToBottom();
}}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/components/shared/replyInput/ReplyInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { replyReducer } from './helper/replyReducer';

type Props = {
initialInput?: string;
onSubmit: (content: string) => Promise<unknown>;
onSubmit: (content: string) => void;
};

const ReplyInput = ({ initialInput, onSubmit }: Props) => {
Expand Down
1 change: 1 addition & 0 deletions src/hooks/auth/useUpdateNicknameMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const useUpdateNicknameMutation = () => {
mutationFn: donworryApi.auth.patchNickname,
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: queryKey.auth.user, refetchType: 'all' });
queryClient.invalidateQueries({ queryKey: queryKey.vote.all, refetchType: 'all' });
toast({ message: 'CHANGE_NICKNAME_SUCCESS' });
},
onError: () => {
Expand Down
14 changes: 8 additions & 6 deletions src/hooks/vote/useCreateVoteReplyMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const useCreateVoteReplyMutation = () => {

return useMutation({
mutationFn: donworryApi.vote.postVoteReply,
onMutate: async ({ voteId, content }) => {
onMutate: async ({ voteId, content, user }) => {
await queryClient.cancelQueries({ queryKey: queryKey.vote.reply(voteId) });
const previousVoteReplies = queryClient.getQueryData<VoteReplyType[]>(
queryKey.vote.reply(voteId),
);
queryClient.setQueryData(queryKey.vote.reply(voteId), (oldVoteReplies: VoteReplyType[]) =>
getOptimisticUpdatedVoteRepliesData(oldVoteReplies, { voteId, content }),
getOptimisticUpdatedVoteRepliesData(oldVoteReplies, { voteId, content, user }),
);
return { previousVoteReplies, voteId };
},
Expand All @@ -29,24 +29,26 @@ const useCreateVoteReplyMutation = () => {
},
onSettled: (data, error, { voteId }) => {
queryClient.invalidateQueries({ queryKey: queryKey.vote.reply(voteId) });
},
onSuccess: () => {
toast({ message: 'REPLY_REGISTER_SUCCESS', above: 'input' });
},
});
};

const getOptimisticUpdatedVoteRepliesData = (
oldData: VoteReplyType[],
{ voteId, content }: PostVoteReplyRequest,
{ voteId, content, user }: PostVoteReplyRequest,
) => {
return produce(oldData, (draft) => {
draft.push({
voteId,
content,
commentId: -1,
userId: 0,
nickname: '등록중...', // TODO 사용자 닉네임
userId: user.userId,
nickname: user.nickname,
status: false,
avatar: '',
avatar: user.avatar,
likes: 0,
createdAt: Date.now().toString(),
modifiedAt: Date.now().toString(),
Expand Down

0 comments on commit ec622ec

Please sign in to comment.