diff --git a/src/components/CreateNote.tsx b/src/components/CreateNote.tsx index 4962a3d..ed8acea 100644 --- a/src/components/CreateNote.tsx +++ b/src/components/CreateNote.tsx @@ -3,11 +3,11 @@ import { api } from "~/utils/api"; import { NoteCard } from "./NoteCard"; import { NoteEditor } from "./NoteEditor"; import { useSelectedTopic } from "~/contexts/SelectedTopicContext"; -import { useState } from "react"; import { type RouterOutputs } from "~/utils/api"; type Note = RouterOutputs["note"]["getAll"][0]; import { toast } from "react-toastify"; import "react-toastify/dist/ReactToastify.css"; +import { useSelectedNote } from "~/contexts/SelectedNoteContext"; const InitialValues: Note = { content: '', @@ -21,7 +21,7 @@ const InitialValues: Note = { export const CreateNote: React.FC = () => { const { data: sessionData } = useSession(); const { selectedTopic, setSelectedTopic } = useSelectedTopic(); - const [selectedValues, setSelectedValues] = useState(InitialValues); + const { selectedNote, setSelectedNote, setIsEdit } = useSelectedNote(); api.topic.getAll.useQuery(undefined, { enabled: sessionData?.user !== undefined, @@ -78,7 +78,7 @@ export const CreateNote: React.FC = () => { note={note} onDelete={() => void deleteNote.mutate({ id: note.id })} onClickEdit={(note) => { - setSelectedValues(note); + setSelectedNote(note); }} /> @@ -91,7 +91,7 @@ export const CreateNote: React.FC = () => { content, topicId: selectedTopic?.id ?? "", }); - setSelectedValues(InitialValues); + setSelectedNote(InitialValues); }} onEdit={({ id, topicId, title, content }) => { void updateNote.mutate({ @@ -100,10 +100,10 @@ export const CreateNote: React.FC = () => { title, content, }); - setSelectedValues(InitialValues); + setSelectedNote(InitialValues); }} - isEdit={selectedValues.id !== '' ?? true} - values={selectedValues} + isForEdit={selectedNote?.id !== '' ?? setIsEdit(true)} + values={selectedNote || InitialValues} /> ); diff --git a/src/components/NoteCard.tsx b/src/components/NoteCard.tsx index 6424520..84ec4cf 100644 --- a/src/components/NoteCard.tsx +++ b/src/components/NoteCard.tsx @@ -13,7 +13,7 @@ export const NoteCard = ({ onDelete: () => void; onClickEdit: (note: Note) => void; }) => { - const [isExpanded, setIsExpanded] = useState(true); + const [isExpanded, setIsExpanded] = useState(false); const handleEditClick = () => { onClickEdit(note); }; @@ -22,23 +22,27 @@ export const NoteCard = ({
setIsExpanded(!isExpanded)} className={`collapse-arrow ${ isExpanded ? "collapse-open" : "" } collapse`} > -
{note.title}
+
setIsExpanded(!isExpanded)} + className="cursor-pointer collapse-title text-xl font-bold" + > + {note.title} +
{note.content}
-
-
+
+ {isForEdit ? ( + + ) : null}
diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index aafa680..1fc1230 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -1,7 +1,7 @@ import { useSession } from "next-auth/react"; import { useEffect, useMemo, useState } from "react"; import { api } from "~/utils/api"; -import { FaPlus, FaRegEdit } from "react-icons/fa"; +import { FaPlus, FaRegEdit, FaRegTimesCircle } from "react-icons/fa"; import { useSelectedTopic } from "~/contexts/SelectedTopicContext"; import { TopicsList } from "./TopicsList"; import { toast } from "react-toastify"; @@ -64,6 +64,12 @@ export const Sidebar: React.FC = () => { setInputValue(""); }; + const handleCancelClick = () => { + setInputValue(""); + setSelectedTopic(null); + setAction(""); + }; + useEffect(() => { if (isEditing) { setInputValue(selectedTopicTitle || ""); @@ -87,7 +93,7 @@ export const Sidebar: React.FC = () => { setInputValue(e.target.value); }} onKeyDown={(e) => { - if (e.key === "Enter") { + if (e.key === "Enter" && inputValue !== '') { if (isEditing) { updateTopic.mutate({ id: selectedTopicId, @@ -102,8 +108,19 @@ export const Sidebar: React.FC = () => { } }} /> + { action === "edit" ? ( + + ) : null } - +
+ + +
); }; diff --git a/src/contexts/SelectedNoteContext.tsx b/src/contexts/SelectedNoteContext.tsx new file mode 100644 index 0000000..9d53b55 --- /dev/null +++ b/src/contexts/SelectedNoteContext.tsx @@ -0,0 +1,52 @@ +import { type Note } from "@prisma/client"; +import React, { createContext, useContext, useState } from "react"; + +const InitialValues: Note = { + content: '', + createdAt: new Date(), + id: '', + title: '', + topicId: '', + updatedAt: new Date(), + }; + +interface SelectedNoteContextType { + selectedNote: Note | null; + setSelectedNote: (note: Note | null) => void; + isEdit: boolean | null; + setIsEdit: (set: boolean | null) => void; +} + +const SelectedNoteContext = createContext< + SelectedNoteContextType | undefined +>(undefined); + +export const SelectedNoteProvider: React.FC<{ children: React.ReactNode }> = ({ + children, +}) => { + const [selectedNote, setSelectedNote] = useState(InitialValues); + const [isEdit, setIsEdit] = useState(false); + + return ( + + {children} + + ); +}; + +export const useSelectedNote = () => { + const context = useContext(SelectedNoteContext); + if (context === undefined) { + throw new Error( + "useSelectedNote must be used within a SelectedNoteProvider" + ); + } + return context; +}; diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 350bbb1..fe16dcf 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -8,6 +8,7 @@ import { SelectedTopicProvider } from "~/contexts/SelectedTopicContext"; import { Loading } from "~/components/Loading"; import { LoadingProvider } from "~/contexts/LoadingContext"; import { ToastContainer } from "react-toastify"; +import { SelectedNoteProvider } from "~/contexts/SelectedNoteContext"; export default function Home() { const { data: sessionData, status: sessionLoading } = useSession(); @@ -25,7 +26,9 @@ export default function Home() { ) : sessionData?.user ? ( - + + + ) : ( diff --git a/src/styles/globals.css b/src/styles/globals.css index 64a30e8..31265f2 100644 --- a/src/styles/globals.css +++ b/src/styles/globals.css @@ -15,4 +15,12 @@ .min-h { min-height: 45vh; } + + .widthMobile { + width: 100%; + } + + .flexWrow { + flex-grow: 1; + } } \ No newline at end of file