From eff9ebcbfad2633f6e4f75948f17f11e28377638 Mon Sep 17 00:00:00 2001 From: keannu125 Date: Sat, 25 Mar 2023 12:30:21 +0800 Subject: [PATCH 1/5] Removed console.logs from apis --- src/Components/Api/Api.tsx | 53 ++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/src/Components/Api/Api.tsx b/src/Components/Api/Api.tsx index 18cce4c..d93144c 100644 --- a/src/Components/Api/Api.tsx +++ b/src/Components/Api/Api.tsx @@ -9,10 +9,14 @@ import { // Note APIs +const instance = axios.create({ + baseURL: "https://keannu126.pythonanywhere.com", +}); + export function GetNotes() { const token = JSON.parse(localStorage.getItem("token") || "{}"); - return axios - .get("http://localhost:8000/api/v1/notes/", { + return instance + .get("/api/v1/notes/", { headers: { Authorization: "Token " + token, }, @@ -24,8 +28,8 @@ export function GetNotes() { export function GetNote(id: number) { const token = JSON.parse(localStorage.getItem("token") || "{}"); - return axios - .get("http://localhost:8000/api/v1/notes/" + id + "/", { + return instance + .get("/api/v1/notes/" + id + "/", { headers: { Authorization: "Token " + token, }, @@ -37,8 +41,8 @@ export function GetNote(id: number) { export function UpdateNote(note: UpdateNoteParams) { const token = JSON.parse(localStorage.getItem("token") || "{}"); - return axios - .patch("http://localhost:8000/api/v1/notes/" + note.id + "/", note, { + return instance + .patch("/api/v1/notes/" + note.id + "/", note, { headers: { Authorization: "Token " + token, }, @@ -47,15 +51,14 @@ export function UpdateNote(note: UpdateNoteParams) { return response.data; }) .catch((error) => { - console.log("Error updating note", error); return error; }); } export function AddNote(note: AddNoteParams) { const token = JSON.parse(localStorage.getItem("token") || "{}"); - return axios - .post("http://localhost:8000/api/v1/notes/", note, { + return instance + .post("/api/v1/notes/", note, { headers: { Authorization: "Token " + token, }, @@ -64,21 +67,19 @@ export function AddNote(note: AddNoteParams) { 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 + "/", { + return instance + .delete("/api/v1/notes/" + id + "/", { headers: { Authorization: "Token " + token, }, }) .catch((error) => { - console.log("Error deleting note", error); return error; }); } @@ -86,58 +87,48 @@ export function DeleteNote(id: number) { // User APIs export function UserRegister(register: RegistrationParams) { - return axios - .post("http://localhost:8000/api/v1/accounts/users/", register) + return instance + .post("/api/v1/accounts/users/", register) .then(async (response) => { - console.log(response.data); return true; }) .catch((error) => { - console.log("Registration failed: " + error); return false; }); } export function UserLogin(user: LoginParams) { - return axios - .post("http://localhost:8000/api/v1/accounts/token/login/", user) + return instance + .post("/api/v1/accounts/token/login/", user) .then(async (response) => { localStorage.setItem("token", JSON.stringify(response.data.auth_token)); - console.log( - "Login Success! Stored Token: ", - JSON.parse(localStorage.getItem("token") || "{}") - ); return true; }) .catch((error) => { - console.log("Login Failed: " + error); return false; }); } export function UserInfo() { const token = JSON.parse(localStorage.getItem("token") || "{}"); - return axios - .get("http://localhost:8000/api/v1/accounts/users/me/", { + return instance + .get("/api/v1/accounts/users/me/", { headers: { Authorization: "Token " + token, }, }) .then((response) => { - console.log(response.data); return response.data; }); } export function UserActivate(activation: ActivationParams) { - return axios - .post("http://localhost:8000/api/v1/accounts/users/activation/", activation) + return instance + .post("/api/v1/accounts/users/activation/", activation) .then(async (response) => { - console.log("Activation Success"); return true; }) .catch((error) => { - console.log("Activation failed: " + error); return false; }); } From 33ea3f8080f5f829e850990fd42f76b4b0219cb9 Mon Sep 17 00:00:00 2001 From: keannu125 Date: Sat, 25 Mar 2023 12:31:43 +0800 Subject: [PATCH 2/5] Polished and fixed Edit note page not displaying properly while loading --- src/Routes/ViewEditNote/ViewEditNote.tsx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Routes/ViewEditNote/ViewEditNote.tsx b/src/Routes/ViewEditNote/ViewEditNote.tsx index 9a82605..e8a0a9e 100644 --- a/src/Routes/ViewEditNote/ViewEditNote.tsx +++ b/src/Routes/ViewEditNote/ViewEditNote.tsx @@ -3,9 +3,8 @@ import { Button } from "@mui/material"; import { useNavigate, useParams } from "react-router-dom"; import { useEffect, useState } from "react"; import Header from "../../Components/Header/Header"; -import { AddNote, GetNote, UpdateNote } from "../../Components/Api/Api"; +import { GetNote, UpdateNote } from "../../Components/Api/Api"; import { useMutation, useQuery, useQueryClient } from "react-query"; -import { NoteProps } from "../../Interfaces/Interfaces"; export interface input { e: React.ChangeEvent; @@ -34,7 +33,7 @@ export default function ViewNote() { }); useEffect(() => { setNote(data); - }, []); + }, [data]); if (error) { return (
@@ -47,8 +46,10 @@ export default function ViewNote() { } if (isLoading) { return ( -
-

Loading Note...

+
+
+

Loading Note...

+
); } @@ -112,4 +113,4 @@ export default function ViewNote() { ); } return
heh
; -} \ No newline at end of file +} From bc3afa860471fcec12647016eb4223b784fc7a97 Mon Sep 17 00:00:00 2001 From: keannu125 Date: Sat, 25 Mar 2023 12:33:27 +0800 Subject: [PATCH 3/5] Polished apis and fixed some missing files --- src/Features/Redux/Store/Api.tsx | 134 ++++++++++++++++++++++ src/Features/Redux/Store/LoginSlice.tsx | 21 ++++ src/Features/Redux/Store/ViewEditNote.tsx | 116 +++++++++++++++++++ 3 files changed, 271 insertions(+) create mode 100644 src/Features/Redux/Store/Api.tsx create mode 100644 src/Features/Redux/Store/LoginSlice.tsx create mode 100644 src/Features/Redux/Store/ViewEditNote.tsx diff --git a/src/Features/Redux/Store/Api.tsx b/src/Features/Redux/Store/Api.tsx new file mode 100644 index 0000000..d93144c --- /dev/null +++ b/src/Features/Redux/Store/Api.tsx @@ -0,0 +1,134 @@ +import axios from "axios"; +import { + ActivationParams, + UpdateNoteParams, + AddNoteParams, + LoginParams, + RegistrationParams, +} from "../../Interfaces/Interfaces"; + +// Note APIs + +const instance = axios.create({ + baseURL: "https://keannu126.pythonanywhere.com", +}); + +export function GetNotes() { + const token = JSON.parse(localStorage.getItem("token") || "{}"); + return instance + .get("/api/v1/notes/", { + headers: { + Authorization: "Token " + token, + }, + }) + .then((response) => { + return response.data; + }); +} + +export function GetNote(id: number) { + const token = JSON.parse(localStorage.getItem("token") || "{}"); + return instance + .get("/api/v1/notes/" + id + "/", { + headers: { + Authorization: "Token " + token, + }, + }) + .then((response) => { + return response.data; + }); +} + +export function UpdateNote(note: UpdateNoteParams) { + const token = JSON.parse(localStorage.getItem("token") || "{}"); + return instance + .patch("/api/v1/notes/" + note.id + "/", note, { + headers: { + Authorization: "Token " + token, + }, + }) + .then((response) => { + return response.data; + }) + .catch((error) => { + return error; + }); +} + +export function AddNote(note: AddNoteParams) { + const token = JSON.parse(localStorage.getItem("token") || "{}"); + return instance + .post("/api/v1/notes/", note, { + headers: { + Authorization: "Token " + token, + }, + }) + .then((response) => { + return response.data; + }) + .catch((error) => { + return error; + }); +} + +export function DeleteNote(id: number) { + const token = JSON.parse(localStorage.getItem("token") || "{}"); + return instance + .delete("/api/v1/notes/" + id + "/", { + headers: { + Authorization: "Token " + token, + }, + }) + .catch((error) => { + return error; + }); +} + +// User APIs + +export function UserRegister(register: RegistrationParams) { + return instance + .post("/api/v1/accounts/users/", register) + .then(async (response) => { + return true; + }) + .catch((error) => { + return false; + }); +} + +export function UserLogin(user: LoginParams) { + return instance + .post("/api/v1/accounts/token/login/", user) + .then(async (response) => { + localStorage.setItem("token", JSON.stringify(response.data.auth_token)); + return true; + }) + .catch((error) => { + return false; + }); +} + +export function UserInfo() { + const token = JSON.parse(localStorage.getItem("token") || "{}"); + return instance + .get("/api/v1/accounts/users/me/", { + headers: { + Authorization: "Token " + token, + }, + }) + .then((response) => { + return response.data; + }); +} + +export function UserActivate(activation: ActivationParams) { + return instance + .post("/api/v1/accounts/users/activation/", activation) + .then(async (response) => { + return true; + }) + .catch((error) => { + return false; + }); +} diff --git a/src/Features/Redux/Store/LoginSlice.tsx b/src/Features/Redux/Store/LoginSlice.tsx new file mode 100644 index 0000000..5802c33 --- /dev/null +++ b/src/Features/Redux/Store/LoginSlice.tsx @@ -0,0 +1,21 @@ +import { createSlice } from "@reduxjs/toolkit"; + +export const LoginSlice = createSlice({ + name: "Login", + initialState: { + value: false, + }, + reducers: { + SetLoggedIn: (state) => { + state.value = !state.value; + }, + SetLoggedOut: (state) => { + state.value = !state.value; + }, + }, +}); + +// Action creators are generated for each case reducer function +export const { SetLoggedIn, SetLoggedOut } = LoginSlice.actions; + +export default LoginSlice.reducer; diff --git a/src/Features/Redux/Store/ViewEditNote.tsx b/src/Features/Redux/Store/ViewEditNote.tsx new file mode 100644 index 0000000..3226e69 --- /dev/null +++ b/src/Features/Redux/Store/ViewEditNote.tsx @@ -0,0 +1,116 @@ +import styles from "../../styles"; +import { Button } from "@mui/material"; +import { useNavigate, useParams } from "react-router-dom"; +import { useEffect, useState } from "react"; +import Header from "../../Components/Header/Header"; +import { GetNote, UpdateNote } from "../../Components/Api/Api"; +import { useMutation, useQuery, useQueryClient } from "react-query"; + +export interface input { + e: React.ChangeEvent; +} +export default function ViewNote() { + const navigate = useNavigate(); + const { id } = useParams(); + const queryClient = useQueryClient(); + const mutation = useMutation({ + mutationFn: UpdateNote, + onSuccess: () => { + queryClient.invalidateQueries("notes"); + }, + }); + const [note, setNote] = useState({ + title: "", + content: "", + }); + async function retrieve() { + let a = await GetNote(Number(id)); + setNote(a); + return a; + } + const { data, isLoading, error } = useQuery("note", retrieve, { + retry: 0, + }); + useEffect(() => { + setNote(data); + }, [data]); + if (error) { + return ( +
+
+
+

Error retrieving specific note

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

Loading Note...

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

Edit Note

+
+
+
+

Title: 

+ { + setNote({ ...note, title: e.target.value }); + }} + maxLength={20} + /> +
+
+