-
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
Happy Thoughts #434
base: master
Are you sure you want to change the base?
Happy Thoughts #434
Changes from 11 commits
e9b0c37
88bcf93
bf9cc06
477bd60
b852ef9
d38756e
cee209c
0abaa6e
9f85e38
44c4ae7
0f16e36
c8db719
426bc09
dedf076
e9f6a06
a5baa58
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,13 +1,11 @@ | ||
# Happy Thoughts | ||
|
||
Replace this readme with your own information about your project. | ||
|
||
Start by briefly describing the assignment in a sentence or two. Keep it short and to the point. | ||
I have created a Twitter-like feed where a user can write a ‘happy thought’ which is then displayed along with other recent happy thoughts from an API. The thoughts are listed with the most recent at the top and older ones at the bottom, with a timestamp on when they were written. Anyone can then see the thought, click on a ‘like’ button if they like it, and see how many likes the thought has received. I had to follow a design closely. As one of my stretch goals I also added a count below the form input which updates as the user types and shows how many characters are remaining. The count goes red if the user types under five characters or over 140. | ||
|
||
## The problem | ||
|
||
Describe how you approached to problem, and what tools and techniques you used to solve it. How did you plan? What technologies did you use? If you had more time, what would be next? | ||
I began by using the Example Project to help me understand how to write the code, which components I would need, and how to fetch and post the thoughts using the APIs. I also thought about which useState hooks I would need and which useEffect hook I would need to use. My first task was to fetch the recent thoughts, then create a thought using a POST request. After that I worked on the Heart (like) button. There were a lot of different elements to this week’s task and I needed help with some challenges I faced. I am pleased I managed to add a stretch goal in the end and if I had more time I would add another stretch goal, such as work on getting an error message back from the API if the message is empty, too long or too short. | ||
|
||
## View it live | ||
|
||
Every project should be deployed somewhere. Be sure to include the link to the deployed project so that the viewer can click around and see what it's all about. | ||
https://fiona-klacar-project-happy-thoughts.netlify.app/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,73 @@ | ||
import React from 'react'; | ||
/* eslint-disable max-len */ | ||
import React, { useState, useEffect } from 'react'; | ||
import { ThoughtInput } from 'components/ThoughtInput'; | ||
import { EachThought } from 'components/EachThought'; | ||
|
||
export const App = () => { | ||
const [eachThought, setEachThought] = useState(''); | ||
// because a new thought has no value and then changes state to having a value | ||
|
||
const [thoughtInput, setThoughtInput] = useState([]); | ||
// because the Thought Input changes state when a new thought is added | ||
|
||
const fetchThoughts = () => { | ||
fetch('https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts') | ||
.then((res) => res.json()) // converts the 'response' object to a JSON object | ||
.then((data) => setThoughtInput(data)) // updates the state with the data from the | ||
// response using the 'setThoughtInput' function | ||
.catch((error) => console.error(error)) // catches errors | ||
.finally(() => { console.log('fetch was successful') }) // this is where setLoading(false)); would go | ||
} | ||
|
||
useEffect(() => { | ||
fetchThoughts(); | ||
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. Is there any specific reason why you did a separate function instead of putting the fetch request directly in useEffect? |
||
// we want to fetch the most recent thoughts from the API | ||
// every time the page is mounted/reloaded - so we use an empty array as a dependency | ||
}, []); | ||
|
||
const onEachThoughtChange = (event) => { | ||
setEachThought(event.target.value) | ||
} | ||
|
||
const onFormSubmit = (event) => { | ||
event.preventDefault(); // prevents from reloading entirely when user submits a thought | ||
|
||
// Note that const onFormSubmit includes everything from line 32 to line 52. | ||
|
||
const options = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
message: eachThought | ||
}) | ||
} | ||
|
||
fetch('https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts', options) | ||
.then((res) => res.json()) | ||
.then((data) => console.log(data)) // Don't put setThoughtInput as the whole thing will be replaced | ||
.catch((error) => console.error(error)) // catches errors | ||
.finally(() => { fetchThoughts(); setEachThought(''); }); // This collects all the updated thoughts and updated likes | ||
}; | ||
|
||
// THIS IS ANOTHER WAY OF DOING IT | ||
// .then((res) => res.json()) | ||
// .then((newThought) => { | ||
// //Now you have `newThought` which is the response from the | ||
// // API as documented at the top of this readme. You can use | ||
// // it to update the `thoughts` array: | ||
// setThoughts((previousThoughts) => [newThought,...previousThoughts]) // Here is object destructuring - it adds the | ||
// new thought to the array as an object and adds the previous thoughts to it after | ||
// }) | ||
|
||
return ( | ||
<div> | ||
Find me in src/app.js! | ||
<div className="mainContainer"> | ||
<ThoughtInput | ||
eachThought={eachThought} | ||
onEachThoughtChange={onEachThoughtChange} | ||
onFormSubmit={onFormSubmit} /> | ||
<EachThought thoughtInput={thoughtInput} fetchThoughts={fetchThoughts} /> | ||
</div> | ||
); | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import React from 'react'; | ||
import { formatDistance } from 'date-fns'; | ||
import { Hearts } from './Hearts'; | ||
|
||
export const EachThought = ({ thoughtInput, fetchThoughts }) => { | ||
return thoughtInput.map((thought) => { | ||
// eslint-disable-next-line no-underscore-dangle | ||
return ( | ||
<div className="eachThought" key={thought._id}> | ||
<p>{thought.message}</p> | ||
<div className="eachThoughtLower"> | ||
<Hearts | ||
thought={thought} | ||
fetchThoughts={fetchThoughts} /> | ||
<p className="timeStamp date">{formatDistance(new Date(thought.createdAt), Date.now(), { addSuffix: true })}</p> | ||
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. Nice way of displaying the date! |
||
</div> | ||
</div> | ||
) | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import React from 'react'; | ||
|
||
export const Hearts = ({ thought, fetchThoughts }) => { | ||
const onHeartCountIncreaseButtonClick = () => { | ||
const options = { | ||
method: 'POST' | ||
} | ||
console.log('options', options) | ||
|
||
fetch(`https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts/${thought._id}/like`, options) | ||
.then((res) => res.json()) | ||
.then((data) => console.log(data)) | ||
.catch((error) => console.log(error)) | ||
.finally(() => fetchThoughts()) | ||
console.log('heart count increase') | ||
} | ||
|
||
return ( | ||
// eslint-disable-next-line react/jsx-no-comment-textnodes | ||
<div> | ||
<div className="heartsSection"> | ||
<button className="heartButton" onClick={onHeartCountIncreaseButtonClick} type="button">❤️</button> | ||
<p className="heartCount">x <span className="heartCountNumber">{thought.hearts}</span></p> | ||
</div> | ||
</div> | ||
) | ||
} | ||
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. good structure and clean code 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* eslint-disable max-len */ | ||
import React from 'react'; | ||
|
||
export const ThoughtInput = ({ eachThought, onEachThoughtChange, onFormSubmit }) => { | ||
return ( | ||
<form className="thoughtInput" onSubmit={onFormSubmit}> | ||
<h1>What's making you happy right now?</h1> | ||
<textarea | ||
value={eachThought} | ||
onChange={onEachThoughtChange} | ||
placeholder="React is making me happy!" /> | ||
<div className="characterCount">{eachThought.length < 5 || eachThought.length > 140 ? ( | ||
<p className="redText">{eachThought.length}/140</p> | ||
) : ( | ||
<p className="normalText">{eachThought.length}/140</p> | ||
)} | ||
</div> | ||
Comment on lines
+12
to
+17
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. Nice with the change depending on the characters, another way of getting the className to change with less code could be: |
||
<button className="submitButton" type="submit"> | ||
<span role="img" aria-label="heart">❤️ </span> | ||
<span className="submitButtonText">Send happy thought!</span> | ||
<span role="img" aria-label="heart"> ❤️</span> | ||
</button> | ||
</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.
Structured way to go about it! Always easier to divide the project in smaller bits to check off ✅