mirror of
https://github.com/lemeow125/React-NotesApp.git
synced 2024-11-17 06:29:28 +08:00
Added backend fetching and posting
This commit is contained in:
parent
eeaf6a9ee4
commit
ee9ebcb9d9
6 changed files with 138 additions and 21 deletions
29
package-lock.json
generated
29
package-lock.json
generated
|
@ -19,6 +19,7 @@
|
||||||
"@types/node": "^16.18.12",
|
"@types/node": "^16.18.12",
|
||||||
"@types/react": "^18.0.28",
|
"@types/react": "^18.0.28",
|
||||||
"@types/react-dom": "^18.0.11",
|
"@types/react-dom": "^18.0.11",
|
||||||
|
"axios": "^1.3.3",
|
||||||
"localforage": "^1.10.0",
|
"localforage": "^1.10.0",
|
||||||
"match-sorter": "^6.3.1",
|
"match-sorter": "^6.3.1",
|
||||||
"react": "^18.2.0",
|
"react": "^18.2.0",
|
||||||
|
@ -5262,6 +5263,29 @@
|
||||||
"node": ">=4"
|
"node": ">=4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/axios": {
|
||||||
|
"version": "1.3.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/axios/-/axios-1.3.3.tgz",
|
||||||
|
"integrity": "sha512-eYq77dYIFS77AQlhzEL937yUBSepBfPIe8FcgEDN35vMNZKMrs81pgnyrQpwfy4NF4b4XWX1Zgx7yX+25w8QJA==",
|
||||||
|
"dependencies": {
|
||||||
|
"follow-redirects": "^1.15.0",
|
||||||
|
"form-data": "^4.0.0",
|
||||||
|
"proxy-from-env": "^1.1.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/axios/node_modules/form-data": {
|
||||||
|
"version": "4.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
|
||||||
|
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
|
||||||
|
"dependencies": {
|
||||||
|
"asynckit": "^0.4.0",
|
||||||
|
"combined-stream": "^1.0.8",
|
||||||
|
"mime-types": "^2.1.12"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/axobject-query": {
|
"node_modules/axobject-query": {
|
||||||
"version": "3.1.1",
|
"version": "3.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-3.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-3.1.1.tgz",
|
||||||
|
@ -14353,6 +14377,11 @@
|
||||||
"node": ">= 0.10"
|
"node": ">= 0.10"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/proxy-from-env": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
|
||||||
|
},
|
||||||
"node_modules/psl": {
|
"node_modules/psl": {
|
||||||
"version": "1.9.0",
|
"version": "1.9.0",
|
||||||
"resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz",
|
"resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz",
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
"@types/node": "^16.18.12",
|
"@types/node": "^16.18.12",
|
||||||
"@types/react": "^18.0.28",
|
"@types/react": "^18.0.28",
|
||||||
"@types/react-dom": "^18.0.11",
|
"@types/react-dom": "^18.0.11",
|
||||||
|
"axios": "^1.3.3",
|
||||||
"localforage": "^1.10.0",
|
"localforage": "^1.10.0",
|
||||||
"match-sorter": "^6.3.1",
|
"match-sorter": "^6.3.1",
|
||||||
"react": "^18.2.0",
|
"react": "^18.2.0",
|
||||||
|
|
65
src/Components/Note/NoteMapper/NoteMapper.tsx
Normal file
65
src/Components/Note/NoteMapper/NoteMapper.tsx
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
import * as React from "react";
|
||||||
|
import styles from "../../../styles";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import Note from "../Note";
|
||||||
|
import { Button } from "@mui/material";
|
||||||
|
|
||||||
|
export default function NoteMapper() {
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const [notes, setNotes] = useState([]);
|
||||||
|
const [error, setError] = useState(false);
|
||||||
|
const [errormessage, seterrormessage] = useState("");
|
||||||
|
useEffect(() => {
|
||||||
|
fetch("localhost:8000/notes")
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((data) => {
|
||||||
|
console.log(data);
|
||||||
|
setNotes(data);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
setError(true);
|
||||||
|
console.log(err.message);
|
||||||
|
seterrormessage(err.message);
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
if (error) {
|
||||||
|
return (
|
||||||
|
<div style={styles.note}>
|
||||||
|
<p style={styles.text_medium}>404</p>
|
||||||
|
<p style={styles.text_medium}>{errormessage}</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!notes) {
|
||||||
|
<div style={styles.note}>
|
||||||
|
<p style={styles.text_medium}>No notes exist yet</p>
|
||||||
|
<p style={styles.text_medium}>Make one!</p>
|
||||||
|
<Button
|
||||||
|
style={styles.button_add}
|
||||||
|
variant="contained"
|
||||||
|
onClick={() => {
|
||||||
|
navigate("/NewNote");
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Add Note
|
||||||
|
</Button>
|
||||||
|
</div>;
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{notes.map((note: { title: string; content: string }) => (
|
||||||
|
<Note title={note.title} content={note.content} />
|
||||||
|
))}
|
||||||
|
<Button
|
||||||
|
style={styles.button_add}
|
||||||
|
variant="contained"
|
||||||
|
onClick={() => {
|
||||||
|
navigate("/NewNote");
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Add Note
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
|
@ -1,11 +1,13 @@
|
||||||
import styles from "../../styles";
|
import styles from "../../styles";
|
||||||
import { Button } from "@mui/material";
|
import { Button } from "@mui/material";
|
||||||
import { useNavigate } from "react-router-dom";
|
|
||||||
import Note from "../../Components/Note/Note";
|
import Note from "../../Components/Note/Note";
|
||||||
import Header from "../../Components/Note/Header/Header";
|
import Header from "../../Components/Note/Header/Header";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import NoteMapper from "../../Components/Note/NoteMapper/NoteMapper";
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
const navigate = useNavigate();
|
const [notes, setNotes] = useState();
|
||||||
const notes_sample = [
|
const notes_sample = [
|
||||||
{ title: "note1", content: "notes are great!" },
|
{ title: "note1", content: "notes are great!" },
|
||||||
{ title: "note2", content: "notes are awesome!" },
|
{ title: "note2", content: "notes are awesome!" },
|
||||||
|
@ -13,20 +15,12 @@ export default function Home() {
|
||||||
return (
|
return (
|
||||||
<div style={styles.background}>
|
<div style={styles.background}>
|
||||||
<Header />
|
<Header />
|
||||||
|
{/*}
|
||||||
{notes_sample.map((note) => (
|
{notes_sample.map((note) => (
|
||||||
<Note title={note.title} content={note.content} />
|
<Note title={note.title} content={note.content} />
|
||||||
))}
|
))}
|
||||||
<div style={styles.flex_row}>
|
{*/}
|
||||||
<Button
|
<NoteMapper />
|
||||||
style={styles.button_add}
|
|
||||||
variant="contained"
|
|
||||||
onClick={() => {
|
|
||||||
navigate("/NewNote");
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
Add Note
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,25 +1,52 @@
|
||||||
import styles from "../../styles";
|
import styles from "../../styles";
|
||||||
import { Button } from "@mui/material";
|
import { Button } from "@mui/material";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { useState } from "react";
|
||||||
import Header from "../../Components/Note/Header/Header";
|
import Header from "../../Components/Note/Header/Header";
|
||||||
|
import axios from "axios";
|
||||||
|
import { title } from "process";
|
||||||
|
|
||||||
|
export interface input {
|
||||||
|
e: React.ChangeEvent;
|
||||||
|
}
|
||||||
export default function NewNote() {
|
export default function NewNote() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const [note, setNote] = useState({
|
||||||
|
title: "",
|
||||||
|
content: "",
|
||||||
|
});
|
||||||
return (
|
return (
|
||||||
<div style={styles.background}>
|
<div style={styles.background}>
|
||||||
<Header />
|
<Header />
|
||||||
<p style={styles.text_medium}>New Note</p>
|
<p style={styles.text_medium}>New Note</p>
|
||||||
<div style={styles.flex_column}>
|
<div style={styles.flex_column}>
|
||||||
<div style={styles.note}>
|
<div style={styles.note}>
|
||||||
<input style={styles.input_notetitle} maxLength={20} />
|
<div style={styles.flex_row}>
|
||||||
|
<p style={styles.text_small}>Title: </p>
|
||||||
|
<input
|
||||||
|
style={styles.input_notetitle}
|
||||||
|
onChange={(e: { target: { value: any } }) => {
|
||||||
|
setNote({ ...note, title: e.target.value });
|
||||||
|
}}
|
||||||
|
maxLength={20}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
<div style={styles.note_content}>
|
<div style={styles.note_content}>
|
||||||
<p style={styles.text_small}>Content</p>
|
<textarea
|
||||||
<textarea style={styles.input_notebody} />
|
style={styles.input_notebody}
|
||||||
|
onChange={(e: { target: { value: any } }) => {
|
||||||
|
setNote({ ...note, content: e.target.value });
|
||||||
|
}}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<Button
|
<Button
|
||||||
style={styles.button_add}
|
style={styles.button_add}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
onClick={() => {
|
onClick={async () => {
|
||||||
|
axios.post("https://localhost:8000/notes", {
|
||||||
|
title: note.content,
|
||||||
|
content: note.content,
|
||||||
|
});
|
||||||
console.log("foo");
|
console.log("foo");
|
||||||
navigate("/");
|
navigate("/");
|
||||||
}}
|
}}
|
||||||
|
|
|
@ -44,9 +44,9 @@ const styles: { [key: string]: React.CSSProperties } = {
|
||||||
},
|
},
|
||||||
input_notetitle: {
|
input_notetitle: {
|
||||||
alignSelf: "center",
|
alignSelf: "center",
|
||||||
backgroundColor: "#004264",
|
backgroundColor: "#001018",
|
||||||
borderRadius: 4,
|
borderRadius: 4,
|
||||||
border: "none",
|
borderColor: "#00293e",
|
||||||
outline: "none",
|
outline: "none",
|
||||||
color: "white",
|
color: "white",
|
||||||
height: "3vh",
|
height: "3vh",
|
||||||
|
@ -61,8 +61,9 @@ const styles: { [key: string]: React.CSSProperties } = {
|
||||||
border: "none",
|
border: "none",
|
||||||
outline: "none",
|
outline: "none",
|
||||||
color: "white",
|
color: "white",
|
||||||
width: "90%",
|
height: "100%",
|
||||||
height: "20vh",
|
width: "100%",
|
||||||
|
maxHeight: "100vh",
|
||||||
fontSize: "2vh",
|
fontSize: "2vh",
|
||||||
fontWeight: "bold",
|
fontWeight: "bold",
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue