diff --git a/src/Components/Api/Api.tsx b/src/Components/Api/Api.tsx index 000b057..84bd8b3 100644 --- a/src/Components/Api/Api.tsx +++ b/src/Components/Api/Api.tsx @@ -1,6 +1,7 @@ import axios from "axios"; import { ActivationParams, + UpdateNoteParams, AddNoteParams, LoginParams, RegistrationParams, @@ -21,6 +22,36 @@ export function GetNotes() { }); } +export function GetNote(id: number) { + const token = JSON.parse(localStorage.getItem("token") || ""); + return axios + .get("http://localhost:8000/api/v1/notes/" + id + "/", { + headers: { + Authorization: "Token " + token, + }, + }) + .then((response) => { + return response.data; + }); +} + +export function UpdateNote(note: UpdateNoteParams) { + const token = JSON.parse(localStorage.getItem("token") || ""); + return axios + .patch("http://localhost:8000/api/v1/notes/" + note.id + "/", note, { + headers: { + Authorization: "Token " + token, + }, + }) + .then((response) => { + return response.data; + }) + .catch((error) => { + console.log("Error updating note", error); + return error; + }); +} + export function AddNote(note: AddNoteParams) { const token = JSON.parse(localStorage.getItem("token") || ""); return axios diff --git a/src/Components/Note/Note.tsx b/src/Components/Note/Note.tsx index 39685da..59003e7 100644 --- a/src/Components/Note/Note.tsx +++ b/src/Components/Note/Note.tsx @@ -4,8 +4,10 @@ import { Button } from "@mui/material"; import { useMutation, useQueryClient } from "react-query"; import { DeleteNote } from "../Api/Api"; import { NoteProps } from "../../Interfaces/Interfaces"; +import { useNavigate } from "react-router-dom"; export default function Note(props: NoteProps) { + const navigate = useNavigate(); const queryClient = useQueryClient(); const mutation = useMutation({ mutationFn: DeleteNote, @@ -28,15 +30,26 @@ export default function Note(props: NoteProps) {

Timestamp: {String(props.date_created)}

- +
+ + +
); diff --git a/src/Interfaces/Interfaces.tsx b/src/Interfaces/Interfaces.tsx index 58219e8..afd5f25 100644 --- a/src/Interfaces/Interfaces.tsx +++ b/src/Interfaces/Interfaces.tsx @@ -50,3 +50,9 @@ export interface AddNoteParams { title: string; content: string; } + +export interface UpdateNoteParams { + id: number; + title: string; + content: string; +} diff --git a/src/Routes/ViewEditNote/ViewEditNote.tsx b/src/Routes/ViewEditNote/ViewEditNote.tsx new file mode 100644 index 0000000..62b5009 --- /dev/null +++ b/src/Routes/ViewEditNote/ViewEditNote.tsx @@ -0,0 +1,115 @@ +import styles from "../../styles"; +import { Button } from "@mui/material"; +import { useNavigate, useParams } from "react-router-dom"; +import { useEffect, useState } from "react"; +import Header from "../../Components/Header/Header"; +import { AddNote, GetNote, UpdateNote } from "../../Components/Api/Api"; +import { useMutation, useQuery, useQueryClient } from "react-query"; +import { NoteProps } from "../../Interfaces/Interfaces"; + +export interface input { + e: React.ChangeEvent; +} +export default function ViewNote() { + const navigate = useNavigate(); + const { id } = useParams(); + const queryClient = useQueryClient(); + const mutation = useMutation({ + mutationFn: UpdateNote, + onSuccess: () => { + queryClient.invalidateQueries("notes"); + }, + }); + const [note, setNote] = useState({ + title: "test", + content: "test", + }); + async function retrieve() { + let a = await GetNote(Number(id)); + setNote(a); + return a; + } + const { data, isLoading, error } = useQuery("note", retrieve, { + retry: 0, + }); + useEffect(() => { + setNote(data); + }, []); + if (error) { + return ( +
+
+
+

Error retrieving specific note

+
+
+ ); + } + if (isLoading) { + return ( +
+

Loading Note...

+
+ ); + } + if (data) { + return ( +
+
+

Edit Note

+
+
+
+

Title: 

+ { + setNote({ ...note, title: e.target.value }); + }} + maxLength={20} + /> +
+
+