-
Notifications
You must be signed in to change notification settings - Fork 145
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
Jonas Happy Thought App #92
Open
Jonash189
wants to merge
7
commits into
Technigo:main
Choose a base branch
from
Jonash189:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
04079bc
Creating first component HappyToughts were i will store everyting.
Jonash189 9765b2c
Creating ThoughtForm component.
Jonash189 92a83da
Adding ThoughtLikes component and budilding the function to like.
Jonash189 6d05312
Creating HappyToughts component and making it the main component whe…
Jonash189 6be38c1
Creating varible savedLikes so I can store the likes and return the …
Jonash189 7462e04
Adding CSS.
Jonash189 5c14722
Cleaning up the code.
Jonash189 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
import { HappyThoughts } from './components/HappyThoughts'; | ||
|
||
|
||
|
||
|
||
export const App = () => { | ||
return <div>Find me in src/app.jsx!</div>; | ||
}; | ||
return ( | ||
<div> | ||
|
||
< HappyThoughts /> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
|
||
import { Header } from "./Header" | ||
import { useState, useEffect } from "react" | ||
import { ThoughtForm } from "./ThoughtForm" | ||
import { ThoughtList } from "./ThoughtList" | ||
|
||
|
||
export const HappyThoughts = () => { | ||
const [thoughts, setThoughts] = useState([]); | ||
const [newThought, setNewThought] = useState("") | ||
|
||
useEffect(() => { | ||
fetch("https://project-happy-thoughts-api-j0eg.onrender.com/thoughts") | ||
.then(res => res.json()) | ||
.then((json) => { | ||
setThoughts(json.response) | ||
}) | ||
.catch(error => { | ||
console.error('Fel vid hämtning av tankar:', error); | ||
}); | ||
}, []); | ||
|
||
const handleNewThought = (event) => { | ||
setNewThought(event.target.value) | ||
} | ||
|
||
//Function to POST new Happy Thoughts | ||
const onFormSubmit = (event) => { | ||
event.preventDefault() | ||
|
||
|
||
const PostOptions = { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
message: newThought, | ||
}), | ||
} | ||
|
||
fetch( | ||
"https://project-happy-thoughts-api-j0eg.onrender.com/thoughts", | ||
PostOptions | ||
) | ||
.then((res) => res.json()) | ||
.then((newThought) => { | ||
setThoughts((previousThoughts) => [ | ||
newThought.response, | ||
...previousThoughts, | ||
]) | ||
setNewThought("") | ||
}) | ||
.catch((error) => { | ||
console.error("Error posting Happy Thought:", error) | ||
}) | ||
} | ||
|
||
return ( | ||
<div> | ||
<Header /> | ||
<main className="main-wrapper"> | ||
<div className="main-content"> | ||
<ThoughtForm | ||
newThought={newThought} | ||
onNewThoughtChange={handleNewThought} | ||
onFormSubmit={onFormSubmit} | ||
/> | ||
<ThoughtList thoughts={thoughts} /> | ||
</div> | ||
</main> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const Header = () => { | ||
return ( | ||
<div className="header"> | ||
<h1>Project Happy Thoughts</h1> | ||
<h2>by Jonas</h2> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from "react"; | ||
|
||
|
||
export const ThoughtForm = ({ onFormSubmit, newThought, onNewThoughtChange }) => { | ||
|
||
const disableSubmit = newThought.length < 5 || newThought.length > 140; | ||
|
||
return ( | ||
<form className="form-container" onSubmit={onFormSubmit}> | ||
<label> | ||
<h3>What makes you happy..</h3> | ||
<textarea | ||
className="thought-input" | ||
value={newThought} | ||
onChange={onNewThoughtChange} | ||
placeholder="Write something here" | ||
required | ||
/> | ||
</label> | ||
|
||
<button | ||
type="submit" | ||
className="submit-thought" | ||
aria-label="button for submit thought" | ||
disabled={disableSubmit} | ||
> | ||
❤️ Send Happy Thought ❤️ | ||
</button> | ||
</form> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useState, useEffect } from "react"; | ||
|
||
export const ThoughtLikes = ({ id, heart }) => { | ||
const [likes, setLikes] = useState(() => { | ||
const savedLikes = localStorage.getItem(`likes-${id}`); | ||
return savedLikes !== null ? Number(savedLikes) : Number(heart) || 0; | ||
}); | ||
|
||
useEffect(() => { | ||
localStorage.setItem(`likes-${id}`, likes); | ||
}, [likes, id]); | ||
|
||
const handleLikes = () => { | ||
fetch(`https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts/${id}/like`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}) | ||
.then((res) => { | ||
if (!res.ok) { | ||
throw new Error('Network response was not ok'); | ||
} | ||
return res.json(); | ||
}) | ||
.then(() => { | ||
setLikes((prevLikes) => prevLikes + 1); | ||
}) | ||
.catch((error) => { | ||
console.error("Error:", error); | ||
}); | ||
}; | ||
|
||
return ( | ||
<div className="likes"> | ||
<button | ||
onClick={handleLikes} | ||
aria-label="Like-button" | ||
className={likes === 0 ? "heart-button" : "liked-button"} | ||
type="button" | ||
> | ||
❤️ | ||
</button> | ||
<p>x {likes}</p> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { formatDistanceToNow } from 'date-fns'; | ||
import { ThoughtLikes } from "./ThoughtLikes.jsx"; | ||
|
||
export const ThoughtList = ({ thoughts }) => { | ||
return ( | ||
<section className="thought-section"> | ||
{thoughts.map((thought) => ( | ||
<div className="thought-wrapper" key={thought._id}> | ||
<div className="input-message">{thought.message}</div> | ||
<div className="info-wrapper"> | ||
<ThoughtLikes id={thought._id} hearts={thought.hearts} /> | ||
<div className="time"> | ||
{formatDistanceToNow(new Date(thought.createdAt), { | ||
addSuffix: true, | ||
})} | ||
</div> | ||
</div> | ||
</div> | ||
))} | ||
</section> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
⭐️