Skip to content
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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion code/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>Technigo React App</title>
<title>Happy Thoughts</title>
</head>

<body>
Expand Down
49 changes: 46 additions & 3 deletions code/src/App.js
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) })
}
Copy link

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!


useEffect(() => {
fetchThoughts()
}, [])

Comment on lines +21 to +24
Copy link

Choose a reason for hiding this comment

The 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)

Copy link

Choose a reason for hiding this comment

The 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>
);
}
}
13 changes: 13 additions & 0 deletions code/src/components/ListThoughts.js
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>
)
})
}
10 changes: 10 additions & 0 deletions code/src/components/SubmitThoughts.js
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>
}