mirror of
https://github.com/lemeow125/React-NotesApp.git
synced 2024-11-17 06:29:28 +08:00
Made remove button functional and added auto refresh
This commit is contained in:
parent
df0326a768
commit
26d6b64c4b
2 changed files with 20 additions and 4 deletions
|
@ -1,10 +1,12 @@
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import styles from "../../styles";
|
import styles from "../../styles";
|
||||||
import { Button } from "@mui/material";
|
import { Button } from "@mui/material";
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
export interface props {
|
export interface props {
|
||||||
title: string;
|
title: string;
|
||||||
content: string;
|
content: string;
|
||||||
|
id: number;
|
||||||
}
|
}
|
||||||
export default function Note(props: props) {
|
export default function Note(props: props) {
|
||||||
return (
|
return (
|
||||||
|
@ -18,7 +20,7 @@ export default function Note(props: props) {
|
||||||
style={styles.button_remove}
|
style={styles.button_remove}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
console.log("foo");
|
axios.delete("http://localhost:8000/notes/" + props.id + "/");
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Remove Note
|
Remove Note
|
||||||
|
|
|
@ -11,7 +11,7 @@ export default function NoteMapper() {
|
||||||
const [notes, setNotes] = useState([]);
|
const [notes, setNotes] = useState([]);
|
||||||
const [error, setError] = useState(false);
|
const [error, setError] = useState(false);
|
||||||
const [errormessage, seterrormessage] = useState("");
|
const [errormessage, seterrormessage] = useState("");
|
||||||
useEffect(() => {
|
function server_get() {
|
||||||
axios
|
axios
|
||||||
.get("http://localhost:8000/notes/")
|
.get("http://localhost:8000/notes/")
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
|
@ -22,6 +22,13 @@ export default function NoteMapper() {
|
||||||
setError(true);
|
setError(true);
|
||||||
seterrormessage(err);
|
seterrormessage(err);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
useEffect(() => {
|
||||||
|
server_get();
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
server_get();
|
||||||
|
}, 1000);
|
||||||
|
return () => clearInterval(interval);
|
||||||
}, []);
|
}, []);
|
||||||
if (error) {
|
if (error) {
|
||||||
return (
|
return (
|
||||||
|
@ -50,9 +57,16 @@ export default function NoteMapper() {
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{notes.map((note: { title: string; content: string }, i) => {
|
{notes.map((note: { title: string; content: string; id: number }, i) => {
|
||||||
console.log(note);
|
console.log(note);
|
||||||
return <Note key={i} title={note.title} content={note.content} />;
|
return (
|
||||||
|
<Note
|
||||||
|
id={note.id}
|
||||||
|
key={i}
|
||||||
|
title={note.title}
|
||||||
|
content={note.content}
|
||||||
|
/>
|
||||||
|
);
|
||||||
})}
|
})}
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
|
|
Loading…
Reference in a new issue