feat(frontend): 채팅 페이지 API 연동 + KST 시각 표시
- apis/chat 모듈(api/queries/mutations/keys/type) + useChatController(append-only send 주입) - useChatStore 재배선, list→chat session_id 라우팅 연결, mock 제거 - 마감/종료/권한 시 입력 잠금·목록 복귀, 가격범위 인라인 에러, CHAT_IN_PROGRESS 처리 - list 마감일 KST(Asia/Seoul) 고정 표시(lib/datetime formatKstDateTime) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
dcce82a630
commit
ce536b43d6
19 files changed
+406
-184
No files matched your search
@@ -0,0 +1,33 @@
|
||||
// 채팅 엔드포인트 호출 함수 (순수 HTTP 레이어, React 의존 없음).
|
||||
import { http } from '@/apis/http'
|
||||
import type {
|
||||
ChatInitResponse,
|
||||
ChatMessagesResponse,
|
||||
ChatSendRequest,
|
||||
ChatSendResponse,
|
||||
} from './chat.type'
|
||||
|
||||
export const chatApi = {
|
||||
/** GET .../chat/init — 상품·견적 메타 + 세션 상태 + 마감 시각 */
|
||||
getInit: async (sessionId: string): Promise<ChatInitResponse> => {
|
||||
const res = await http.get<ChatInitResponse>(`/v1/negotiation/sessions/${sessionId}/chat/init`)
|
||||
return res.data
|
||||
},
|
||||
|
||||
/** GET .../chat/messages — 대화 히스토리(seq 오름차순). 비어 있으면 오프닝 포함 */
|
||||
getMessages: async (sessionId: string): Promise<ChatMessagesResponse> => {
|
||||
const res = await http.get<ChatMessagesResponse>(
|
||||
`/v1/negotiation/sessions/${sessionId}/chat/messages`,
|
||||
)
|
||||
return res.data
|
||||
},
|
||||
|
||||
/** POST .../chat/send — 한 턴 전송, 새 봇 메시지 1건 반환(append-only) */
|
||||
send: async (sessionId: string, body: ChatSendRequest): Promise<ChatSendResponse> => {
|
||||
const res = await http.post<ChatSendResponse>(
|
||||
`/v1/negotiation/sessions/${sessionId}/chat/send`,
|
||||
body,
|
||||
)
|
||||
return res.data
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// 채팅 도메인의 TanStack Query 키 팩토리.
|
||||
export const chatKeys = {
|
||||
all: ['chat'] as const,
|
||||
init: (sessionId: string) => [...chatKeys.all, 'init', sessionId] as const,
|
||||
messages: (sessionId: string) => [...chatKeys.all, 'messages', sessionId] as const,
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// 채팅 도메인의 변경(useMutation) 훅.
|
||||
import { useMutation } from '@tanstack/react-query'
|
||||
import { chatApi } from './chat.api'
|
||||
import type { ChatSendRequest } from './chat.type'
|
||||
|
||||
/**
|
||||
* 협상 한 턴 전송. append-only 라 캐시 무효화/refetch 를 하지 않는다.
|
||||
* 응답의 새 봇 메시지는 호출부(컨트롤러)가 스토어에 직접 append 한다.
|
||||
*/
|
||||
export function useChatSendMutation(sessionId: string) {
|
||||
return useMutation({
|
||||
mutationFn: (body: ChatSendRequest) => chatApi.send(sessionId, body),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// 채팅 도메인의 조회(useQuery) 훅.
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { chatApi } from './chat.api'
|
||||
import { chatKeys } from './chat.keys'
|
||||
import { mapInit, mapMessage } from './chat.type'
|
||||
|
||||
/** 채팅 진입 메타(상품·견적). 마감 시각은 거의 불변이라 오래 캐싱한다. */
|
||||
export function useChatInitQuery(sessionId: string) {
|
||||
return useQuery({
|
||||
queryKey: chatKeys.init(sessionId),
|
||||
queryFn: () => chatApi.getInit(sessionId).then(mapInit),
|
||||
enabled: !!sessionId,
|
||||
staleTime: 5 * 60 * 1000,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 대화 히스토리. 진입 시 1회만 받고 이후엔 send 응답을 로컬에 append 한다(append-only).
|
||||
* 따라서 백그라운드 refetch 가 로컬 상태를 덮지 않도록 staleTime 을 무한대로 둔다.
|
||||
*/
|
||||
export function useChatMessagesQuery(sessionId: string) {
|
||||
return useQuery({
|
||||
queryKey: chatKeys.messages(sessionId),
|
||||
queryFn: () => chatApi.getMessages(sessionId).then((r) => r.items.map(mapMessage)),
|
||||
enabled: !!sessionId,
|
||||
staleTime: Infinity,
|
||||
refetchOnWindowFocus: false,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
// 채팅 API 의 와이어 타입(백엔드 snake_case 미러) + feature 타입 매퍼.
|
||||
// sender 는 정수 코드(1=BOT, 2=USER)로 내려오고 프론트에서 'bot'|'user' 로 매핑한다.
|
||||
import type { ApiResult } from '@/apis/types'
|
||||
import type {
|
||||
ChatInitData,
|
||||
ChatMessage,
|
||||
NextInputMode,
|
||||
UserInputType,
|
||||
} from '@/features/chat/types'
|
||||
|
||||
// RemoveNoneResponse 로 null 필드는 생략될 수 있어 대부분 optional.
|
||||
export interface ChatMessageWire {
|
||||
chat_id: string
|
||||
session_id: string
|
||||
seq: number
|
||||
sender: number
|
||||
script?: string
|
||||
user_input_type?: string | null
|
||||
step?: string
|
||||
display_step?: string
|
||||
next_input_mode?: string | null
|
||||
next_input_type?: string[] | null
|
||||
chat_end?: boolean
|
||||
indicator_value?: number | null
|
||||
bot_chat_type?: string | null
|
||||
}
|
||||
|
||||
export interface ChatInitResponse {
|
||||
result: ApiResult
|
||||
session_id: string
|
||||
session_status: number
|
||||
quotation_id: string
|
||||
quotation_end_time: string
|
||||
quotation_memo?: string
|
||||
item_id: string
|
||||
item_name: string
|
||||
item_code?: string
|
||||
item_image?: string
|
||||
item_price: number
|
||||
item_model_name?: string
|
||||
item_maker_name?: string
|
||||
item_spec?: string
|
||||
item_lead_time?: string
|
||||
item_min_order_quantity?: string
|
||||
item_vat_yn?: boolean
|
||||
item_delivery_fee_yn?: boolean
|
||||
}
|
||||
|
||||
export interface ChatMessagesResponse {
|
||||
result: ApiResult
|
||||
items: ChatMessageWire[]
|
||||
}
|
||||
|
||||
export interface ChatSendRequest {
|
||||
user_input: string
|
||||
user_input_type?: string | null
|
||||
}
|
||||
|
||||
export interface ChatSendResponse {
|
||||
result: ApiResult
|
||||
message?: ChatMessageWire
|
||||
session_status: number
|
||||
}
|
||||
|
||||
// --- 매퍼: 와이어 → feature 타입 --------------------------------------------
|
||||
export function mapMessage(w: ChatMessageWire): ChatMessage {
|
||||
return {
|
||||
chat_id: w.chat_id,
|
||||
sender: w.sender === 1 ? 'bot' : 'user',
|
||||
bot_chat_type: (w.bot_chat_type ?? null) as ChatMessage['bot_chat_type'],
|
||||
user_input_type: (w.user_input_type ?? null) as UserInputType | null,
|
||||
script: w.script ?? null,
|
||||
chat_end: w.chat_end ?? false,
|
||||
next_input_mode: (w.next_input_mode ?? null) as NextInputMode | null,
|
||||
next_input_type: w.next_input_type ?? null,
|
||||
step: w.step ?? '',
|
||||
display_step: w.display_step ?? '',
|
||||
summary: null, // (범위 외) 최종 요약 카드는 추후
|
||||
indicator_value: w.indicator_value ?? null,
|
||||
}
|
||||
}
|
||||
|
||||
export function mapInit(r: ChatInitResponse): ChatInitData {
|
||||
return {
|
||||
session_id: r.session_id,
|
||||
item_id: r.item_id,
|
||||
quotation_id: r.quotation_id,
|
||||
item_name: r.item_name,
|
||||
item_code: r.item_code ?? '',
|
||||
item_image: r.item_image ?? '',
|
||||
item_price: r.item_price ?? 0,
|
||||
item_model_name: r.item_model_name ?? '',
|
||||
item_maker_name: r.item_maker_name ?? '',
|
||||
item_vat_yn: r.item_vat_yn == null ? '' : r.item_vat_yn ? 'VAT포함' : 'VAT별도',
|
||||
item_delivery_fee_yn:
|
||||
r.item_delivery_fee_yn == null ? '' : r.item_delivery_fee_yn ? '배송비포함' : '배송비별도',
|
||||
item_min_order_quantity: r.item_min_order_quantity ?? '',
|
||||
item_lead_time: r.item_lead_time ?? '',
|
||||
item_spec: r.item_spec ?? '',
|
||||
quotation_memo: r.quotation_memo ?? '',
|
||||
quotation_end_time: r.quotation_end_time ?? '',
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// 채팅 도메인 API 공개 표면.
|
||||
export { chatApi } from './chat.api'
|
||||
export { chatKeys } from './chat.keys'
|
||||
export { useChatInitQuery, useChatMessagesQuery } from './chat.queries'
|
||||
export { useChatSendMutation } from './chat.mutations'
|
||||
export { mapInit, mapMessage } from './chat.type'
|
||||
export type {
|
||||
ChatInitResponse,
|
||||
ChatMessagesResponse,
|
||||
ChatMessageWire,
|
||||
ChatSendRequest,
|
||||
ChatSendResponse,
|
||||
} from './chat.type'
|
||||
Reference in new issue
Block a user