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 Project - Cholpon #102

Open
wants to merge 7 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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
</a>
</h1>

### View it live

https://happythoughtscholpon.netlify.app/


# Happy thoughts Project

In this week's project, you'll be able to practice your React state skills by fetching and posting data to an API.
Expand All @@ -24,9 +29,7 @@ npm i && code . && npm run dev

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?

### 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.


## Instructions

Expand Down
15 changes: 12 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Happy Thought - Project - Week 7</title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Happy Thoughts</title>
<link rel="stylesheet" href="/src/index.css" />


<!-- Font import -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Afacad+Flux:[email protected]&family=Cinzel:[email protected]&family=Josefin+Sans:ital,wght@0,100..700;1,100..700&family=Montserrat:ital,wght@0,100..900;1,100..900&family=Open+Sans:ital,wght@0,300..800;1,300..800&display=swap" rel="stylesheet">

</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
</html>
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": {
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
59 changes: 56 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
export const App = () => {
return <div>Find me in src/app.jsx!</div>;
};
import React, { useState, useEffect } from 'react';
import ThoughtsList from './components/ThoughtsList';
import ThoughtForm from './components/ThoughtForm';

function App() {
const [thoughts, setThoughts] = useState([]);

useEffect(() => {
const fetchThoughts = async () => {
const response = await fetch('https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts');
const data = await response.json();
const sortedThoughts = data.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
setThoughts(sortedThoughts);
};

fetchThoughts();
}, []);

const addNewThought = (newThought) => {
setThoughts([newThought, ...thoughts]);
};


const handleLike = async (thoughtId) => {
try {
const response = await fetch(
`https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts/${thoughtId}/like`,
{
method: 'POST',
}
);

if (response.ok) {
setThoughts((prevThoughts) =>
prevThoughts.map((thought) =>
thought._id === thoughtId ? { ...thought, hearts: thought.hearts + 1 } : thought
)
);
} else {
console.error('Failed to like the thought');
}
} catch (error) {
console.error('Error:', error);
}
};

return (
<div className="App">
<h1>Happy Thoughts</h1>
<ThoughtForm onNewThought={addNewThought} />
<ThoughtsList thoughts={thoughts} onLike={handleLike} />
</div>
);
}

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

const ThoughtForm = ({ onNewThought }) => {
const [message, setMessage] = useState('');

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

if (message.length < 5 || message.length > 140) {
alert("Message must be between 5 and 140 characters.");
return;
}

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 }),
});

if (!response.ok) throw new Error('Failed to send thought');

const newThought = await response.json();
onNewThought(newThought);
setMessage('');
} catch (error) {
console.error('Error sending thought:', error);
}
};

return (
<form onSubmit={handleSubmit} className="thought-form">
<textarea
value={message}
onChange={(e) => setMessage(e.target.value)}
placeholder="What's making you happy right now?"
rows="3"
/>
<button type="submit">Post</button>
</form>
);
};

export default ThoughtForm;
20 changes: 20 additions & 0 deletions src/components/ThoughtsList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';

const ThoughtsList = ({ thoughts, onLike }) => {
return (
<div className="thoughts-list">
{thoughts.map((thought) => (
<div key={thought._id} className="thought">
<p>{thought.message}</p>
<div className="like-section">
<button className="like-button" onClick={() => onLike(thought._id)}>❤️</button>
<span className="like-count"> x {thought.hearts}</span>
</div>
<span className="timestamp">{new Date(thought.createdAt).toLocaleString()}</span>
</div>
))}
</div>
);
};

export default ThoughtsList;
Loading