From 39102177042e53aa89e7cc6056f46953c0ab2209 Mon Sep 17 00:00:00 2001 From: gabster94 Date: Fri, 25 Oct 2024 14:47:02 +0200 Subject: [PATCH 1/9] created a thought component which GETs the data from the api and returns the messages as listed --- src/App.jsx | 11 ++++++++++- src/utils/Thoughts.jsx | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/utils/Thoughts.jsx diff --git a/src/App.jsx b/src/App.jsx index 1091d431..120afdbc 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,3 +1,12 @@ +import React from "react" +import { Thoughts } from "./utils/Thoughts" + export const App = () => { - return
Find me in src/app.jsx!
; + return ( +
+ + + +
+ ); }; diff --git a/src/utils/Thoughts.jsx b/src/utils/Thoughts.jsx new file mode 100644 index 00000000..554cc44f --- /dev/null +++ b/src/utils/Thoughts.jsx @@ -0,0 +1,33 @@ + +import { useState,useEffect } from "react" + +export const Thoughts = () => { + const [thoughts, setThoughts] = useState([]) + + const URL = "https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts" + + const fetchThoughts = async () => { + const response = await fetch(URL) + const data = await response.json() + setThoughts(data) + console.log("Thought is", data) + } + + useEffect(() => { + fetchThoughts() + }, []); + + return ( +
+ +
+ ) + +} + + + From 22a7e434a8eb698adcf9f21fbba20d3554e407f9 Mon Sep 17 00:00:00 2001 From: gabster94 Date: Fri, 25 Oct 2024 17:40:11 +0200 Subject: [PATCH 2/9] created 3 components: header, footer and SendThoughts. The latter makes a POST-request. Successful --- src/App.jsx | 8 +++- src/components/Footer.jsx | 8 ++++ src/components/Header.jsx | 9 ++++ src/components/SendThoughts.jsx | 57 ++++++++++++++++++++++++++ src/{utils => components}/Thoughts.jsx | 11 +++-- src/index.css | 2 + 6 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 src/components/Footer.jsx create mode 100644 src/components/Header.jsx create mode 100644 src/components/SendThoughts.jsx rename src/{utils => components}/Thoughts.jsx (65%) diff --git a/src/App.jsx b/src/App.jsx index 120afdbc..49198d63 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,10 +1,16 @@ import React from "react" -import { Thoughts } from "./utils/Thoughts" +import { Header } from "./components/Header"; +import { Text } from "./components/SendThoughts"; +import { Thoughts } from "./components/Thoughts" +import { Footer } from "./components/Footer"; export const App = () => { return (
+
+ +
diff --git a/src/components/Footer.jsx b/src/components/Footer.jsx new file mode 100644 index 00000000..9a923676 --- /dev/null +++ b/src/components/Footer.jsx @@ -0,0 +1,8 @@ +export const Footer = () => { + return ( + <> +

Thank you for your sparkle

+

copyright by Gabriella Iofe

+ + ) +} \ No newline at end of file diff --git a/src/components/Header.jsx b/src/components/Header.jsx new file mode 100644 index 00000000..953ce6df --- /dev/null +++ b/src/components/Header.jsx @@ -0,0 +1,9 @@ +export const Header = () => { + return ( + <> +

Happpy Thoughts Bank

+

Here we share all our happy rhoughts to send some shine to eachother

+ + ) + +} \ No newline at end of file diff --git a/src/components/SendThoughts.jsx b/src/components/SendThoughts.jsx new file mode 100644 index 00000000..51c0d448 --- /dev/null +++ b/src/components/SendThoughts.jsx @@ -0,0 +1,57 @@ +import { useState } from "react"; + +export const Text = () => { + + const [body, setBody] = useState('') + const [response, setResponse] = useState(null) + + const handleSubmit = async (event) => { + event.preventDefault() + + const URL = "https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts" + + try { + const res = await fetch(URL, { + method: "POST", + headers: { + "content-type": "application/json; charset=utf-8" + }, //this is a way to send extra information to our API. Some request an API key as a header or access tokens: 'AccessToken': 'myaccesstoken' + body: JSON.stringify({ + // title, //write it like this if our name for it is different title: titleText. same for body + message: body + // userID: 1 -optional + }) + }) + + const data = await res.json() + setResponse(data) //this is optional. it displays the post and shows it to the user in the browser + } catch (error) { + console.log("error:", error) + } + } + + + + return ( +
+

Submit a port

+
+