diff --git a/src/Components/Api/Api.tsx b/src/Components/Api/Api.tsx index a9478b3..18cce4c 100644 --- a/src/Components/Api/Api.tsx +++ b/src/Components/Api/Api.tsx @@ -1,38 +1,91 @@ import axios from "axios"; +import { + ActivationParams, + UpdateNoteParams, + AddNoteParams, + LoginParams, + RegistrationParams, +} from "../../Interfaces/Interfaces"; // Note APIs export function GetNotes() { - return axios.get("http://localhost:8000/api/v1/notes/").then((response) => { - return response.data; - }); -} - -export interface note { - title: string; - content: string; -} - -export function AddNote(note: note) { + const token = JSON.parse(localStorage.getItem("token") || "{}"); return axios - .post("http://localhost:8000/api/v1/notes/", note) + .get("http://localhost:8000/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 axios + .get("http://localhost:8000/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 axios + .patch("http://localhost:8000/api/v1/notes/" + note.id + "/", note, { + headers: { + Authorization: "Token " + token, + }, + }) + .then((response) => { + 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, { + headers: { + Authorization: "Token " + token, + }, + }) + .then((response) => { + return response.data; + }) + .catch((error) => { + console.log("Error adding note", error); + return error; + }); +} + 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, + }, + }) + .catch((error) => { + console.log("Error deleting note", error); + return error; + }); } // User APIs -export interface register { - email: string; - username: string; - password: string; -} -export function UserRegister(register: register) { +export function UserRegister(register: RegistrationParams) { return axios .post("http://localhost:8000/api/v1/accounts/users/", register) .then(async (response) => { @@ -45,19 +98,14 @@ export function UserRegister(register: register) { }); } -export interface user { - username: string; - password: string; -} - -export function UserLogin(user: user) { +export function UserLogin(user: LoginParams) { return axios .post("http://localhost:8000/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") || "") + JSON.parse(localStorage.getItem("token") || "{}") ); return true; }) @@ -68,7 +116,7 @@ export function UserLogin(user: user) { } 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: { @@ -76,20 +124,12 @@ export function UserInfo() { }, }) .then((response) => { + console.log(response.data); return response.data; - }) - .catch((error) => { - console.log("Error in fetching user data"); - console.log(error); }); } -export interface activation { - uid: string; - token: string; -} - -export function UserActivate(activation: activation) { +export function UserActivate(activation: ActivationParams) { return axios .post("http://localhost:8000/api/v1/accounts/users/activation/", activation) .then(async (response) => { diff --git a/src/Components/AppIcon/AppIcon.tsx b/src/Components/AppIcon/AppIcon.tsx index 56d5c80..5d1afa7 100644 --- a/src/Components/AppIcon/AppIcon.tsx +++ b/src/Components/AppIcon/AppIcon.tsx @@ -1,11 +1,7 @@ import * as React from "react"; +import { IconProps } from "../../Interfaces/Interfaces"; -export interface props { - size: number; - color: string; -} - -export default function AppIcon(props: props) { +export default function AppIcon(props: IconProps) { return ( <> diff --git a/src/Components/Header/Header.tsx b/src/Components/Header/Header.tsx index 687ce92..3acb0b8 100644 --- a/src/Components/Header/Header.tsx +++ b/src/Components/Header/Header.tsx @@ -4,6 +4,7 @@ import styles from "../../styles"; import AppIcon from "../AppIcon/AppIcon"; import Login from "../LoginButton/LoginButton"; import HomeIcon from "../HomeIcon/HomeIcon"; +import UserIcon from "../UserIcon/UserIcon"; export default function Header() { return ( @@ -16,9 +17,7 @@ export default function Header() { }} > - - - +
diff --git a/src/Components/LoginButton/LoginButton.tsx b/src/Components/LoginButton/LoginButton.tsx index cb1746f..6df15b5 100644 --- a/src/Components/LoginButton/LoginButton.tsx +++ b/src/Components/LoginButton/LoginButton.tsx @@ -5,23 +5,14 @@ import styles from "../../styles"; import { useDispatch, useSelector } from "react-redux"; import { SetLoggedOut } from "../../Features/Redux/Slices/LoginSlice/LoginSlice"; import { UnsetUser } from "../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice"; - -export interface user { - LoggedInUser: { - value: { - email: string; - id: number; - username: string; - }; - }; -} +import { LoggedInUserState, LoginState } from "../../Interfaces/Interfaces"; export default function LoginButton() { const dispatch = useDispatch(); - const logged_in = useSelector( - (state: { Login: { logged_in: boolean } }) => state.Login.logged_in + const logged_in = useSelector((state: LoginState) => state.Login.logged_in); + const logged_in_user = useSelector( + (state: LoggedInUserState) => state.LoggedInUser.value ); - const logged_in_user = useSelector((state: user) => state.LoggedInUser.value); const navigate = useNavigate(); if (!logged_in) { return ( diff --git a/src/Components/Note/Note.tsx b/src/Components/Note/Note.tsx index 153e424..59003e7 100644 --- a/src/Components/Note/Note.tsx +++ b/src/Components/Note/Note.tsx @@ -3,14 +3,11 @@ import styles from "../../styles"; import { Button } from "@mui/material"; import { useMutation, useQueryClient } from "react-query"; import { DeleteNote } from "../Api/Api"; +import { NoteProps } from "../../Interfaces/Interfaces"; +import { useNavigate } from "react-router-dom"; -export interface props { - title: string; - content: string; - id: number; - date_created: string; -} -export default function Note(props: props) { +export default function Note(props: NoteProps) { + const navigate = useNavigate(); const queryClient = useQueryClient(); const mutation = useMutation({ mutationFn: DeleteNote, @@ -21,20 +18,38 @@ export default function Note(props: props) { return (
-

{props.title}

+

Owner: {props.owner}

+

Title: {props.title}

-

{props.content}

+