-
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 Josefin R #438
base: master
Are you sure you want to change the base?
Changes from 12 commits
2765602
5791e35
b6045c6
ad252dd
69f37e2
3458694
8c0e7b0
63b0cd4
a7fbf8d
d6277d5
2fdcbe1
e914e8f
6ca7209
057a452
a7ae584
cd55192
df3e9f0
f9c2cb9
e7186ed
e3e14e7
6d19ee8
54ac6a8
9b397f0
1f71e67
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"liveServer.settings.port": 5501 | ||
} |
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. | ||
A cute little project where you post happy thoughts and like other peoples' happy thoughts and everyone gets happy vibes | ||
|
||
## 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? | ||
Create fetches to both post to and get data from the API. Create like button. Follow styling requests | ||
|
||
## 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://mellow-duckanoo-8fdcc7.netlify.app/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,29 @@ | ||
import React from 'react'; | ||
import React, { useState } from 'react'; | ||
import DataList from 'components/DataList'; | ||
import InputForm from 'components/InputForm'; | ||
|
||
export const App = () => { | ||
const [happyThoughtsList, setHappyThoughtsList] = useState([]); | ||
const [loading, setLoading] = useState(false); | ||
const [newThought, setNewThought] = useState(''); | ||
const [charCount, setCharCount] = useState(140); | ||
|
||
return ( | ||
<div> | ||
Find me in src/app.js! | ||
</div> | ||
); | ||
} | ||
<> | ||
<InputForm | ||
loading={loading} | ||
setLoading={setLoading} | ||
newThought={newThought} | ||
setNewThought={setNewThought} | ||
setHappyThoughtsList={setHappyThoughtsList} | ||
happyThoughtsList={happyThoughtsList} | ||
charCount={charCount} | ||
setCharCount={setCharCount} /> | ||
<DataList | ||
loading={loading} | ||
setLoading={setLoading} | ||
happyThoughtsList={happyThoughtsList} | ||
setHappyThoughtsList={setHappyThoughtsList} /> | ||
</> | ||
) | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React from 'react'; | ||
import SingleThought from './SingleThought'; | ||
|
||
const DataList = ({ loading, setLoading, happyThoughtsList, setHappyThoughtsList }) => { | ||
const updateList = (thought) => { | ||
setLoading(true) | ||
const options = { | ||
method: 'POST' | ||
}; | ||
|
||
console.log('options', options); | ||
// eslint-disable-next-line no-underscore-dangle | ||
fetch(`https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts/${thought._id}/like`, options) | ||
.then((response) => response.json()) | ||
.then((updatedThought) => { | ||
console.log(updatedThought); | ||
|
||
// create a new array with the updated thought | ||
const updatedThoughtsList = happyThoughtsList.map((thoughtItem) => { | ||
// eslint-disable-next-line no-underscore-dangle | ||
if (thoughtItem._id === updatedThought._id) { | ||
return updatedThought; | ||
} | ||
return thoughtItem; | ||
}); | ||
|
||
if (loading) { | ||
return (<h2>loading in process...</h2>); | ||
} | ||
|
||
// set the updated thoughts list | ||
setHappyThoughtsList(updatedThoughtsList); | ||
}) | ||
.catch((error) => console.log(error)) | ||
.finally(() => { | ||
setLoading(false); | ||
console.log('heart count increased') | ||
}); | ||
}; | ||
|
||
const heartCountClick = (thought) => { | ||
console.log('like-click') | ||
updateList(thought); | ||
}; | ||
|
||
return ( | ||
<div className="listItems"> | ||
{happyThoughtsList.map((thought) => ( | ||
// eslint-disable-next-line no-underscore-dangle | ||
<SingleThought key={thought._id} thought={thought} onHeartClick={heartCountClick} /> | ||
))} | ||
</div> | ||
) | ||
}; | ||
|
||
export default DataList; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import React, { useEffect } from 'react'; | ||
|
||
const InputForm = ({ | ||
loading, | ||
setLoading, | ||
newThought, | ||
setNewThought, | ||
setHappyThoughtsList, | ||
happyThoughtsList, | ||
charCount, | ||
setCharCount | ||
}) => { | ||
const updateThoughtsList = () => { | ||
setLoading(true); | ||
|
||
fetch('https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts') | ||
.then((response) => response.json()) | ||
.then((data) => setHappyThoughtsList(data)) | ||
.catch((error) => console.log(error)) | ||
.finally(() => { | ||
console.log('it worked'); | ||
setLoading(false); | ||
console.log('loading-Get'); | ||
console.log(happyThoughtsList); | ||
}); | ||
}; | ||
|
||
const handleFormSubmit = (event) => { | ||
event.preventDefault(); | ||
setLoading(true); | ||
const options = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
message: newThought | ||
}) | ||
}; | ||
|
||
console.log('options', options); | ||
fetch('https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts', options) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
setHappyThoughtsList([data, ...happyThoughtsList]); | ||
console.log(data); | ||
}) | ||
.catch((error) => console.log(error)) | ||
.finally(() => { | ||
setLoading(false); | ||
console.log(happyThoughtsList) | ||
setNewThought(''); // clear textarea | ||
setCharCount(140); | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
updateThoughtsList(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
const handleInputChange = (event) => { | ||
const { value } = event.target; | ||
setNewThought(value); | ||
setCharCount(140 - value.length); // update character count | ||
}; | ||
|
||
if (loading) { | ||
return (<h2>loading in process...</h2>); | ||
} | ||
|
||
return ( | ||
<div className="inputForm"> | ||
<form onSubmit={handleFormSubmit}> | ||
<h3>What's making you happy right now?</h3> | ||
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. &apos |
||
<textarea | ||
value={newThought} | ||
onChange={handleInputChange} | ||
maxLength="140" /> | ||
<div id="counter">140/{charCount}</div> | ||
<button type="submit"><span id="heart"> Send Happy Thought </span></button> | ||
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. You might want to add a feature that disables the send button if a value (<= 5) is entered in the text box;, to prevent a error message from the API, that gets rendered out in the DataList component. |
||
</form> | ||
</div> | ||
); | ||
}; | ||
|
||
export default InputForm; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
|
||
let timeStamp; | ||
|
||
const SingleThought = ({ thought, onHeartClick }) => { | ||
const date = new Date(thought.createdAt); | ||
const timeDiff = Math.round((new Date() - date) / (1000 * 60)); | ||
if (timeDiff < 1) { | ||
timeStamp = 'just now'; | ||
} else if (timeDiff < 90) { | ||
timeStamp = `${timeDiff} min ago`; | ||
} else { | ||
const hoursDiff = Math.round(timeDiff / 60); | ||
timeStamp = `${hoursDiff} hour${hoursDiff > 1 ? 's' : ''} ago`; | ||
} | ||
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. A very nice structured function; Its easy to understand. |
||
console.log('singlelistitem') | ||
return ( | ||
<div className="singleListItem"> | ||
<h3 id="stretched">{thought.message}</h3> | ||
<div className="buttonTimestampBox"> | ||
<div className="heartCounter"><button onClick={() => onHeartClick(thought)} type="button"><span id="heartButton">🧡</span></button> | ||
<span> x {thought.hearts}</span> | ||
</div> | ||
<p className="timeStamp">{timeStamp}</p> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SingleThought; |
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.
Is there a reason why you didn't use eslint-disable in the whole file? Makes it easier (in my own opinion) to remove if necessary in the top level of the file.