Skip to content
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
Binary file modified server/notes.db
Binary file not shown.
88 changes: 74 additions & 14 deletions src/components/CreateNoteForm.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,85 @@
// TODO: Import useForm, zodResolver, axios, useNavigate, useState, and noteSchema


import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import axios from "axios";
import { useState } from "react";
import { noteSchema } from "../schema/notes";
import { Save } from "lucide-react";
import { useNavigate } from "react-router-dom";

const CreateNoteForm = () => {
// TODO: Setup isSubmitting state with useState
// TODO: create navigate variable and set to useNavigate()

const CreateNoteForm = ({ onSubmitSuccess }) => {
const [isSubmitting, setIsSubmitting] = useState(false);
const navigate = useNavigate()

// TODO: Set up the form with useForm from react-hook-form and zodResolver from @hookform/resolvers/zod
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
resolver: zodResolver(noteSchema),
});

const sendToTheServer = async (data) => {
// TODO: Send the data to the server
// TODO: Use axios to create a new note in the server using the endpoint http://localhost:3001/api/notes
try {
setIsSubmitting(true);
await axios.post("http://localhost:3001/api/notes", data);
if (onSubmitSuccess) {
onSubmitSuccess(); // notify parent page

}
setTimeout(() => {
navigate("/notes");
}, 2000);
} catch (error) {
console.error("Error creating note:", error);
} finally {
setIsSubmitting(false);
}
};

return (
<>
<h1>Create Note</h1>
{/* TODO: Setup the form with TailwindCSS, create a form with the following fields: title, content, and submit button */}
</>
<form
onSubmit={handleSubmit(sendToTheServer)}
className="bg-white shadow-lg rounded-xl p-6 max-w-md mx-auto"
>
{/* Title */}
<label className="block text-gray-700 font-semibold mb-2">Title</label>
<input
type="text"
placeholder="Note Title"
{...register("title")}
className={`w-full border ${
errors.title ? "border-red-500" : "border-gray-300"
} rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-400 transition`}
/>
{errors.title && (
<p className="text-red-500 text-sm mt-1">{errors.title.message}</p>
)}

{/* Content */}
<label className="block text-gray-700 font-semibold mt-4 mb-2">
Content
</label>
<textarea
placeholder="Write your note here..."
{...register("content")}
className={`w-full h-32 border ${
errors.content ? "border-red-500" : "border-gray-300"
} rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-400 transition resize-none`}
></textarea>
{errors.content && (
<p className="text-red-500 text-sm mt-1">{errors.content.message}</p>
)}

{/* Submit Button */}
<button
type="submit"
disabled={isSubmitting}
className="mt-4 w-full bg-yellow-500 text-white font-semibold py-2 rounded-lg flex items-center justify-center gap-2 hover:bg-yellow-600 transition disabled:opacity-50"
>
<Save className="w-5 h-5" />
{isSubmitting ? "Saving..." : "Save Note"}
</button>
</form>
);
};

Expand Down
8 changes: 8 additions & 0 deletions src/schema/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@ export const noteSchema = z.object({
//TODO: create the title and content schema,
// Make sure the title is required and the content is required
// Make sure the title is max 50 characters and the content is max 500 characters
title: z
.string()
.min(1, "Title is required")
.max(50, "Title must be at most 50 characters"),
content: z
.string()
.min(1, "Content is required")
.max(500, "Content must be at most 500 characters"),

});