Merge pull request #11 from lemeow125/feature/deploy_online

Feature/deploy online
This commit is contained in:
lemeow125 2023-03-25 13:03:49 +08:00 committed by GitHub
commit 607fa42cc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 112 additions and 52 deletions

View file

@ -9,10 +9,14 @@ import {
// Note APIs // Note APIs
const instance = axios.create({
baseURL: "https://keannu126.pythonanywhere.com",
});
export function GetNotes() { export function GetNotes() {
const token = JSON.parse(localStorage.getItem("token") || "{}"); const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return instance
.get("http://localhost:8000/api/v1/notes/", { .get("/api/v1/notes/", {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
@ -24,8 +28,8 @@ 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 instance
.get("http://localhost:8000/api/v1/notes/" + id + "/", { .get("/api/v1/notes/" + id + "/", {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
@ -37,8 +41,8 @@ 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 instance
.patch("http://localhost:8000/api/v1/notes/" + note.id + "/", note, { .patch("/api/v1/notes/" + note.id + "/", note, {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
@ -47,15 +51,14 @@ export function UpdateNote(note: UpdateNoteParams) {
return response.data; return response.data;
}) })
.catch((error) => { .catch((error) => {
console.log("Error updating note", error);
return error; return error;
}); });
} }
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 instance
.post("http://localhost:8000/api/v1/notes/", note, { .post("/api/v1/notes/", note, {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
@ -64,21 +67,19 @@ export function AddNote(note: AddNoteParams) {
return response.data; return response.data;
}) })
.catch((error) => { .catch((error) => {
console.log("Error adding note", error);
return 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 return instance
.delete("http://localhost:8000/api/v1/notes/" + id + "/", { .delete("/api/v1/notes/" + id + "/", {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
}) })
.catch((error) => { .catch((error) => {
console.log("Error deleting note", error);
return error; return error;
}); });
} }
@ -86,58 +87,48 @@ export function DeleteNote(id: number) {
// User APIs // User APIs
export function UserRegister(register: RegistrationParams) { export function UserRegister(register: RegistrationParams) {
return axios return instance
.post("http://localhost:8000/api/v1/accounts/users/", register) .post("/api/v1/accounts/users/", register)
.then(async (response) => { .then(async (response) => {
console.log(response.data);
return true; return true;
}) })
.catch((error) => { .catch((error) => {
console.log("Registration failed: " + error);
return false; return false;
}); });
} }
export function UserLogin(user: LoginParams) { export function UserLogin(user: LoginParams) {
return axios return instance
.post("http://localhost:8000/api/v1/accounts/token/login/", user) .post("/api/v1/accounts/token/login/", user)
.then(async (response) => { .then(async (response) => {
localStorage.setItem("token", JSON.stringify(response.data.auth_token)); localStorage.setItem("token", JSON.stringify(response.data.auth_token));
console.log(
"Login Success! Stored Token: ",
JSON.parse(localStorage.getItem("token") || "{}")
);
return true; return true;
}) })
.catch((error) => { .catch((error) => {
console.log("Login Failed: " + error);
return false; return false;
}); });
} }
export function UserInfo() { export function UserInfo() {
const token = JSON.parse(localStorage.getItem("token") || "{}"); const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return instance
.get("http://localhost:8000/api/v1/accounts/users/me/", { .get("/api/v1/accounts/users/me/", {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
}) })
.then((response) => { .then((response) => {
console.log(response.data);
return response.data; return response.data;
}); });
} }
export function UserActivate(activation: ActivationParams) { export function UserActivate(activation: ActivationParams) {
return axios return instance
.post("http://localhost:8000/api/v1/accounts/users/activation/", activation) .post("/api/v1/accounts/users/activation/", activation)
.then(async (response) => { .then(async (response) => {
console.log("Activation Success");
return true; return true;
}) })
.catch((error) => { .catch((error) => {
console.log("Activation failed: " + error);
return false; return false;
}); });
} }

View file

@ -5,10 +5,11 @@ import AppIcon from "../AppIcon/AppIcon";
import Login from "../LoginButton/LoginButton"; import Login from "../LoginButton/LoginButton";
import HomeIcon from "../HomeIcon/HomeIcon"; import HomeIcon from "../HomeIcon/HomeIcon";
import UserIcon from "../UserIcon/UserIcon"; import UserIcon from "../UserIcon/UserIcon";
import PreviousSessionChecker from "../PreviousSessionChecker/PreviousSessionChecker";
export default function Header() { export default function Header() {
return ( return (
<div style={styles.header}> <div style={styles.header}>
<PreviousSessionChecker />
<div style={styles.flex_row}> <div style={styles.flex_row}>
<div <div
style={{ style={{

View file

@ -3,18 +3,16 @@ import { Button } from "@mui/material";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import styles from "../../styles"; import styles from "../../styles";
import { useDispatch, useSelector } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
import { SetLoggedOut } from "../../Features/Redux/Slices/LoginSlice/LoginSlice"; import { Toggle_Login } from "../../Features/Redux/Slices/LoginSlice/LoginSlice";
import { UnsetUser } from "../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice"; import { UnsetUser } from "../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice";
import { RootState } from "../../Features/Redux/Store/Store"; import { RootState } from "../../Features/Redux/Store/Store";
export default function LoginButton() { export default function LoginButton() {
const dispatch = useDispatch(); const dispatch = useDispatch();
const logged_in = useSelector( const logged_in = useSelector((state: RootState) => state.logged_in.value);
(state: RootState) => state.logged_in.value
);
const logged_in_user = useSelector( const logged_in_user = useSelector(
(state: RootState) => state.logged_in_user.value (state: RootState) => state.logged_in_user.value
); );
const navigate = useNavigate(); const navigate = useNavigate();
if (!logged_in) { if (!logged_in) {
return ( return (
@ -42,7 +40,7 @@ export default function LoginButton() {
variant="contained" variant="contained"
onClick={() => { onClick={() => {
console.log("Logged out..."); console.log("Logged out...");
dispatch(SetLoggedOut()); dispatch(Toggle_Login());
dispatch(UnsetUser()); dispatch(UnsetUser());
localStorage.setItem("token", ""); localStorage.setItem("token", "");
}} }}

View file

@ -0,0 +1,32 @@
import * as React from "react";
import { useEffect, useCallback } from "react";
import { useDispatch, useSelector } from "react-redux";
import { UserInfo } from "../Api/Api";
import { Toggle_Login } from "../../Features/Redux/Slices/LoginSlice/LoginSlice";
import { SetUser } from "../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice";
import { set_checked } from "../../Features/Redux/Slices/OldSession/OldSessionSlice";
import { RootState } from "../../Features/Redux/Store/Store";
export default function PreviousSessionChecker() {
const dispatch = useDispatch();
const logged_in = useSelector((state: RootState) => state.logged_in.value);
// Function to check for previous login session
const check = useCallback(async () => {
if (await UserInfo()) {
if (logged_in !== true) {
console.log("Previous session found. Restoring");
await dispatch(Toggle_Login());
await dispatch(SetUser(await UserInfo()));
}
} else {
console.log("No old session found");
localStorage.removeItem("token");
}
await dispatch(set_checked());
}, [dispatch, logged_in]);
useEffect(() => {
if (!logged_in) {
check();
}
}, [check, logged_in]);
return <div />;
}

View file

@ -6,16 +6,13 @@ export const LoginSlice = createSlice({
value: false, value: false,
}, },
reducers: { reducers: {
SetLoggedIn: (state) => { Toggle_Login: (state) => {
state.value = !state.value;
},
SetLoggedOut: (state) => {
state.value = !state.value; state.value = !state.value;
}, },
}, },
}); });
// Action creators are generated for each case reducer function // Action creators are generated for each case reducer function
export const { SetLoggedIn, SetLoggedOut } = LoginSlice.actions; export const { Toggle_Login } = LoginSlice.actions;
export default LoginSlice.reducer; export default LoginSlice.reducer;

View file

@ -0,0 +1,17 @@
import { createSlice } from "@reduxjs/toolkit";
export const OldSessionSlice = createSlice({
name: "old_session_checked",
initialState: {
value: false,
},
reducers: {
set_checked: (state) => {
state.value = !state.value;
},
},
});
export const { set_checked } = OldSessionSlice.actions;
export default OldSessionSlice.reducer;

View file

@ -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;

View file

@ -1,11 +1,13 @@
import { configureStore } from "@reduxjs/toolkit"; import { configureStore } from "@reduxjs/toolkit";
import LoginReducer from "../Slices/LoginSlice/LoginSlice"; import LoginReducer from "../Slices/LoginSlice/LoginSlice";
import LoggedInUserReucer from "../Slices/LoggedInUserSlice/LoggedInUserSlice"; import LoggedInUserReucer from "../Slices/LoggedInUserSlice/LoggedInUserSlice";
import OldSessionReducer from "../Slices/OldSession/OldSessionSlice";
const store = configureStore({ const store = configureStore({
reducer: { reducer: {
logged_in: LoginReducer, logged_in: LoginReducer,
logged_in_user: LoggedInUserReucer, logged_in_user: LoggedInUserReucer,
old_session_checked: OldSessionReducer,
}, },
}); });
@ -13,4 +15,4 @@ export default store;
// Infer the `RootState` and `AppDispatch` types from the store itself // Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>; export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch; export type AppDispatch = typeof store.dispatch;

View file

@ -9,7 +9,7 @@ import { UserInfo, UserLogin } from "../../Components/Api/Api";
import { useDispatch } from "react-redux"; import { useDispatch } from "react-redux";
import { SetUser } from "../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice"; import { SetUser } from "../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice";
import { SetLoggedIn } from "../../Features/Redux/Slices/LoginSlice/LoginSlice"; import { Toggle_Login } from "../../Features/Redux/Slices/LoginSlice/LoginSlice";
export default function Login() { export default function Login() {
const navigate = useNavigate(); const navigate = useNavigate();
@ -56,7 +56,7 @@ export default function Login() {
password: "", password: "",
}); });
if (await UserLogin(user)) { if (await UserLogin(user)) {
await dispatch(SetLoggedIn()); await dispatch(Toggle_Login());
await dispatch(SetUser(await UserInfo())); await dispatch(SetUser(await UserInfo()));
navigate("/"); navigate("/");
} else { } else {

View file

@ -3,9 +3,8 @@ import { Button } from "@mui/material";
import { useNavigate, useParams } from "react-router-dom"; import { useNavigate, useParams } from "react-router-dom";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import Header from "../../Components/Header/Header"; 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 { useMutation, useQuery, useQueryClient } from "react-query";
import { NoteProps } from "../../Interfaces/Interfaces";
export interface input { export interface input {
e: React.ChangeEvent; e: React.ChangeEvent;
@ -34,7 +33,7 @@ export default function ViewNote() {
}); });
useEffect(() => { useEffect(() => {
setNote(data); setNote(data);
}, []); }, [data]);
if (error) { if (error) {
return ( return (
<div style={styles.background}> <div style={styles.background}>
@ -47,8 +46,10 @@ export default function ViewNote() {
} }
if (isLoading) { if (isLoading) {
return ( return (
<div style={styles.note}> <div style={styles.background}>
<p style={styles.text_medium}>Loading Note...</p> <div style={styles.note}>
<p style={styles.text_medium}>Loading Note...</p>
</div>
</div> </div>
); );
} }
@ -112,4 +113,4 @@ export default function ViewNote() {
); );
} }
return <div>heh</div>; return <div>heh</div>;
} }