File tree Expand file tree Collapse file tree 1 file changed +10
-24
lines changed
Expand file tree Collapse file tree 1 file changed +10
-24
lines changed Original file line number Diff line number Diff line change 11"use client" ;
22
33import { useState , FormEvent } from "react" ;
4-
5- interface ChatApiResponse {
6- response : string ;
7- }
4+ import { askAI } from "@/app/actions/chatActions" ;
85
96export function ChatForm ( ) {
107 const [ inputValue , setInputValue ] = useState ( "" ) ;
@@ -17,29 +14,18 @@ export function ChatForm() {
1714 setIsLoading ( true ) ;
1815 setResponse ( "" ) ;
1916
20- try {
21- const res = await fetch ( "/api/chat" , {
22- method : "POST" ,
23- headers : {
24- "Content-Type" : "application/json" ,
25- } ,
26- body : JSON . stringify ( { message : inputValue } ) ,
27- } ) ;
17+ const formData = new FormData ( ) ;
18+ formData . append ( "message" , inputValue ) ;
19+
20+ const result = await askAI ( { response : "" , error : null } , formData ) ;
2821
29- const data = ( await res . json ( ) ) as ChatApiResponse ;
30- if ( ! res . ok ) {
31- throw new Error ( data . response || "エラーが発生しました。" ) ;
32- }
33- setResponse ( data . response ) ;
34- } catch ( error : unknown ) {
35- if ( error instanceof Error ) {
36- setResponse ( `エラー: ${ error . message } ` ) ;
22+ if ( result . error ) {
23+ setResponse ( `エラー: ${ result . error } ` ) ;
3724 } else {
38- setResponse ( `エラー: ${ String ( error ) } ` ) ;
39- }
40- } finally {
41- setIsLoading ( false ) ;
25+ setResponse ( result . response ) ;
4226 }
27+
28+ setIsLoading ( false ) ;
4329 } ;
4430 return (
4531 < >
You can’t perform that action at this time.
0 commit comments