-
Notifications
You must be signed in to change notification settings - Fork 434
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
Project happy thoughts #449
base: master
Are you sure you want to change the base?
Changes from 1 commit
ae24ac1
fc3e795
b42822a
94ad92a
ee7e1a5
0a92b37
c2537c6
c1bdcb1
1441fdc
55e06fb
533be9d
7ee0d11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,52 @@ | ||
import React from 'react'; | ||
/* eslint-disable no-underscore-dangle */ | ||
/* eslint-disable no-unused-vars */ | ||
import React, { useState, useEffect } from 'react'; | ||
import { SubmitThoughts } from 'components/SubmitThoughts'; | ||
import { ListThoughts } from 'components/ListThoughts'; | ||
|
||
export const App = () => { | ||
const [thoughtsList, setThoughtsList] = useState([]) | ||
const [submitThoughts, setSubmitThoughts] = useState('') | ||
const [loading, setLoading] = useState(false) | ||
|
||
const fetchThoughts = () => { | ||
setLoading(true) | ||
fetch('https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts') | ||
.then((response) => response.json()) | ||
.then((data) => setThoughtsList(data)) | ||
.catch((error) => console.log(error)) | ||
.finally(() => { setLoading(false) }) | ||
} | ||
|
||
useEffect(() => { | ||
fetchThoughts() | ||
}, []) | ||
|
||
Comment on lines
+21
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This hook is to call the fetchThoughts function when the component is mounted so this should/could/would also be moved if so we were to change the structure for a "cleaner" App.js |
||
const handleFormSubmit = (event) => { | ||
event.preventDefault() | ||
setLoading(true) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the "handleFormSubmit" handles the submission of a new thought it could be moved into your component "submitThoughts.js" instead if we want to continue on "cleaning" the app.js a bit. |
||
const options = { | ||
method: 'POST', | ||
body: JSON.stringify({ message: submitThoughts }), | ||
headers: { 'Content-Type': 'application/json' } | ||
} | ||
fetch('https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts', options) | ||
.then((response) => response.json()) | ||
.then((data) => { setThoughtsList([data, ...thoughtsList]) }) | ||
.catch((error) => console.log(error)) | ||
.finally(() => { setLoading(false) }) | ||
} | ||
|
||
return ( | ||
<div> | ||
Find me in src/app.js! | ||
<SubmitThoughts | ||
submitThoughts={submitThoughts} | ||
setSubmitThoughts={setSubmitThoughts} | ||
handleFormSubmit={handleFormSubmit} /> | ||
<ListThoughts | ||
loading={loading} | ||
thoughtsList={thoughtsList} /> | ||
</div> | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import React from 'react' | ||
|
||
export const ListThoughts = ({ loading, thoughtsList }) => { | ||
if (loading) { | ||
return <h2>Loading...</h2> | ||
} | ||
return thoughtsList.map((thought) => { | ||
return ( | ||
<p key={thought._id}>{thought.message}</p> | ||
) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from 'react' | ||
|
||
export const SubmitThoughts = ({ handleFormSubmit, submitThoughts, setSubmitThoughts }) => { | ||
<form onSubmit={handleFormSubmit}> | ||
<textarea type="text" value={submitThoughts} onChange={(event) => setSubmitThoughts(event.target.value)} /> | ||
<p> | ||
<button type="submit">Submit form</button> | ||
</p> | ||
</form> | ||
} |
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.
To keep the App component cleaner (not really necessary I think in this size of projects but a good practise for the future when we work in bigger projects) this could've been places in one of your components. Since this is fetching the thoughts from the API it would be neat to place it in your ListThoughts.js maybe!