Fixed prompts for when there no connection to backend

This commit is contained in:
keannu125 2023-03-04 23:04:02 +08:00
parent eea71c8370
commit cfc9a7e1f0
3 changed files with 41 additions and 29 deletions

View file

@ -10,7 +10,7 @@ import {
// Note APIs // Note APIs
export function GetNotes() { export function GetNotes() {
const token = JSON.parse(localStorage.getItem("token") || ""); const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return axios
.get("http://localhost:8000/api/v1/notes/", { .get("http://localhost:8000/api/v1/notes/", {
headers: { headers: {
@ -23,7 +23,7 @@ export function GetNotes() {
} }
export function GetNote(id: number) { export function GetNote(id: number) {
const token = JSON.parse(localStorage.getItem("token") || ""); const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return axios
.get("http://localhost:8000/api/v1/notes/" + id + "/", { .get("http://localhost:8000/api/v1/notes/" + id + "/", {
headers: { headers: {
@ -36,7 +36,7 @@ export function GetNote(id: number) {
} }
export function UpdateNote(note: UpdateNoteParams) { export function UpdateNote(note: UpdateNoteParams) {
const token = JSON.parse(localStorage.getItem("token") || ""); const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return axios
.patch("http://localhost:8000/api/v1/notes/" + note.id + "/", note, { .patch("http://localhost:8000/api/v1/notes/" + note.id + "/", note, {
headers: { headers: {
@ -53,7 +53,7 @@ export function UpdateNote(note: UpdateNoteParams) {
} }
export function AddNote(note: AddNoteParams) { export function AddNote(note: AddNoteParams) {
const token = JSON.parse(localStorage.getItem("token") || ""); const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return axios
.post("http://localhost:8000/api/v1/notes/", note, { .post("http://localhost:8000/api/v1/notes/", note, {
headers: { headers: {
@ -62,16 +62,25 @@ export function AddNote(note: AddNoteParams) {
}) })
.then((response) => { .then((response) => {
return response.data; return response.data;
})
.catch((error) => {
console.log("Error adding note", error);
return error;
}); });
} }
export function DeleteNote(id: number) { export function DeleteNote(id: number) {
const token = JSON.parse(localStorage.getItem("token") || ""); const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios.delete("http://localhost:8000/api/v1/notes/" + id + "/", { return axios
headers: { .delete("http://localhost:8000/api/v1/notes/" + id + "/", {
Authorization: "Token " + token, headers: {
}, Authorization: "Token " + token,
}); },
})
.catch((error) => {
console.log("Error deleting note", error);
return error;
});
} }
// User APIs // User APIs
@ -96,7 +105,7 @@ export function UserLogin(user: LoginParams) {
localStorage.setItem("token", JSON.stringify(response.data.auth_token)); localStorage.setItem("token", JSON.stringify(response.data.auth_token));
console.log( console.log(
"Login Success! Stored Token: ", "Login Success! Stored Token: ",
JSON.parse(localStorage.getItem("token") || "") JSON.parse(localStorage.getItem("token") || "{}")
); );
return true; return true;
}) })
@ -107,7 +116,7 @@ export function UserLogin(user: LoginParams) {
} }
export function UserInfo() { export function UserInfo() {
const token = JSON.parse(localStorage.getItem("token") || ""); const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return axios
.get("http://localhost:8000/api/v1/accounts/users/me/", { .get("http://localhost:8000/api/v1/accounts/users/me/", {
headers: { headers: {
@ -117,10 +126,6 @@ export function UserInfo() {
.then((response) => { .then((response) => {
console.log(response.data); console.log(response.data);
return response.data; return response.data;
})
.catch((error) => {
console.log("Error in fetching user data");
console.log(error);
}); });
} }

View file

@ -22,18 +22,18 @@ export default function Notes() {
<p style={styles.text_medium}>Loading Notes...</p> <p style={styles.text_medium}>Loading Notes...</p>
</div> </div>
); );
} else if (!logged_in && !error) {
return (
<div style={styles.note}>
<p style={styles.text_medium}>Please login to use Clip Notes</p>
</div>
);
} else if (error) { } else if (error) {
return ( return (
<div style={styles.note}> <div style={styles.note}>
<p style={styles.text_medium_red}>Error contacting Notes server</p> <p style={styles.text_medium_red}>Error contacting Notes server</p>
</div> </div>
); );
} else if (!logged_in) {
return (
<div style={styles.note}>
<p style={styles.text_medium}>Please login to use Clip Notes</p>
</div>
);
} else if (notes.length === 0) { } else if (notes.length === 0) {
return ( return (
<div style={styles.note}> <div style={styles.note}>
@ -51,7 +51,6 @@ export default function Notes() {
</div> </div>
); );
} }
return ( return (
<> <>
{notes.map((note: NoteProps, index: number) => { {notes.map((note: NoteProps, index: number) => {

View file

@ -12,30 +12,38 @@ export default function UserPage() {
return ( return (
<div style={styles.background}> <div style={styles.background}>
<Header /> <Header />
<p style={styles.text_medium}>Loading...</p> <div style={styles.note}>
<p style={styles.text_medium}>Loading...</p>
</div>
</div> </div>
); );
} else if (error) { } else if (error) {
return ( return (
<div style={styles.background}> <div style={styles.background}>
<Header /> <Header />
<p style={styles.text_medium_red}>An error has occured</p> <div style={styles.note}>
<p style={styles.text_medium_red}>An error has occured</p>
</div>
</div> </div>
); );
} else if (!logged_in) { } else if (!logged_in) {
return ( return (
<div style={styles.background}> <div style={styles.background}>
<Header /> <Header />
<p style={styles.text_medium_red}>Please login to view user info</p> <div style={styles.note}>
<p style={styles.text_medium_red}>Please login to view user info</p>
</div>
</div> </div>
); );
} }
return ( return (
<div style={styles.background}> <div style={styles.background}>
<Header /> <Header />
<p style={styles.text_medium}>Username: {data.username}</p> <div style={styles.note}>
<p style={styles.text_medium}>Email: {data.email}</p> <p style={styles.text_medium}>Username: {data.username}</p>
<p style={styles.text_medium}>User ID: {data.id}</p> <p style={styles.text_medium}>Email: {data.email}</p>
<p style={styles.text_medium}>User ID: {data.id}</p>
</div>
</div> </div>
); );
} }