mirror of
https://github.com/lemeow125/React-NotesApp.git
synced 2024-11-17 06:29:28 +08:00
Convert delete and new note into rest query syntax
This commit is contained in:
parent
8153561889
commit
fff01854ce
3 changed files with 42 additions and 12 deletions
|
@ -1,11 +1,24 @@
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
export function GetNotes() {
|
export function GetNotes() {
|
||||||
return axios.get("http://localhost:8000/api/v1/notes/").then((res) => {
|
return axios.get("http://localhost:8000/api/v1/notes/").then((response) => {
|
||||||
return res.data;
|
return response.data;
|
||||||
});
|
});
|
||||||
/*return fetch("http://localhost:8000/api/v1/notes/").then((res) => {
|
}
|
||||||
console.log(res.body);
|
|
||||||
return res.json();
|
export interface note {
|
||||||
});*/
|
title: string;
|
||||||
|
content: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function AddNote(note: note) {
|
||||||
|
return axios
|
||||||
|
.post("http://localhost:8000/api/v1/notes/", note)
|
||||||
|
.then((response) => {
|
||||||
|
return response.data;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function DeleteNote(id: number) {
|
||||||
|
return axios.delete("http://localhost:8000/api/v1/notes/" + id + "/");
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,8 @@ 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";
|
import axios from "axios";
|
||||||
import { useQueryClient } from "react-query";
|
import { useMutation, useQueryClient } from "react-query";
|
||||||
|
import { DeleteNote } from "../Api/Api";
|
||||||
|
|
||||||
export interface props {
|
export interface props {
|
||||||
title: string;
|
title: string;
|
||||||
|
@ -12,6 +13,12 @@ export interface props {
|
||||||
}
|
}
|
||||||
export default function Note(props: props) {
|
export default function Note(props: props) {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
const mutation = useMutation({
|
||||||
|
mutationFn: DeleteNote,
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries("notes");
|
||||||
|
},
|
||||||
|
});
|
||||||
return (
|
return (
|
||||||
<div style={styles.flex_column}>
|
<div style={styles.flex_column}>
|
||||||
<div style={styles.note}>
|
<div style={styles.note}>
|
||||||
|
@ -24,9 +31,7 @@ export default function Note(props: props) {
|
||||||
style={styles.button_remove}
|
style={styles.button_remove}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
axios.delete(
|
mutation.mutate(props.id);
|
||||||
"http://localhost:8000/api/v1/notes/" + props.id + "/"
|
|
||||||
);
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Remove Note
|
Remove Note
|
||||||
|
|
|
@ -4,6 +4,8 @@ import { useNavigate } from "react-router-dom";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import Header from "../../Components/Header/Header";
|
import Header from "../../Components/Header/Header";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import { AddNote } from "../../Components/Api/Api";
|
||||||
|
import { useMutation, useQueryClient } from "react-query";
|
||||||
import { title } from "process";
|
import { title } from "process";
|
||||||
|
|
||||||
export interface input {
|
export interface input {
|
||||||
|
@ -15,6 +17,14 @@ export default function NewNote() {
|
||||||
title: "",
|
title: "",
|
||||||
content: "",
|
content: "",
|
||||||
});
|
});
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
const mutation = useMutation({
|
||||||
|
mutationFn: AddNote,
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries("notes");
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={styles.background}>
|
<div style={styles.background}>
|
||||||
<Header />
|
<Header />
|
||||||
|
@ -43,8 +53,10 @@ export default function NewNote() {
|
||||||
style={styles.button_add}
|
style={styles.button_add}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
axios.post("http://localhost:8000/api/v1/notes/", note);
|
mutation.mutate({
|
||||||
console.log("foo");
|
title: note.title,
|
||||||
|
content: note.content,
|
||||||
|
});
|
||||||
navigate("/");
|
navigate("/");
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|
Loading…
Reference in a new issue