From 8c27801158125d6c91314ab7691ac47fa9c6b95e Mon Sep 17 00:00:00 2001
From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com>
Date: Tue, 17 May 2022 13:56:24 -0500
Subject: [PATCH] conditional rendering
---
src/App.css | 2 +-
src/App.js | 23 +++++++++---
src/components/ResponseSection.js | 58 +++++++++----------------------
src/components/SubmitSection.js | 30 ++++++++++++++++
4 files changed, 67 insertions(+), 46 deletions(-)
create mode 100644 src/components/SubmitSection.js
diff --git a/src/App.css b/src/App.css
index cb00af8..8b4e0b9 100644
--- a/src/App.css
+++ b/src/App.css
@@ -72,7 +72,7 @@ button:active {
display: grid;
grid-template: "prompt-label prompt" 1fr
"response-label response" 1fr
- / auto;
+ / 1fr 4fr;
background-color: lightgray;
border-radius: 12px;
padding: 0 1rem;
diff --git a/src/App.js b/src/App.js
index 923432e..b6a95ec 100644
--- a/src/App.js
+++ b/src/App.js
@@ -1,11 +1,25 @@
-import { useState } from "react";
+import { useCallback, useState, useRef, useEffect } from "react";
import ResponseSection from './components/ResponseSection';
import './App.css';
+import SubmitSection from "./components/SubmitSection";
function App() {
const [userInput, setUserInput] = useState('');
+ const [response, setResponse] = useState(null);
- const userPrompt = null;
+ const responseRef = useRef(null);
+
+ const promptCallback = useCallback((userPrompt) => {
+ setUserInput(userPrompt);
+ }, []);
+
+ const responseCallback = useCallback((APIresponse) => {
+ setResponse(APIresponse);
+ }, []);
+
+ useEffect(() => {
+ response && responseRef.current();
+ }, [response]);
return (
@@ -14,11 +28,12 @@ function App() {
-
+
+
);
}
diff --git a/src/components/ResponseSection.js b/src/components/ResponseSection.js
index 2f7ed7a..49180e7 100644
--- a/src/components/ResponseSection.js
+++ b/src/components/ResponseSection.js
@@ -1,9 +1,8 @@
-import { useState } from "react";
-
-export default function ResponseSection({ userPrompt, userInput }) {
- let secret = process.env.REACT_APP_API_SECRET;
+import { useEffect, useState } from "react";
+export default function ResponseSection({ userInput, response, responseRef }) {
const [resCount, setResCount] = useState(1);
+
const [contents, setContents] = useState([
Prompt:
@@ -13,54 +12,31 @@ export default function ResponseSection({ userPrompt, userInput }) {
]);
- const addNewResponse = async () => {
- let data = { prompt: userInput };
-
- let result = await fetch("https://api.openai.com/v1/engines/text-curie-001/completions", {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- Authorization: `Bearer ${secret}`
- },
- body: JSON.stringify(data)
- }).then(res => res.json());
-
- let AIresponse = result.choices[0].text;
-
+ const appendResponse = () => {
let newState = contents;
let newCount;
- AIresponse && (newCount = newState.unshift(
-
+ response && (newCount = setContents(newState.unshift(
+
Prompt:
{userInput}
Response:
-
{AIresponse}
+
{response}
- ));
+ )));
setResCount(newCount);
- setContents(newState);
+
+ console.log(contents);
}
+ useEffect(() => {
+ responseRef.current = appendResponse;
+ }, []);
+
return (
-
-
-
-
-
-
-
-
-
-
- Responses
- {contents}
-
+
)
}
\ No newline at end of file
diff --git a/src/components/SubmitSection.js b/src/components/SubmitSection.js
new file mode 100644
index 0000000..b2dc404
--- /dev/null
+++ b/src/components/SubmitSection.js
@@ -0,0 +1,30 @@
+export default function SubmitSection({ promptCallback, userInput, responseCallback }) {
+ const getResponse = async () => {
+ let data = { prompt: userInput };
+
+ await fetch("https://api.openai.com/v1/engines/text-curie-001/completions", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${process.env.REACT_APP_API_SECRET}`
+ },
+ body: JSON.stringify(data)
+ }).then(res => res.json())
+ .then(x => responseCallback(x.choices[0].text));
+ }
+
+ return (
+
+
+
+
+
+
+
+ )
+}