From cfc9a7e1f0a637eb8c57187a635345ce7939c7eb Mon Sep 17 00:00:00 2001 From: keannu125 Date: Sat, 4 Mar 2023 23:04:02 +0800 Subject: [PATCH] Fixed prompts for when there no connection to backend --- src/Components/Api/Api.tsx | 37 ++++++++++++++++++-------------- src/Components/Notes/Notes.tsx | 13 ++++++----- src/Routes/UserPage/UserPage.tsx | 20 +++++++++++------ 3 files changed, 41 insertions(+), 29 deletions(-) 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

-
- ); } else if (error) { return (

Error contacting Notes server

); + } else if (!logged_in) { + return ( +
+

Please login to use Clip Notes

+
+ ); } else if (notes.length === 0) { return (
@@ -51,7 +51,6 @@ export default function Notes() {
); } - return ( <> {notes.map((note: NoteProps, index: number) => { diff --git a/src/Routes/UserPage/UserPage.tsx b/src/Routes/UserPage/UserPage.tsx index 0085caf..33a84c7 100644 --- a/src/Routes/UserPage/UserPage.tsx +++ b/src/Routes/UserPage/UserPage.tsx @@ -12,30 +12,38 @@ export default function UserPage() { return (
-

Loading...

+
+

Loading...

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

An error has occured

+
+

An error has occured

+
); } else if (!logged_in) { return (
-

Please login to view user info

+
+

Please login to view user info

+
); } return (
-

Username: {data.username}

-

Email: {data.email}

-

User ID: {data.id}

+
+

Username: {data.username}

+

Email: {data.email}

+

User ID: {data.id}

+
); }