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

Happy Thoughts-Vera Sjunnesson #450

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions code/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"dependencies": {
"babel-eslint": "^10.1.0",
"date-fns": "^2.29.3",
"eslint": "^8.21.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-plugin-import": "^2.26.0",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion code/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
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>
<link rel="icon" href="https://cdn.shopify.com/s/files/1/1061/1924/products/Growing_Pink_Heart_Emoji_large.png?v=1571606066" type="image/icon type">
<title>Happy Thoughts Feed</title>
</head>

<body>
Expand Down
5 changes: 3 additions & 2 deletions code/src/App.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import { ThoughtFeed } from './components/ThoughtFeed';

export const App = () => {
return (
<div>
Find me in src/app.js!
<div className="main-wrapper">
<ThoughtFeed />
</div>
);
}
Empty file added code/src/SingleThought.js
Empty file.
87 changes: 87 additions & 0 deletions code/src/components/ThoughtFeed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* eslint no-underscore-dangle: 0 */
import React, { useState, useEffect } from 'react';
import { formatDistance } from 'date-fns';
import { ThoughtForm } from './ThoughtForm';

const API = 'https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts'

export const ThoughtFeed = () => {
const [thoughtList, setThoughtList] = useState([])
const [isLoading, setIsLoading] = useState(false)

const fetchThoughts = () => {
fetch(`${API}`)
.then((response) => response.json())
.then((data) => setThoughtList(data))
.catch((error) => console.log(error))
.finally(() => { setIsLoading(false) })
}

useEffect(() => {
setIsLoading(true);
fetchThoughts();
}, []);

const handleLikes = (_id) => {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}
fetch(`${API}/${_id}/like`, options)
.then((res) => {
return res.json();
})
.then((data) => {
const updatedThoughtList = thoughtList.map((thought) => {
if (thought._id === data._id) {
return {
...thought, hearts: data.hearts
};
}
return thought;
});
setThoughtList(updatedThoughtList);
})
.catch((error) => console.error(error));
}

return (
<>
<section className="form-container">
<ThoughtForm />
</section>
<section className="feed-container">
{!isLoading && thoughtList.map((thought) => {
return (
<div key={thought._id} className="card">
<p className="happy-message">{thought.message}</p>
<div className="card-details">
<div className="heart-container">
<button
type="button"
className={`heart-button ${thought.hearts > 0 ? 'heart-active' : ''}`}
onClick={() => { handleLikes(thought._id, thought.hearts); }}>
<span
role="img"
className="heart-icon"
aria-label="Like post">
❤️
</span>
</button>
<span className="total-hearts">x {thought.hearts}</span>
</div>
<p className="date">
{formatDistance(new Date(thought.createdAt), Date.now(), { addSuffix: true })}
</p>
</div>
</div>
)
})}
</section>
{isLoading && (<span className="is-loading" />)}
</>
)
}

66 changes: 66 additions & 0 deletions code/src/components/ThoughtForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { useState, useEffect } from 'react';

export const ThoughtForm = () => {
const [newThought, setNewThought] = useState('');
const [minMaxCount, setMinMaxCount] = useState(false);

const API = 'https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts'

const onFormSubmit = (event) => {
event.preventDefault()
if (newThought.length < 5 || newThought.length >= 141) {
return alert('Oh no, you have too few or too many characters! Please, try again.')
} else {
const options = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: newThought })
};
fetch(`${API}`, options)
.then((response) => response.json())
.then(() => {
setNewThought('');
window.location.reload();
})
}
}

const onNewThoughtChange = (event) => {
setNewThought(event.target.value);
}

useEffect(() => {
if (newThought.length < 5 || newThought.length >= 141) {
setMinMaxCount(true)
} else {
setMinMaxCount(false)
}
}, [newThought.length]);

return (
<form className="thoughtForm" onSubmit={onFormSubmit}>
<h3>What&apos;s making you happy right now?</h3>
<label htmlFor="thought-input">
<textarea
name="thought-input"
id="thought-input"
className={`text-input ${minMaxCount ? 'red-text' : ''}`}
value={newThought}
onChange={onNewThoughtChange}
placeholder="Type your happy thought..." />
</label>
<div className="form-details">
<p>
<span className={`number-count ${minMaxCount ? 'red-number' : ''}`}>
{newThought.length}
</span> / 140
</p>
<button
className="send-button"
type="submit">
❤️ <span>Send Happy Thought</span> ❤️
</button>
</div>
</form>
)
}
Loading