-
Notifications
You must be signed in to change notification settings - Fork 1
チャットフォームのクライアント側とサーバー側APIを連携 #7
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,112 +1,156 @@ | ||
| "use client"; | ||
|
|
||
| import { hello } from "./chatServer"; | ||
| import { useState, FormEvent } from "react"; | ||
|
|
||
| export function ChatForm() {return ( | ||
| interface ChatApiResponse { | ||
| response: string; | ||
| } | ||
|
|
||
| export function ChatForm() { | ||
| const [inputValue, setInputValue] = useState(""); | ||
| const [response, setResponse] = useState(""); | ||
| const [isLoading, setIsLoading] = useState(false); | ||
|
|
||
| const handleSubmit = async (e: FormEvent<HTMLFormElement>) => { | ||
| e.preventDefault(); | ||
| setIsLoading(true); | ||
| setResponse(""); | ||
|
|
||
| try { | ||
| const res = await fetch("/api/chat", { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify({ message: inputValue }), | ||
| }); | ||
|
|
||
| const data = (await res.json()) as ChatApiResponse; | ||
| if (!res.ok) { | ||
| throw new Error(data.response || "エラーが発生しました。"); | ||
| } | ||
| setResponse(data.response); | ||
| } catch (error: any) { | ||
| setResponse(`エラー: ${error.message}`); | ||
| } finally { | ||
| setIsLoading(false); | ||
| } | ||
| }; | ||
| return ( | ||
| <> | ||
| <style jsx>{` | ||
| <style jsx>{` | ||
| /* 簡単なCSSで見た目を整える(オプション) */ | ||
| .form-container { | ||
| background-color: white; | ||
| border-radius: 10px; | ||
| box-shadow: 0 4px 8px rgba(67, 204, 216, 0.86); | ||
| padding: 20px; | ||
| width: 90%; | ||
| max-width: 1000px; | ||
| display: flex; | ||
| flex-direction: column; | ||
| background-color: white; | ||
| border-radius: 10px; | ||
| box-shadow: 0 4px 8px rgba(67, 204, 216, 0.86); | ||
| padding: 20px; | ||
| width: 90%; | ||
| max-width: 1000px; | ||
| display: flex; | ||
| flex-direction: column; | ||
| } | ||
| .input-area { | ||
| border: 1px solid #ccc; | ||
| border-radius: 8px; | ||
| padding: 5px 15 px; | ||
| margin-bottom: 15px; | ||
| min-height: 150px; /* 入力欄の高さ */ | ||
| display: flex; | ||
| border: 1px solid #ccc; | ||
| border-radius: 8px; | ||
| padding: 5px 15 px; | ||
| margin-bottom: 15px; | ||
| min-height: 150px; /* 入力欄の高さ */ | ||
| display: flex; | ||
| } | ||
| .text-input { | ||
| border: none; | ||
| outline: none; | ||
| flex-grow: 1; | ||
| font-size: 16px; | ||
| resize: none; /* テキストエリアのリサイズを無効化 */ | ||
| overflow: auto; | ||
| padding: 10px; | ||
| border: none; | ||
| outline: none; | ||
| flex-grow: 1; | ||
| font-size: 16px; | ||
| resize: none; /* テキストエリアのリサイズを無効化 */ | ||
| overflow: auto; | ||
| padding: 10px; | ||
| } | ||
| .controls { | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: space-between; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: space-between; | ||
| } | ||
| .left-icons button { | ||
| background: none; | ||
| border: none; | ||
| font-size: 24px; | ||
| cursor: pointer; | ||
| color: #555; | ||
| margin-right: 15px; | ||
| padding: 5px; | ||
| background: none; | ||
| border: none; | ||
| font-size: 24px; | ||
| cursor: pointer; | ||
| color: #555; | ||
| margin-right: 15px; | ||
| padding: 5px; | ||
| } | ||
| .left-icons button:hover { | ||
| color: #000; | ||
| color: #000; | ||
| } | ||
| .left-icons span { | ||
| font-size: 14px; | ||
| vertical-align: middle; | ||
| margin-left: 5px; | ||
| color: #555; | ||
| font-size: 14px; | ||
| vertical-align: middle; | ||
| margin-left: 5px; | ||
| color: #555; | ||
| } | ||
| .right-controls { | ||
| display: flex; | ||
| align-items: center; | ||
| display: flex; | ||
| align-items: center; | ||
| } | ||
| .voice-icon button { | ||
| background: none; | ||
| border: none; | ||
| font-size: 24px; | ||
| cursor: pointer; | ||
| color: #555; | ||
| margin-right: 15px; | ||
| padding: 5px; | ||
| background: none; | ||
| border: none; | ||
| font-size: 24px; | ||
| cursor: pointer; | ||
| color: #555; | ||
| margin-right: 15px; | ||
| padding: 5px; | ||
| } | ||
| .voice-icon button:hover { | ||
| color: #000; | ||
| color: #000; | ||
| } | ||
| .send-button { | ||
| background-color: #007bff; /* 青色の送信ボタン */ | ||
| color: white; | ||
| border: none; | ||
| border-radius: 50%; /* 丸いボタン */ | ||
| width: 40px; | ||
| height: 40px; | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| font-size: 20px; | ||
| cursor: pointer; | ||
| box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); | ||
| transition: background-color 0.3s ease; | ||
| background-color: #007bff; /* 青色の送信ボタン */ | ||
| color: white; | ||
| border: none; | ||
| border-radius: 50%; /* 丸いボタン */ | ||
| width: 40px; | ||
| height: 40px; | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| font-size: 20px; | ||
| cursor: pointer; | ||
| box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); | ||
| transition: background-color 0.3s ease; | ||
| } | ||
| .send-button:hover { | ||
| background-color: #0056b3; | ||
| background-color: #0056b3; | ||
| } | ||
| `}</style> | ||
| `}</style> | ||
|
|
||
| <div className="form-container"> | ||
| <form className="form-container" onSubmit={handleSubmit}> | ||
| <div className="input-area"> | ||
| <textarea className="text-input" placeholder="質問を入力してください"></textarea> | ||
| <textarea | ||
| className="text-input" | ||
| placeholder="質問を入力してください" | ||
| value={inputValue} | ||
| onChange={(e) => setInputValue(e.target.value)} | ||
| disabled={isLoading} | ||
| ></textarea> | ||
| </div> | ||
|
|
||
| <div className="controls"> | ||
| <div className="left-icons"> | ||
|
|
||
| </div> | ||
| <div className="right-controls"> | ||
| <button type="submit" className="send-button" title="送信"> | ||
| <span className="icon">➤</span> | ||
| </button> | ||
| </div> | ||
| <div className="left-icons"></div> | ||
| <div className="right-controls"> | ||
| <button | ||
| type="submit" | ||
| className="send-button" | ||
| title="送信" | ||
| disabled={isLoading} | ||
| > | ||
| <span className="icon">➤</span> | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </form> | ||
| {response && <div className="response-container">{response}</div>} | ||
| </> | ||
| )} | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
エラーの型はanyではなくunknownにすると良いらしいですね
https://qiita.com/frozenbonito/items/e708dfb3ab7c1fd3824d