Added token to header for get APIs to support notes by user

This commit is contained in:
keannu125 2023-03-01 23:28:31 +08:00
parent 5c717cdf1f
commit a8c14f1331
2 changed files with 35 additions and 9 deletions

View file

@ -3,9 +3,16 @@ import axios from "axios";
// Note APIs // Note APIs
export function GetNotes() { export function GetNotes() {
return axios.get("http://localhost:8000/api/v1/notes/").then((response) => { const token = JSON.parse(localStorage.getItem("token") || "");
return response.data; return axios
}); .get("http://localhost:8000/api/v1/notes/", {
headers: {
Authorization: "Token " + token,
},
})
.then((response) => {
return response.data;
});
} }
export interface note { export interface note {
@ -14,15 +21,25 @@ export interface note {
} }
export function AddNote(note: note) { export function AddNote(note: note) {
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: {
Authorization: "Token " + token,
},
})
.then((response) => { .then((response) => {
return response.data; return response.data;
}); });
} }
export function DeleteNote(id: number) { 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 // User APIs

View file

@ -5,6 +5,7 @@ import Note from "../Note/Note";
import { Button } from "@mui/material"; import { Button } from "@mui/material";
import { useQuery } from "react-query"; import { useQuery } from "react-query";
import { GetNotes } from "../Api/Api"; import { GetNotes } from "../Api/Api";
import { useSelector } from "react-redux";
export default function Notes() { export default function Notes() {
const navigate = useNavigate(); const navigate = useNavigate();
@ -13,21 +14,29 @@ export default function Notes() {
isLoading, isLoading,
error, error,
} = useQuery("notes", GetNotes, { retry: 0 }); } = useQuery("notes", GetNotes, { retry: 0 });
const logged_in = useSelector(
(state: { Login: { logged_in: boolean } }) => state.Login.logged_in
);
if (!logged_in) {
return (
<div style={styles.note}>
<p style={styles.text_medium}>Please login to use Clip Notes</p>
</div>
);
}
if (error) { 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 (isLoading) {
if (isLoading) {
return ( return (
<div style={styles.note}> <div style={styles.note}>
<p style={styles.text_medium}>Loading Notes...</p> <p style={styles.text_medium}>Loading Notes...</p>
</div> </div>
); );
} } else if (notes.length === 0) {
if (notes.length === 0) {
return ( return (
<div style={styles.note}> <div style={styles.note}>
<p style={styles.text_medium}>No notes exist yet</p> <p style={styles.text_medium}>No notes exist yet</p>