Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 새로운 메시지 저장 시 refetch 전까지 이전 메시지들을 보여주는 오류를 해결 #498

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
7 changes: 7 additions & 0 deletions frontend/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ const RECIPIENT = {
MEMBER: "MEMBER",
} as const;

const ROLLINGPAPER_STATE_TYPE = {
WRITE: "WRITE",
EDIT: "EDIT",
NORMAL: "NORMAL",
} as const;

export {
REGEX,
COLORS,
Expand All @@ -59,4 +65,5 @@ export {
KAKAO_OAUTH_URL,
GOOGLE_OAUTH_URL,
RECIPIENT,
ROLLINGPAPER_STATE_TYPE,
};
29 changes: 21 additions & 8 deletions frontend/src/pages/RollingpaperPage/components/LetterPaper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,43 @@ import PencilIcon from "@/assets/icons/bx-pencil.svg";

import MessageCreateForm from "@/pages/RollingpaperPage/components/MessageCreateForm";
import MessageBox from "@/pages/RollingpaperPage/components/MessageBox";
import useMessageWrite from "@/pages/RollingpaperPage/hooks/useMessageWrite";
import useSliceMessageList from "../hooks/useSliceMessageList";
import useSliceMessageList from "@/pages/RollingpaperPage/hooks/useSliceMessageList";

import { ROLLINGPAPER_STATE_TYPE } from "@/constants";

interface LetterPaperProp {
to: string;
recipientType: Recipient;
messageList: Message[];
rollingpaperState: string;
handleWriteButtonClick: () => void;
handleEditButtonClick: () => void;
handleWriteEnd: () => void;
}

const LetterPaper = ({ to, recipientType, messageList }: LetterPaperProp) => {
const { isWrite, handleWriteButtonClick, handleWriteEnd } = useMessageWrite();

const LetterPaper = ({
to,
recipientType,
messageList,
rollingpaperState,
handleWriteButtonClick,
handleEditButtonClick,
handleWriteEnd,
}: LetterPaperProp) => {
const elementList = useMemo(() => {
const elementList = messageList
.map((message) => (
<MessageBox
handleEditButtonClick={handleEditButtonClick}
handleWriteEnd={handleWriteEnd}
key={message.id}
recipientType={recipientType}
{...message}
/>
))
.reverse();

return isWrite
return rollingpaperState === ROLLINGPAPER_STATE_TYPE.WRITE
? [
<MessageCreateForm
enableSecretMessage={recipientType === "MEMBER"}
Expand All @@ -41,15 +54,15 @@ const LetterPaper = ({ to, recipientType, messageList }: LetterPaperProp) => {
...elementList,
]
: [...elementList];
}, [messageList, isWrite]);
}, [rollingpaperState]);

const slicedMessageLists = useSliceMessageList(elementList);

return (
<StyledLetterPaper>
<StyledLetterPaperTop>
<StyledTo>To. {to}</StyledTo>
{!isWrite && (
{rollingpaperState === ROLLINGPAPER_STATE_TYPE.NORMAL && (
<IconButton size="small" onClick={handleWriteButtonClick}>
<PencilIcon />
</IconButton>
Expand Down
18 changes: 15 additions & 3 deletions frontend/src/pages/RollingpaperPage/components/MessageBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import styled from "@emotion/styled";

import IconButton from "@/components/IconButton";

import useValidatedParam from "@/hooks/useValidatedParam";

import TrashIcon from "@/assets/icons/bx-trash.svg";
import Pencil from "@/assets/icons/bx-pencil.svg";
import LockIcon from "@/assets/icons/bx-lock-alt.svg";
Expand All @@ -12,7 +14,6 @@ import useMessageBox from "@/pages/RollingpaperPage/hooks/useMessageBox";
import SecretMessage from "@/pages/RollingpaperPage/components/SecretMessage";

import { Message, Recipient } from "@/types";
import useValidatedParam from "@/hooks/useValidatedParam";

const MessageBox = ({
id,
Expand All @@ -24,15 +25,25 @@ const MessageBox = ({
editable,
visible,
recipientType,
}: Message & { recipientType: Recipient }) => {
handleEditButtonClick,
handleWriteEnd,
}: Message & {
recipientType: Recipient;
handleEditButtonClick: () => void;
handleWriteEnd: () => void;
}) => {
const rollingpaperId = useValidatedParam<number>("rollingpaperId");

const {
isEdit,
handleWriteButtonClick,
handleDeleteButtonClick,
handleEditEnd,
} = useMessageBox({ id, rollingpaperId: rollingpaperId });
} = useMessageBox({
id,
rollingpaperId: rollingpaperId,
handleEditButtonClick,
});

if (!visible) {
return <SecretMessage from={from} />;
Expand All @@ -47,6 +58,7 @@ const MessageBox = ({
anonymous={anonymous}
secret={secret}
onEditEnd={handleEditEnd}
handleWriteEnd={handleWriteEnd}
enableSecretMessage={recipientType === "MEMBER"}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const MessageCreateForm = ({
handleColorClick,
handleAnonymousCheckBoxChange,
handleSecretCheckBoxChange,
initMessage,
} = useMessageForm({});

const rollingpaperId = useValidatedParam<number>("rollingpaperId");
Expand All @@ -38,13 +37,10 @@ export const MessageCreateForm = ({
anonymous,
secret: enableSecretMessage && secret,
});
initMessage();
onEditEnd();
};

const handleMessageCancel = () => {
if (confirm("메시지 작성을 취소하시겠습니까?")) {
initMessage();
onEditEnd();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Message } from "@/types";
type MessageUpdateFormProps = {
enableSecretMessage: boolean;
onEditEnd: () => void;
handleWriteEnd: () => void;
} & Pick<Message, "id" | "content" | "color" | "anonymous" | "secret">;

export const MessageUpdateForm = ({
Expand All @@ -20,6 +21,7 @@ export const MessageUpdateForm = ({
anonymous,
secret,
onEditEnd,
handleWriteEnd,
}: MessageUpdateFormProps) => {
const {
content: newContent,
Expand All @@ -30,7 +32,6 @@ export const MessageUpdateForm = ({
handleMessageChange,
handleAnonymousCheckBoxChange,
handleSecretCheckBoxChange,
initMessage,
} = useMessageForm({
initContent: content,
initColor: color,
Expand All @@ -47,13 +48,12 @@ export const MessageUpdateForm = ({
anonymous: newAnonymous,
secret: enableSecretMessage && newSecret,
});
initMessage();
onEditEnd();
};

const handleMessageCancel = () => {
if (confirm("메시지 작성을 취소하시겠습니까?")) {
initMessage();
handleWriteEnd();
onEditEnd();
}
};
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/pages/RollingpaperPage/hooks/useMessageBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ import useDeleteMessage from "@/pages/RollingpaperPage/hooks/useDeleteMessage";
interface UseMessageProps {
id: number;
rollingpaperId: number;
handleEditButtonClick: () => void;
}

const useMessage = ({ id, rollingpaperId }: UseMessageProps) => {
const useMessage = ({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

얘 이름이 useMessage네요. useMessageBox로 변경해야겠습니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

놓치고 있었는데 굿굿굿 e835b8d

id,
rollingpaperId,
handleEditButtonClick,
}: UseMessageProps) => {
const [isEdit, setIsEdit] = useState(false);
const { deleteRollingpaperMessage } = useDeleteMessage(rollingpaperId);

const handleWriteButtonClick: React.MouseEventHandler<
HTMLButtonElement
> = () => {
handleEditButtonClick();
setIsEdit(true);
};

const handleDeleteButtonClick = () => {
if (id) {
handleEditButtonClick();
deleteRollingpaperMessage(id);
}
};
Expand Down
6 changes: 0 additions & 6 deletions frontend/src/pages/RollingpaperPage/hooks/useMessageForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ const useMessageForm = ({
setColor(color);
};

const initMessage = () => {
setContent("");
setColor(INIT_COLOR);
};

return {
color,
content,
Expand All @@ -50,7 +45,6 @@ const useMessageForm = ({
handleColorClick,
handleAnonymousCheckBoxChange,
handleSecretCheckBoxChange,
initMessage,
};
};

Expand Down
22 changes: 18 additions & 4 deletions frontend/src/pages/RollingpaperPage/hooks/useMessageWrite.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import React, { useState } from "react";

import { ValueOf } from "@/types";
import { ROLLINGPAPER_STATE_TYPE } from "@/constants";

const useMessageWrite = () => {
const [isWrite, setIsWrite] = useState(false);
const [rollingpaperState, setRollingpaperState] = useState<
ValueOf<typeof ROLLINGPAPER_STATE_TYPE>
>(ROLLINGPAPER_STATE_TYPE.NORMAL);

const handleWriteButtonClick = () => {
setIsWrite(true);
setRollingpaperState(ROLLINGPAPER_STATE_TYPE.WRITE);
};

const handleEditButtonClick = () => {
setRollingpaperState(ROLLINGPAPER_STATE_TYPE.EDIT);
};

const handleWriteEnd = () => {
setIsWrite(false);
setRollingpaperState(ROLLINGPAPER_STATE_TYPE.NORMAL);
};

return { isWrite, handleWriteButtonClick, handleWriteEnd };
return {
rollingpaperState,
handleWriteButtonClick,
handleEditButtonClick,
handleWriteEnd,
};
};

export default useMessageWrite;
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ import { GetRollingpaperResponse } from "@/types/apiResponse";
interface UseReadRollingpaperArgs {
teamId: number;
rollingpaperId: number;
handleWriteEnd: () => void;
}

export const useReadRollingpaper = ({
teamId,
rollingpaperId,
handleWriteEnd,
}: UseReadRollingpaperArgs) =>
useQuery<GetRollingpaperResponse, AxiosError>(
["rollingpaper", rollingpaperId],
() => getRollingpaper({ teamId, id: rollingpaperId }),
{ useErrorBoundary: true }
{
useErrorBoundary: true,
refetchOnWindowFocus: false,
onSuccess: () => {
handleWriteEnd();
},
}
);
12 changes: 12 additions & 0 deletions frontend/src/pages/RollingpaperPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ import LetterPaper from "@/pages/RollingpaperPage/components/LetterPaper";

import useValidatedParam from "@/hooks/useValidatedParam";
import { useReadRollingpaper } from "@/pages/RollingpaperPage/hooks/useReadRollingpaper";
import useMessageWrite from "./hooks/useMessageWrite";

const RollingpaperPage = () => {
const teamId = useValidatedParam<number>("teamId");
const rollingpaperId = useValidatedParam<number>("rollingpaperId");
const {
rollingpaperState,
handleWriteButtonClick,
handleWriteEnd,
handleEditButtonClick,
} = useMessageWrite();
Comment on lines +21 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 친구는 LetterPaper 내로 들어가도 되겠네요. 꼭 RollinpaperPage에 있어야 할 이유가 없는 것 같은데, 한번 확인해주세요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleWriteEnd 때문에 상단에서 다 관리하자! 라는 생각으로 구현한 것 같아요. 이 훅도 그렇고 전반적으로 LetterPaper 컴포넌트를 따로 두는 것보다 index 내부에서 전부 처리해주는 것이 prop drilling도 적게 일어나고 코드를 알아보기 좋을 것 같은데... 어떻게하면 하나의 컴포넌트에 다 데려올 수 있을지 고민해봐야겠어요 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그럼 일단은 index.tsx에서 useMessageWrite를 사용하는 방식을 유지하는 걸까요?
그대로 index.tsx에 있어서 물어봅니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네네 이거를 옮길려고 하니까 복잡해지는 것 같더라고요. 밑에 코멘트 준 대로 context를 도입해야하나 생각이 들기도 하고 구조를 좀 바꾸면서 위치도 옮겨주는게 좋을 것 같아요!


const { isLoading, data: rollingpaper } = useReadRollingpaper({
teamId,
rollingpaperId,
handleWriteEnd,
});

if (isLoading) {
Expand All @@ -26,9 +34,13 @@ const RollingpaperPage = () => {
<PageTitleWithBackButton>{rollingpaper.title}</PageTitleWithBackButton>
<main>
<LetterPaper
rollingpaperState={rollingpaperState}
to={rollingpaper.to}
recipientType={rollingpaper.recipient}
messageList={[...rollingpaper.messages]}
handleWriteButtonClick={handleWriteButtonClick}
handleEditButtonClick={handleEditButtonClick}
handleWriteEnd={handleWriteEnd}
/>
</main>
</>
Expand Down