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

Week 11: Project Happy thoughts #98

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"moment": "^2.30.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
7 changes: 7 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.app-container {
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}

61 changes: 59 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,60 @@
export const App = () => {
return <div>Find me in src/app.jsx!</div>;
import React, { useState, useEffect } from "react";
import { ThoughtForm } from "./components/ThoughtForm";
import { ThoughtList } from "./components/ThoughtList";
import { LoadingSpinner } from "./components/LoadingSpinner";
import './App.css';

const App = () => {
const [thoughts, setThoughts] = useState([]);
const [likedThoughts, setLikedThoughts] = useState(() => {
const savedLikes = localStorage.getItem("likedThoughts");
return savedLikes ? JSON.parse(savedLikes) : [];
});

useEffect(() => {
const fetchThoughts = async () => {
try {
const response = await fetch("https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts");
const data = await response.json();
setThoughts(data);
} catch (error) {
console.error("Error fetching thoughts:", error);
}
};
fetchThoughts();
}, []);

useEffect(() => {
localStorage.setItem("likedThoughts", JSON.stringify(likedThoughts));
}, [likedThoughts]);

const handleLike = (thoughtId) => {
if (!likedThoughts.includes(thoughtId)) {
setLikedThoughts((prevLikes) => [...prevLikes, thoughtId]);
setThoughts((prevThoughts) =>
prevThoughts.map((thought) =>
thought._id === thoughtId
? { ...thought, hearts: thought.hearts + 1 }
: thought
)
);
}
};

return (
<div className="app-container">
<ThoughtForm setThoughts={setThoughts} />
{thoughts.length === 0 ? (
<LoadingSpinner />
) : (
<ThoughtList
thoughts={thoughts}
onLike={handleLike}
likedThoughts={likedThoughts}
/>
)}
</div>
);
};

export default App;
Binary file added src/assets/heart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions src/components/LoadingSpinner.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.loading-spinner {
display: flex;
align-items: center;
justify-content: center;
font-size: 1.2rem;
font-family: 'Courier New', monospace;
color: #000000;
background-color: #ffffff;
padding: 20px;
border-radius: 12px;
}

.loading-spinner p {
margin: 0;
}
7 changes: 7 additions & 0 deletions src/components/LoadingSpinner.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import './LoadingSpinner.css';

export const LoadingSpinner = () => (
<div className="loading-spinner">
<p>Loading thoughts...</p>
</div>
);
58 changes: 58 additions & 0 deletions src/components/ThoughtForm.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
.thought-form {
background-color: #E0E0E0;
border: 1px solid #000000;
padding: 20px;
border-radius: 8px;
box-shadow: 8px 8px 0 #000000;
font-family: 'Courier New', monospace;
max-width: 400px;
width: 100%;
margin: 20px auto;
}

.thought-form h2 {
font-size: 1.5rem;
margin: 0 0 10px 0;
font-weight: bold;
color: #000000;
}

.thought-form textarea {
width: calc(100% - 22px); /* Ensures textarea is aligned with other elements */
height: 100px;
padding: 10px;
border: 1px solid #000000;
border-radius: 4px;
font-family: 'Courier New', monospace;
font-size: 1rem;
color: #000000;
background-color: #FFFFFF;
resize: none;
margin-bottom: 10px;
box-sizing: border-box;
}

.character-count {
font-size: 0.9rem;
color: #000000;
text-align: right;
margin-bottom: 15px;
}

.thought-form button {
width: 100%;
padding: 10px;
background-color: #000000;
color: #FFFFFF;
border: none;
font-size: 1rem;
font-family: 'Courier New', monospace;
cursor: pointer;
text-transform: uppercase;
transition: background-color 0.3s;
}

.thought-form button:hover {
background-color: #333333;
}

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

export const ThoughtForm = ({ setThoughts }) => {
const [thought, setThought] = useState("");
const [error, setError] = useState("");
const [isPosting, setIsPosting] = useState(false);

const minLength = 5;
const maxLength = 140;

const handleSubmit = async (e) => {
e.preventDefault();

if (thought.length < minLength) {
setError(`Thought must be at least ${minLength} characters.`);
return;
}

setIsPosting(true);
try {
const response = await fetch(
"https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts",
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: thought }),
}
);
if (!response.ok) throw new Error("Failed to post thought.");

const newThought = await response.json();
setThoughts((prevThoughts) => [newThought, ...prevThoughts]);
setThought(""); // This clears the input field
setError("");
} catch (err) {
setError("Something went wrong. Please try again.");
} finally {
setIsPosting(false);
}
};

return (
<div className="thought-form">
<h2>Share a happy thought</h2>
<form onSubmit={handleSubmit}>
<textarea
value={thought}
maxLength={maxLength}
onChange={(e) => {
setThought(e.target.value);
if (error && e.target.value.length >= minLength) {
setError(""); // This clears the errors if valid
}
}}
placeholder="What is making you happy right now?"
/>
<div className="character-count">{thought.length} of {maxLength}</div>
{error && <p className="error-message">{error}</p>}
<button type="submit" disabled={isPosting}>
{isPosting ? "Posting..." : "Post happy thought"}
</button>
</form>
</div>
);
};
53 changes: 53 additions & 0 deletions src/components/ThoughtItem.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.thought-item {
background-color: #FFFFFF;
border: 1px solid #000000;
border-radius: 8px;
padding: 10px 15px;
font-family: 'Courier New', monospace;
font-size: 1rem;
color: #000000;
box-shadow: 4px 4px 0 #000000;
width: 100%;
box-sizing: border-box;
}

.thought-item p {
margin: 0 0 10px 0;
}

.thought-item .bottom-row {
display: flex;
justify-content: space-between;
align-items: center;
font-size: 0.9rem;
color: #555555;
}

.thought-item .heart-container {
display: flex;
align-items: center;
gap: 5px;
}

.thought-item .heart-container button {
background: none;
border: none;
padding: 0;
cursor: pointer;
}

.thought-item .heart-container button.liked {
cursor: default;
opacity: 0.5;
}

.thought-item .heart-container img {
width: 20px;
height: 20px;
vertical-align: middle;
}

.thought-item .heart-container button.liked img {
filter: grayscale(100%);
}

37 changes: 37 additions & 0 deletions src/components/ThoughtItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import './ThoughtItem.css';
import likeIcon from '../assets/heart.png';
import moment from 'moment';

export const ThoughtItem = ({ thought, isLiked, onLike }) => {
const handleLike = async () => {
if (isLiked) return;

try {
await fetch(`https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts/${thought._id}/like`, {
method: 'POST',
});
onLike(thought._id);
} catch (error) {
console.error('Failed to like thought:', error);
}
};

return (
<div className="thought-item">
<p>{thought.message}</p>
<div className="bottom-row">
<div className="heart-container">
<button
onClick={handleLike}
disabled={isLiked} // Disable button if liked
className={isLiked ? 'liked' : ''} // Add 'liked' class if already liked
>
<img src={likeIcon} alt="like" />
</button>
<span>x {thought.hearts}</span>
</div>
<span className="timestamp">{moment(thought.createdAt).fromNow()}</span>
</div>
</div>
);
};
10 changes: 10 additions & 0 deletions src/components/ThoughtList.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.thought-list {
display: flex;
flex-direction: column;
gap: 10px;
max-width: 350px;
width: 100%;
font-family: Arial, sans-serif;
margin: 20px auto 0;
padding: 0;
}
15 changes: 15 additions & 0 deletions src/components/ThoughtList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import './ThoughtList.css';
import { ThoughtItem } from "./ThoughtItem";

export const ThoughtList = ({ thoughts, onLike, likedThoughts }) => (
<div className="thought-list">
{thoughts.map((thought) => (
<ThoughtItem
key={thought._id}
thought={thought}
isLiked={likedThoughts.includes(thought._id)}
onLike={onLike}
/>
))}
</div>
);
8 changes: 8 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}

.main-container {
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}

2 changes: 1 addition & 1 deletion src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { App } from "./App.jsx";
import App from "./App.jsx";
import "./index.css";

ReactDOM.createRoot(document.getElementById("root")).render(
Expand Down