diff --git a/README.md b/README.md index 6fc6d095..1e41cdd3 100644 --- a/README.md +++ b/README.md @@ -6,27 +6,15 @@ # 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. +The goal of this project was to build a positive social feed for sharing "happy thoughts" by utilizing React’s state management capabilities to fetch, display, and post data to an API. I started by analyzing the structure and requirements, breaking it down into three key areas: fetching recent thoughts, posting new ones, and implementing a "like" feature. Planning involved deciding on the component structure and defining the necessary state for each component to ensure smooth interactions and an intuitive user experience. -## Getting Started with the Project +For the build, I used React to manage component state and effects, enabling efficient data fetching and updating. useEffect was used to load initial data from the API upon component mounting, and useState to handle form submissions and like counts. For an improved user experience, I implemented an optimistic UI update, where new thoughts appear instantly in the feed before the API has confirmed the post. This approach provided fast feedback for users, especially for mobile responsiveness and handling slow network connections. -### Dependency Installation & Startup Development Server - -Once cloned, navigate to the project's root directory and this project uses npm (Node Package Manager) to manage its dependencies. - -The command below is a combination of installing dependencies, opening up the project on VS Code and it will run a development server on your terminal. - -```bash -npm i && code . && npm run dev -``` - -### 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? +If I had more time, I would focus on refining accessibility by improving screen reader support, adding animations for new thoughts, and implementing a loading spinner to indicate the fetching process. Adding validation feedback and character counting would further enhance the app's interactivity and user experience. ### 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://happy-thoughts-project-technigo.netlify.app/ ## Instructions diff --git a/package.json b/package.json index 74245b0c..7bef2983 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "preview": "vite preview" }, "dependencies": { + "date-fns": "^4.1.0", "react": "^18.2.0", "react-dom": "^18.2.0" }, diff --git a/src/AddPost/AddPost.jsx b/src/AddPost/AddPost.jsx new file mode 100644 index 00000000..d5ef8f31 --- /dev/null +++ b/src/AddPost/AddPost.jsx @@ -0,0 +1,13 @@ +// AddPost.jsx + +import { PostForm } from "./AddPostComponents/PostForm"; + +export const AddPost = ({ addNewThought, url }) => { + return ( +
+

What's making you happy right now?

+ + +
+ ); +}; \ No newline at end of file diff --git a/src/AddPost/AddPostComponents/PostForm.jsx b/src/AddPost/AddPostComponents/PostForm.jsx new file mode 100644 index 00000000..00d269fd --- /dev/null +++ b/src/AddPost/AddPostComponents/PostForm.jsx @@ -0,0 +1,57 @@ +// PostForm.jsx + +import { useState } from "react"; + +export const PostForm = ({ addNewThought, url }) => { + // Hooks + const [newThought, setNewThought] = useState(""); + const [submitting, setSubmitting] = useState(false); + + // Handle submit on form and POST to API + const handleSubmit = async (event) => { + event.preventDefault(); // Prevent site to update + setSubmitting(true); // For loading message on the button + + try { + const response = await fetch(url, { // Use URL from App.jsx + method: "POST", // POST to API + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ message: newThought }), + }); + + if (response.ok) { + const result = await response.json(); + addNewThought(result); // Send new thought to App.jsx + setNewThought(""); // Clean input/text field + } else { + console.error("Post failed:", response.status); + } + } catch (error) { + console.error("Error during post:", error); + } finally { + setSubmitting(false); // No loading message on button anymore + } + }; + + return ( +
+