-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
85 lines (69 loc) · 1.78 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import "./App.css";
import React, { useEffect, useState } from "react";
import Axios from "axios";
import { DataGrid } from '@mui/x-data-grid';
function App() {
const [bookTitle, setBookTitle] = useState("");
const [author, setAuthor] = useState("");
const [bookTitleList, setBookList] = useState([])
useEffect(()=> {
Axios.get('http://localhost:5000/api/get').then((response)=> {
setBookList(response.data); //retrieves books from the database
});
}, []);
const submitBook = () => {
Axios.post('http://localhost:5000/api/insert', {bookTitle: bookTitle, author: author,
}).then(() => {
alert("successful insert"); //adds new book to the database
});
};
//Set columns for table
const columns = [
{ field: 'id', headerName: 'ID', width: 70 },
{ field: 'name', headerName: 'Name', width: 160 },
{ field: 'title', headerName: 'Title', width: 300 },
];
const rows = [];
return (
<div className="App">
<h1>Reading List</h1>
//Form to submit new book to database
<div className="form">
<label>Book Title:</label>
<input
type="text"
name="bookTitle"
onChange={(e) => {
setBookTitle(e.target.value);
}}
/>
<label>Author:</label>
<input
type="text"
name="author"
onChange={(e) => {
setAuthor(e.target.value);
}}
/>
<button onClick={submitBook}> Submit </button>
</div>
//Retrieve book list from SQL database
{bookTitleList.map((val) => {
rows.push({id: val.id, title: val.title, name: val.name});
})}
//Add styling for mui-table of book list
<div className="table">
<div style={{ height: 400, width: '50%' }}>
<DataGrid
rows={rows}
columns={columns}
pageSize={5}
rowsPerPageOptions={[5]}
checkboxSelection
/>
</div>
</div>
</div>
);
}
export default App;