diff --git a/src/Components/Api/Api.tsx b/src/Components/Api/Api.tsx index a9478b3..be99e4c 100644 --- a/src/Components/Api/Api.tsx +++ b/src/Components/Api/Api.tsx @@ -3,9 +3,16 @@ import axios from "axios"; // Note APIs export function GetNotes() { - return axios.get("http://localhost:8000/api/v1/notes/").then((response) => { - return response.data; - }); + const token = JSON.parse(localStorage.getItem("token") || ""); + return axios + .get("http://localhost:8000/api/v1/notes/", { + headers: { + Authorization: "Token " + token, + }, + }) + .then((response) => { + return response.data; + }); } export interface note { @@ -14,15 +21,25 @@ export interface note { } export function AddNote(note: note) { + const token = JSON.parse(localStorage.getItem("token") || ""); return axios - .post("http://localhost:8000/api/v1/notes/", note) + .post("http://localhost:8000/api/v1/notes/", note, { + headers: { + Authorization: "Token " + token, + }, + }) .then((response) => { return response.data; }); } export function DeleteNote(id: number) { - return axios.delete("http://localhost:8000/api/v1/notes/" + id + "/"); + const token = JSON.parse(localStorage.getItem("token") || ""); + return axios.delete("http://localhost:8000/api/v1/notes/" + id + "/", { + headers: { + Authorization: "Token " + token, + }, + }); } // User APIs diff --git a/src/Components/Notes/Notes.tsx b/src/Components/Notes/Notes.tsx index 630a34a..96b7ada 100644 --- a/src/Components/Notes/Notes.tsx +++ b/src/Components/Notes/Notes.tsx @@ -5,6 +5,7 @@ import Note from "../Note/Note"; import { Button } from "@mui/material"; import { useQuery } from "react-query"; import { GetNotes } from "../Api/Api"; +import { useSelector } from "react-redux"; export default function Notes() { const navigate = useNavigate(); @@ -13,21 +14,29 @@ export default function Notes() { isLoading, error, } = useQuery("notes", GetNotes, { retry: 0 }); + const logged_in = useSelector( + (state: { Login: { logged_in: boolean } }) => state.Login.logged_in + ); + if (!logged_in) { + return ( +
+

Please login to use Clip Notes

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

Error contacting Notes server

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

Loading Notes...

); - } - if (notes.length === 0) { + } else if (notes.length === 0) { return (

No notes exist yet