Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

fix: edit note issue

Khuram Niaz1d53dc7c010d
diff --git a/src/components/myjobs/NotesCollapsibleSection.tsx b/src/components/myjobs/NotesCollapsibleSection.tsx
index 4831051..3f938c4 100644
--- a/src/components/myjobs/NotesCollapsibleSection.tsx
+++ b/src/components/myjobs/NotesCollapsibleSection.tsx
@@ -1,31 +1,38 @@
"use client";
-import { useCallback, useEffect, useState } from "react";
+import { useCallback, useEffect, useState, useTransition } from "react";
import { NoteResponse } from "@/models/note.model";
-import { getNotesByJobId, deleteNote } from "@/actions/note.actions";
+import {
+ getNotesByJobId,
+ deleteNote,
+ addNote,
+ updateNote,
+} from "@/actions/note.actions";
import { NoteCard } from "./NoteCard";
-import { NoteDialog } from "./NoteDialog";
-import { DeleteAlertDialog } from "../DeleteAlertDialog";
import { Button } from "../ui/button";
import { Badge } from "../ui/badge";
-import { ChevronDown, PlusCircle, StickyNote } from "lucide-react";
+import { ChevronDown, Loader, PlusCircle, StickyNote } from "lucide-react";
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "../ui/collapsible";
import { toast } from "../ui/use-toast";
+import TiptapEditor from "../TiptapEditor";
type NotesCollapsibleSectionProps = {
jobId: string;
};
-export function NotesCollapsibleSection({ jobId }: NotesCollapsibleSectionProps) {
+export function NotesCollapsibleSection({
+ jobId,
+}: NotesCollapsibleSectionProps) {
const [notes, setNotes] = useState<NoteResponse[]>([]);
const [isOpen, setIsOpen] = useState(false);
- const [dialogOpen, setDialogOpen] = useState(false);
- const [editNote, setEditNote] = useState<NoteResponse | null>(null);
- const [deleteAlertOpen, setDeleteAlertOpen] = useState(false);
- const [noteIdToDelete, setNoteIdToDelete] = useState("");
+ const [editingNote, setEditingNote] = useState<NoteResponse | null>(null);
+ const [isAdding, setIsAdding] = useState(false);
+ const [editorContent, setEditorContent] = useState("");
+ const [deleteConfirmId, setDeleteConfirmId] = useState<string | null>(null);
+ const [isPending, startTransition] = useTransition();
const loadNotes = useCallback(async () => {
const result = await getNotesByJobId(jobId);
@@ -38,99 +45,201 @@ export function NotesCollapsibleSection({ jobId }: NotesCollapsibleSectionProps)
loadNotes();
}, [loadNotes]);
+ const handleAddNote = () => {
+ setEditingNote(null);
+ setEditorContent("");
+ setIsAdding(true);
+ setIsOpen(true);
+ };
+
const handleEdit = (note: NoteResponse) => {
- setEditNote(note);
- setDialogOpen(true);
+ setIsAdding(false);
+ setEditingNote(note);
+ setEditorContent(note.content);
};
- const handleDeleteClick = (noteId: string) => {
- setNoteIdToDelete(noteId);
- setDeleteAlertOpen(true);
+ const handleCancel = () => {
+ setIsAdding(false);
+ setEditingNote(null);
+ setEditorContent("");
};
- const handleDelete = async () => {
- const result = await deleteNote(noteIdToDelete);
- if (result.success) {
- toast({
- variant: "success",
- description: "Note deleted successfully",
- });
- loadNotes();
- } else {
- toast({
- variant: "destructive",
- title: "Error!",
- description: result.message,
- });
- }
+ const handleSave = () => {
+ if (!editorContent.trim()) return;
+
+ startTransition(async () => {
+ const result = editingNote
+ ? await updateNote({
+ id: editingNote.id,
+ jobId,
+ content: editorContent,
+ })
+ : await addNote({ jobId, content: editorContent });
+
+ if (result.success) {
+ toast({
+ variant: "success",
+ description: `Note ${editingNote ? "updated" : "added"} successfully`,
+ });
+ handleCancel();
+ loadNotes();
+ } else {
+ toast({
+ variant: "destructive",
+ title: "Error!",
+ description: result.message,
+ });
+ }
+ });
};
- const handleAddNote = () => {
- setEditNote(null);
- setDialogOpen(true);
+ const handleDeleteClick = (noteId: string) => {
+ setDeleteConfirmId(noteId);
};
- const handleSaved = () => {
- setEditNote(null);
- loadNotes();
+ const handleDeleteConfirm = () => {
+ if (!deleteConfirmId) return;
+
+ startTransition(async () => {
+ const result = await deleteNote(deleteConfirmId);
+ if (result.success) {
+ toast({
+ variant: "success",
+ description: "Note deleted successfully",
+ });
+ setDeleteConfirmId(null);
+ loadNotes();
+ } else {
+ toast({
+ variant: "destructive",
+ title: "Error!",
+ description: result.message,
+ });
+ }
+ });
};
+ const inlineEditor = (
+ <div className="border rounded-lg p-4 space-y-3">
+ <p className="text-sm font-medium">
+ {editingNote ? "Edit Note" : "Add Note"}
+ </p>
+ <TiptapEditor
+ field={
+ {
+ value: editorContent,
+ onChange: (val: string) => setEditorContent(val),
+ onBlur: () => {},
+ name: "content" as const,
+ ref: () => {},
+ } as any
+ }
+ />
+ <div className="flex justify-end gap-2">
+ <Button
+ type="button"
+ variant="outline"
+ size="sm"
+ onClick={handleCancel}
+ >
+ Cancel
+ </Button>
+ <Button
+ type="button"
+ size="sm"
+ onClick={handleSave}
+ disabled={isPending || !editorContent.trim()}
+ >
+ Save
+ {isPending && <Loader className="ml-2 h-4 w-4 shrink-0 spinner" />}
+ </Button>
+ </div>
+ </div>
+ );
+
return (
- <>
- <Collapsible open={isOpen} onOpenChange={setIsOpen} className="md:col-span-2">
- <div className="flex items-center gap-2">
- <CollapsibleTrigger className="flex items-center gap-2 hover:opacity-80">
- <StickyNote className="h-4 w-4" />
- <span className="text-sm font-medium">Notes</span>
- {notes.length > 0 && (
- <Badge variant="secondary" className="text-xs">
- {notes.length}
- </Badge>
- )}
- <ChevronDown
- className={`h-4 w-4 transition-transform ${isOpen ? "rotate-180" : ""}`}
- />
- </CollapsibleTrigger>
- <Button
- variant="outline"
- size="sm"
- type="button"
- className="h-7 gap-1 ml-auto"
- onClick={handleAddNote}
- >
- <PlusCircle className="h-3.5 w-3.5" />
- Add Note
- </Button>
- </div>
- <CollapsibleContent className="mt-3 space-y-3">
- {notes.length === 0 ? (
- <p className="text-sm text-muted-foreground">No notes yet.</p>
- ) : (
- notes.map((note) => (
+ <Collapsible
+ open={isOpen}
+ onOpenChange={setIsOpen}
+ className="md:col-span-2"
+ >
+ <div className="flex items-center gap-2">
+ <CollapsibleTrigger className="flex items-center gap-2 hover:opacity-80">
+ <StickyNote className="h-4 w-4" />
+ <span className="text-sm font-medium">Notes</span>
+ {notes.length > 0 && (
+ <Badge variant="secondary" className="text-xs">
+ {notes.length}
+ </Badge>
+ )}
+ <ChevronDown
+ className={`h-4 w-4 transition-transform ${isOpen ? "rotate-180" : ""}`}
+ />
+ </CollapsibleTrigger>
+ <Button
+ variant="outline"
+ size="sm"
+ type="button"
+ className="h-7 gap-1 ml-auto"
+ onClick={handleAddNote}
+ >
+ <PlusCircle className="h-3.5 w-3.5" />
+ New Note
+ </Button>
+ </div>
+ <CollapsibleContent className="mt-3 space-y-3">
+ {isAdding && inlineEditor}
+ {notes.length === 0 && !isAdding ? (
+ <p className="text-sm text-muted-foreground">No notes yet.</p>
+ ) : (
+ notes.map((note) =>
+ editingNote?.id === note.id ? (
+ <div key={note.id}>{inlineEditor}</div>
+ ) : deleteConfirmId === note.id ? (
+ <div
+ key={note.id}
+ className="border border-destructive rounded-lg p-4 space-y-3"
+ >
+ <p className="text-sm font-medium">
+ Are you sure you want to delete this note?
+ </p>
+ <p className="text-sm text-muted-foreground">
+ This action cannot be undone.
+ </p>
+ <div className="flex justify-end gap-2">
+ <Button
+ type="button"
+ variant="outline"
+ size="sm"
+ onClick={() => setDeleteConfirmId(null)}
+ >
+ Cancel
+ </Button>
+ <Button
+ type="button"
+ variant="destructive"
+ size="sm"
+ onClick={handleDeleteConfirm}
+ disabled={isPending}
+ >
+ Delete
+ {isPending && (
+ <Loader className="ml-2 h-4 w-4 shrink-0 spinner" />
+ )}
+ </Button>
+ </div>
+ </div>
+ ) : (
<NoteCard
key={note.id}
note={note}
onEdit={handleEdit}
onDelete={handleDeleteClick}
/>
- ))
- )}
- </CollapsibleContent>
- </Collapsible>
-
- <NoteDialog
- open={dialogOpen}
- onOpenChange={setDialogOpen}
- jobId={jobId}
- editNote={editNote}
- onSaved={handleSaved}
- />
- <DeleteAlertDialog
- pageTitle="note"
- open={deleteAlertOpen}
- onOpenChange={setDeleteAlertOpen}
- onDelete={handleDelete}
- />
- </>
+ ),
+ )
+ )}
+ </CollapsibleContent>
+ </Collapsible>
);
}
diff --git a/src/components/myjobs/NotesSection.tsx b/src/components/myjobs/NotesSection.tsx
index 47e5bbc..5d416da 100644
--- a/src/components/myjobs/NotesSection.tsx
+++ b/src/components/myjobs/NotesSection.tsx
@@ -99,7 +99,7 @@ export function NotesSection({ jobId }: NotesSectionProps) {
onClick={handleAddNote}
>
<PlusCircle className="h-3.5 w-3.5" />
- Add Note
+ New Note
</Button>
</div>
<CollapsibleContent className="mt-3 space-y-3">