diff --git a/src/Components/Api/Api.tsx b/src/Components/Api/Api.tsx index 70d018b..18cce4c 100644 --- a/src/Components/Api/Api.tsx +++ b/src/Components/Api/Api.tsx @@ -10,7 +10,7 @@ import { // Note APIs export function GetNotes() { - const token = JSON.parse(localStorage.getItem("token") || ""); + const token = JSON.parse(localStorage.getItem("token") || "{}"); return axios .get("http://localhost:8000/api/v1/notes/", { headers: { @@ -23,7 +23,7 @@ export function GetNotes() { } export function GetNote(id: number) { - const token = JSON.parse(localStorage.getItem("token") || ""); + const token = JSON.parse(localStorage.getItem("token") || "{}"); return axios .get("http://localhost:8000/api/v1/notes/" + id + "/", { headers: { @@ -36,7 +36,7 @@ export function GetNote(id: number) { } export function UpdateNote(note: UpdateNoteParams) { - const token = JSON.parse(localStorage.getItem("token") || ""); + const token = JSON.parse(localStorage.getItem("token") || "{}"); return axios .patch("http://localhost:8000/api/v1/notes/" + note.id + "/", note, { headers: { @@ -53,7 +53,7 @@ export function UpdateNote(note: UpdateNoteParams) { } export function AddNote(note: AddNoteParams) { - const token = JSON.parse(localStorage.getItem("token") || ""); + const token = JSON.parse(localStorage.getItem("token") || "{}"); return axios .post("http://localhost:8000/api/v1/notes/", note, { headers: { @@ -62,16 +62,25 @@ export function AddNote(note: AddNoteParams) { }) .then((response) => { return response.data; + }) + .catch((error) => { + console.log("Error adding note", error); + return error; }); } export function DeleteNote(id: number) { - const token = JSON.parse(localStorage.getItem("token") || ""); - return axios.delete("http://localhost:8000/api/v1/notes/" + id + "/", { - headers: { - Authorization: "Token " + token, - }, - }); + const token = JSON.parse(localStorage.getItem("token") || "{}"); + return axios + .delete("http://localhost:8000/api/v1/notes/" + id + "/", { + headers: { + Authorization: "Token " + token, + }, + }) + .catch((error) => { + console.log("Error deleting note", error); + return error; + }); } // User APIs @@ -96,7 +105,7 @@ export function UserLogin(user: LoginParams) { localStorage.setItem("token", JSON.stringify(response.data.auth_token)); console.log( "Login Success! Stored Token: ", - JSON.parse(localStorage.getItem("token") || "") + JSON.parse(localStorage.getItem("token") || "{}") ); return true; }) @@ -107,7 +116,7 @@ export function UserLogin(user: LoginParams) { } export function UserInfo() { - const token = JSON.parse(localStorage.getItem("token") || ""); + const token = JSON.parse(localStorage.getItem("token") || "{}"); return axios .get("http://localhost:8000/api/v1/accounts/users/me/", { headers: { @@ -117,10 +126,6 @@ export function UserInfo() { .then((response) => { console.log(response.data); return response.data; - }) - .catch((error) => { - console.log("Error in fetching user data"); - console.log(error); }); } diff --git a/src/Components/Notes/Notes.tsx b/src/Components/Notes/Notes.tsx index bc3a4f5..4cccaf2 100644 --- a/src/Components/Notes/Notes.tsx +++ b/src/Components/Notes/Notes.tsx @@ -22,18 +22,18 @@ export default function Notes() {
Loading Notes...
); - } else if (!logged_in && !error) { - return ( -Please login to use Clip Notes
-Error contacting Notes server
Please login to use Clip Notes
+Loading...
+Loading...
+An error has occured
+An error has occured
+Please login to view user info
+Please login to view user info
+Username: {data.username}
-Email: {data.email}
-User ID: {data.id}
+Username: {data.username}
+Email: {data.email}
+User ID: {data.id}
+