|
1 | | -import { useMemo, useState } from "react"; |
| 1 | +import { useEffect, useMemo, useRef, useState } from "react"; |
2 | 2 | import { useLocation, useNavigate } from "react-router-dom"; |
3 | 3 | import { useQuery } from "@tanstack/react-query"; |
4 | 4 | import Header from "@/layout/Header"; |
5 | 5 | import ManagerNavbar from "@/layout/ManagerNavbar"; |
6 | 6 | import RightChevronIcon from "@/assets/icon_right-chevron.svg"; |
7 | 7 | import LeftChevronIcon from "@/assets/icon_left-chevron.svg"; |
8 | 8 | import { getMonthlyReservations } from "@/apis/manager_home/home"; |
| 9 | +import { getUserInfo } from "@/apis/mypage/mypage"; |
| 10 | +import axios from "axios"; |
9 | 11 |
|
10 | 12 | type FilterKey = "pending" | "completed" | "cancelled"; |
11 | 13 |
|
@@ -119,6 +121,67 @@ export default function TodaysFilteredReservationsPage() { |
119 | 121 |
|
120 | 122 | const fmt = (t?: string) => (t ?? "").slice(0, 5); |
121 | 123 |
|
| 124 | + const userIdRef = useRef<number | null>(null); |
| 125 | + const shopIdRef = useRef<number | null>(null); |
| 126 | + |
| 127 | + useEffect(() => { |
| 128 | + let cancelled = false; |
| 129 | + (async () => { |
| 130 | + try { |
| 131 | + const userInfo = await getUserInfo(); |
| 132 | + if (!cancelled) { |
| 133 | + userIdRef.current = userInfo.id; |
| 134 | + shopIdRef.current = userInfo.shopMembers?.[0]?.shopId ?? null; |
| 135 | + if (shopIdRef.current) { |
| 136 | + localStorage.setItem("shopId", String(shopIdRef.current)); |
| 137 | + } |
| 138 | + } |
| 139 | + } catch (e) { |
| 140 | + console.error("failed to load user info", e); |
| 141 | + } |
| 142 | + })(); |
| 143 | + return () => { |
| 144 | + cancelled = true; |
| 145 | + }; |
| 146 | + }, []); |
| 147 | + |
| 148 | + // 채팅방 입장 |
| 149 | + const handleCreateRoom = async (customerId: number, customerName: string) => { |
| 150 | + try { |
| 151 | + const token = localStorage.getItem("accessToken"); |
| 152 | + const shopId = shopIdRef.current; |
| 153 | + const designerId = userIdRef.current; |
| 154 | + |
| 155 | + if (!token || !designerId || !shopId) { |
| 156 | + console.error("정보가 부족합니다."); |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + const response = await axios.post( |
| 161 | + `${import.meta.env.VITE_API_BASE_URL}/chat/rooms`, |
| 162 | + { shopId, customerId, designerId }, |
| 163 | + { headers: { Authorization: `Bearer ${token}` } }, |
| 164 | + ); |
| 165 | + |
| 166 | + const roomId = response.data.data.roomId; |
| 167 | + navigate(`/chat/rooms/${roomId}`, { |
| 168 | + state: { |
| 169 | + customerId, |
| 170 | + name: customerName, |
| 171 | + }, |
| 172 | + }); |
| 173 | + } catch (error) { |
| 174 | + if (axios.isAxiosError(error)) { |
| 175 | + console.error("채팅방 생성 실패", { |
| 176 | + message: error.message, |
| 177 | + response: error.response, |
| 178 | + status: error.response?.status, |
| 179 | + data: error.response?.data, |
| 180 | + }); |
| 181 | + } |
| 182 | + } |
| 183 | + }; |
| 184 | + |
122 | 185 | return ( |
123 | 186 | <div className="mx-auto min-h-screen max-w-[375px] bg-[var(--color-grey-1000)] pb-24 text-[var(--color-grey-150)]"> |
124 | 187 | <Header /> |
@@ -164,6 +227,9 @@ export default function TodaysFilteredReservationsPage() { |
164 | 227 | onClick={() => |
165 | 228 | navigate(`/manager/reservations/${item.reservationId}`) |
166 | 229 | } |
| 230 | + onChat={() => |
| 231 | + handleCreateRoom(item.customerId, item.customerName) |
| 232 | + } |
167 | 233 | /> |
168 | 234 | ))} |
169 | 235 | {list.length === 0 && ( |
|
0 commit comments