| 
 | 1 | +import { useChatMessagesContext } from "@/chat/hooks/useChatMessagesStore";  | 
 | 2 | +import { useSendChatMessage } from "@/chat/hooks/useSendChatMesasge";  | 
 | 3 | +import { delay } from "@/shared/utils/delay";  | 
 | 4 | +import { useParams } from "next/navigation";  | 
 | 5 | +import { css } from "styled-components";  | 
 | 6 | +import ChipButton from "./ChipButton";  | 
 | 7 | + | 
 | 8 | +type Props = {  | 
 | 9 | +  open: boolean;  | 
 | 10 | +};  | 
 | 11 | + | 
 | 12 | +export default function AcceptRejectButtons({ open }: Props) {  | 
 | 13 | +  const { addMessage, deleteMessage } = useChatMessagesContext();  | 
 | 14 | +  const { mutate: sendChatMessage, isPending: isSendingChatMessage } = useSendChatMessage();  | 
 | 15 | +  const { chatId } = useParams<{ chatId: string }>();  | 
 | 16 | + | 
 | 17 | +  const rejectMessage = "아니, 얘기 더 들어봐";  | 
 | 18 | +  const acceptMessage = "좋아! 타로 볼래";  | 
 | 19 | + | 
 | 20 | +  if (!chatId) throw new Error("chatId가 Dynamic Route에서 전달 되어야 합니다.");  | 
 | 21 | + | 
 | 22 | +  const handleAcceptClick = async () => {  | 
 | 23 | +    addMessage({  | 
 | 24 | +      messageId: Math.random(),  | 
 | 25 | +      type: "USER_NORMAL",  | 
 | 26 | +      sender: "USER",  | 
 | 27 | +      answers: [acceptMessage],  | 
 | 28 | +    });  | 
 | 29 | + | 
 | 30 | +    await delay(500);  | 
 | 31 | + | 
 | 32 | +    const loadingMessageId = Math.random();  | 
 | 33 | + | 
 | 34 | +    addMessage({  | 
 | 35 | +      messageId: loadingMessageId,  | 
 | 36 | +      type: "SYSTEM_NORMAL_REPLY",  | 
 | 37 | +      sender: "SYSTEM",  | 
 | 38 | +      loading: true,  | 
 | 39 | +      answers: [],  | 
 | 40 | +    });  | 
 | 41 | + | 
 | 42 | +    sendChatMessage(  | 
 | 43 | +      {  | 
 | 44 | +        roomId: Number(chatId),  | 
 | 45 | +        message: acceptMessage,  | 
 | 46 | +        intent: "TAROT_ACCEPT",  | 
 | 47 | +      },  | 
 | 48 | +      {  | 
 | 49 | +        onSuccess: (data) => {  | 
 | 50 | +          deleteMessage(loadingMessageId);  | 
 | 51 | + | 
 | 52 | +          addMessage({  | 
 | 53 | +            messageId: data.messageId,  | 
 | 54 | +            type: data.type,  | 
 | 55 | +            sender: data.sender,  | 
 | 56 | +            answers: data.answers,  | 
 | 57 | +          });  | 
 | 58 | +        },  | 
 | 59 | +      }  | 
 | 60 | +    );  | 
 | 61 | +  };  | 
 | 62 | + | 
 | 63 | +  const handleRejectClick = async () => {  | 
 | 64 | +    addMessage({  | 
 | 65 | +      messageId: Math.random(),  | 
 | 66 | +      type: "USER_NORMAL",  | 
 | 67 | +      sender: "USER",  | 
 | 68 | +      answers: [rejectMessage],  | 
 | 69 | +    });  | 
 | 70 | + | 
 | 71 | +    await delay(500);  | 
 | 72 | + | 
 | 73 | +    const loadingMessageId = Math.random();  | 
 | 74 | + | 
 | 75 | +    addMessage({  | 
 | 76 | +      messageId: loadingMessageId,  | 
 | 77 | +      type: "SYSTEM_NORMAL_REPLY",  | 
 | 78 | +      sender: "SYSTEM",  | 
 | 79 | +      loading: true,  | 
 | 80 | +      answers: [],  | 
 | 81 | +    });  | 
 | 82 | + | 
 | 83 | +    sendChatMessage(  | 
 | 84 | +      {  | 
 | 85 | +        roomId: Number(chatId),  | 
 | 86 | +        message: rejectMessage,  | 
 | 87 | +        intent: "TAROT_DECLINE",  | 
 | 88 | +      },  | 
 | 89 | +      {  | 
 | 90 | +        onSuccess: (data) => {  | 
 | 91 | +          deleteMessage(loadingMessageId);  | 
 | 92 | + | 
 | 93 | +          addMessage({  | 
 | 94 | +            messageId: data.messageId,  | 
 | 95 | +            type: data.type,  | 
 | 96 | +            sender: data.sender,  | 
 | 97 | +            answers: data.answers,  | 
 | 98 | +          });  | 
 | 99 | +        },  | 
 | 100 | +      }  | 
 | 101 | +    );  | 
 | 102 | +  };  | 
 | 103 | + | 
 | 104 | +  if (!open) return null;  | 
 | 105 | + | 
 | 106 | +  return (  | 
 | 107 | +    <div  | 
 | 108 | +      css={css`  | 
 | 109 | +        display: flex;  | 
 | 110 | +        gap: 8px;  | 
 | 111 | +        margin-top: 76px;  | 
 | 112 | +      `}  | 
 | 113 | +    >  | 
 | 114 | +      <ChipButton type="button" disabled={isSendingChatMessage} color="primary02" onClick={handleAcceptClick}>  | 
 | 115 | +        {acceptMessage}  | 
 | 116 | +      </ChipButton>  | 
 | 117 | +      <ChipButton type="button" disabled={isSendingChatMessage} color="grey30" onClick={handleRejectClick}>  | 
 | 118 | +        {rejectMessage}  | 
 | 119 | +      </ChipButton>  | 
 | 120 | +    </div>  | 
 | 121 | +  );  | 
 | 122 | +}  | 
0 commit comments