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
8 changes: 8 additions & 0 deletions frontend/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ const RECIPIENT = {
MEMBER: "MEMBER",
} as const;

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

export {
REGEX,
COLORS,
Expand All @@ -59,4 +66,5 @@ export {
KAKAO_OAUTH_URL,
GOOGLE_OAUTH_URL,
RECIPIENT,
ROLLINGPAPER_STATE_TYPE,
};
31 changes: 22 additions & 9 deletions frontend/src/pages/RollingpaperPage/components/LetterPaper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,60 @@ 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, RECIPIENT } 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"}
enableSecretMessage={recipientType === RECIPIENT.MEMBER}
onEditEnd={handleWriteEnd}
/>,
...elementList,
]
: [...elementList];
}, [messageList, isWrite]);
}, [rollingpaperState]);

const slicedMessageLists = useSliceMessageList(elementList);

return (
<StyledLetterPaper>
<StyledLetterPaperTop>
<StyledTo>To. {to}</StyledTo>
{!isWrite && (
{rollingpaperState !== ROLLINGPAPER_STATE_TYPE.WRITE && (
<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
11 changes: 9 additions & 2 deletions 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 useMessageBox = ({
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 All @@ -34,4 +41,4 @@ const useMessage = ({ id, rollingpaperId }: UseMessageProps) => {
};
};

export default useMessage;
export default useMessageBox;
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.LOADING);

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;
14 changes: 12 additions & 2 deletions frontend/src/pages/RollingpaperPage/hooks/useReadRollingpaper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@ import { GetRollingpaperResponse } from "@/types/apiResponse";
interface UseReadRollingpaperArgs {
teamId: number;
rollingpaperId: number;
handleWriteEnd: () => void;
}

export const useReadRollingpaper = ({
const useReadRollingpaper = ({
teamId,
rollingpaperId,
handleWriteEnd,
}: UseReadRollingpaperArgs) =>
useQuery<GetRollingpaperResponse, AxiosError>(
["rollingpaper", rollingpaperId],
() => getRollingpaper({ teamId, id: rollingpaperId }),
{ useErrorBoundary: true }
{
useErrorBoundary: true,
refetchOnWindowFocus: false,
onSuccess: () => {
handleWriteEnd();
},
}
);

export default useReadRollingpaper;
Loading