Added edit note page

This commit is contained in:
keannu125 2023-03-04 19:27:24 +08:00
parent 0f18c0be9e
commit c222ab0078
6 changed files with 184 additions and 16 deletions

View file

@ -1,6 +1,7 @@
import axios from "axios"; import axios from "axios";
import { import {
ActivationParams, ActivationParams,
UpdateNoteParams,
AddNoteParams, AddNoteParams,
LoginParams, LoginParams,
RegistrationParams, RegistrationParams,
@ -21,6 +22,36 @@ 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 + "/", {
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) { export function AddNote(note: AddNoteParams) {
const token = JSON.parse(localStorage.getItem("token") || ""); const token = JSON.parse(localStorage.getItem("token") || "");
return axios return axios

View file

@ -4,8 +4,10 @@ import { Button } from "@mui/material";
import { useMutation, useQueryClient } from "react-query"; import { useMutation, useQueryClient } from "react-query";
import { DeleteNote } from "../Api/Api"; import { DeleteNote } from "../Api/Api";
import { NoteProps } from "../../Interfaces/Interfaces"; import { NoteProps } from "../../Interfaces/Interfaces";
import { useNavigate } from "react-router-dom";
export default function Note(props: NoteProps) { export default function Note(props: NoteProps) {
const navigate = useNavigate();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const mutation = useMutation({ const mutation = useMutation({
mutationFn: DeleteNote, mutationFn: DeleteNote,
@ -28,15 +30,26 @@ export default function Note(props: NoteProps) {
<p style={styles.text_medium}> <p style={styles.text_medium}>
Timestamp: {String(props.date_created)} Timestamp: {String(props.date_created)}
</p> </p>
<Button <div style={styles.flex_row}>
style={styles.button_red} <Button
variant="contained" style={styles.button_red}
onClick={() => { variant="contained"
mutation.mutate(props.id); onClick={() => {
}} mutation.mutate(props.id);
> }}
Remove Note >
</Button> Remove Note
</Button>
<Button
style={styles.button_yellow}
variant="contained"
onClick={() => {
navigate("/Note/" + props.id);
}}
>
Edit Note
</Button>
</div>
</div> </div>
</div> </div>
); );

View file

@ -50,3 +50,9 @@ export interface AddNoteParams {
title: string; title: string;
content: string; content: string;
} }
export interface UpdateNoteParams {
id: number;
title: string;
content: string;
}

View file

@ -0,0 +1,115 @@
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 { AddNote, GetNote, UpdateNote } from "../../Components/Api/Api";
import { useMutation, useQuery, useQueryClient } from "react-query";
import { NoteProps } from "../../Interfaces/Interfaces";
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: "test",
content: "test",
});
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);
}, []);
if (error) {
return (
<div style={styles.background}>
<Header />
<div style={styles.note}>
<p style={styles.text_medium_red}>Error retrieving specific note</p>
</div>
</div>
);
}
if (isLoading) {
return (
<div style={styles.note}>
<p style={styles.text_medium}>Loading Note...</p>
</div>
);
}
if (data) {
return (
<div style={styles.background}>
<Header />
<p style={styles.text_medium}>Edit Note</p>
<div style={styles.flex_column}>
<div style={styles.note}>
<div style={styles.flex_row}>
<p style={styles.text_small}>Title:&nbsp;</p>
<input
style={styles.input_notetitle}
value={note.title}
onChange={(e: { target: { value: any } }) => {
setNote({ ...note, title: e.target.value });
}}
maxLength={20}
/>
</div>
<div style={styles.note_content}>
<textarea
style={styles.input_notebody}
value={note.content}
onChange={async (e: { target: { value: any } }) => {
await setNote({ ...note, content: e.target.value });
}}
/>
</div>
<Button
style={styles.button_green}
variant="contained"
onClick={async () => {
try {
await mutation.mutate({
id: Number(id),
title: note.title,
content: note.content,
});
navigate("/");
} catch (error) {}
}}
>
Update Note
</Button>
</div>
</div>
<div style={styles.flex_row}>
<Button
style={styles.button_red}
variant="contained"
onClick={() => {
navigate("/");
}}
>
Cancel
</Button>
</div>
</div>
);
}
return <div>heh</div>;
}

View file

@ -14,6 +14,7 @@ import { QueryClient, QueryClientProvider } from "react-query";
import { Provider } from "react-redux"; import { Provider } from "react-redux";
import Store from "./Features/Redux/Store/Store"; import Store from "./Features/Redux/Store/Store";
import ViewEditNote from "./Routes/ViewEditNote/ViewEditNote";
const queryClient = new QueryClient(); const queryClient = new QueryClient();
@ -38,6 +39,10 @@ const router = createBrowserRouter([
path: "/Activation/:uid/:token", path: "/Activation/:uid/:token",
element: <Activation />, element: <Activation />,
}, },
{
path: "/Note/:id",
element: <ViewEditNote />,
},
]); ]);
const root = ReactDOM.createRoot( const root = ReactDOM.createRoot(

View file

@ -81,7 +81,8 @@ const styles: { [key: string]: React.CSSProperties } = {
height: "100%", height: "100%",
width: "100%", width: "100%",
minWidth: "100%", minWidth: "100%",
minHeight: "512px", minHeight: "256px",
maxHeight: "768px",
fontSize: "2vh", fontSize: "2vh",
fontWeight: "bold", fontWeight: "bold",
}, },
@ -91,8 +92,7 @@ const styles: { [key: string]: React.CSSProperties } = {
display: "flex", display: "flex",
flexDirection: "row", flexDirection: "row",
width: "256x", width: "256x",
marginTop: 4, margin: 4,
marginBottom: 4,
}, },
button_yellow: { button_yellow: {
backgroundColor: "#e2b465", backgroundColor: "#e2b465",
@ -100,8 +100,7 @@ const styles: { [key: string]: React.CSSProperties } = {
display: "flex", display: "flex",
flexDirection: "row", flexDirection: "row",
width: "256px", width: "256px",
marginTop: 4, margin: 4,
marginBottom: 4,
}, },
button_red: { button_red: {
backgroundColor: "#bc231e", backgroundColor: "#bc231e",
@ -109,8 +108,7 @@ const styles: { [key: string]: React.CSSProperties } = {
display: "flex", display: "flex",
flexDirection: "row", flexDirection: "row",
width: "256px", width: "256px",
marginTop: 4, margin: 4,
marginBottom: 4,
}, },
text_small: { text_small: {
color: "white", color: "white",