Permalink
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
openai-quickstart-node/pages/index.js /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
58 lines (52 sloc)
1.54 KB
This file contains 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
| import Head from "next/head"; | |
| import { useState } from "react"; | |
| import styles from "./index.module.css"; | |
| export default function Home() { | |
| const [animalInput, setAnimalInput] = useState(""); | |
| const [result, setResult] = useState(); | |
| async function onSubmit(event) { | |
| event.preventDefault(); | |
| try { | |
| const response = await fetch("/api/generate", { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| }, | |
| body: JSON.stringify({ animal: animalInput }), | |
| }); | |
| const data = await response.json(); | |
| if (response.status !== 200) { | |
| throw data.error || new Error(`Request failed with status ${response.status}`); | |
| } | |
| setResult(data.result); | |
| setAnimalInput(""); | |
| } catch(error) { | |
| // Consider implementing your own error handling logic here | |
| console.error(error); | |
| alert(error.message); | |
| } | |
| } | |
| return ( | |
| <div> | |
| <Head> | |
| <title>OpenAI Quickstart</title> | |
| <link rel="icon" href="/dog.png" /> | |
| </Head> | |
| <main className={styles.main}> | |
| <img src="/dog.png" className={styles.icon} /> | |
| <h3>Name my pet</h3> | |
| <form onSubmit={onSubmit}> | |
| <input | |
| type="text" | |
| name="animal" | |
| placeholder="Enter an animal" | |
| value={animalInput} | |
| onChange={(e) => setAnimalInput(e.target.value)} | |
| /> | |
| <input type="submit" value="Generate names" /> | |
| </form> | |
| <div className={styles.result}>{result}</div> | |
| </main> | |
| </div> | |
| ); | |
| } |