-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
86 lines (78 loc) · 2.1 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
83
84
85
86
import { apiURL } from "./util/apiURL";
import { useState, useEffect } from "react";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import axios from 'axios';
import Home from "./Pages/Home";
import Index from "./Pages/Index";
import Edit from "./Pages/Edit";
import New from "./Pages/New";
import Show from "./Pages/Show";
import './App.css'
import NavBar from './Components/NavBar';
const API = apiURL();
function App() {
const [budgets, setBudgets] = useState([]);
const addBudget = (newBudget) => {
axios.post(`${API}/budgets`, newBudget).then((response) => {
setBudgets([...budgets, newBudget])
}).catch((err) => {
console.log(err)
})
}
const deleteBudget = (index) => {
axios.delete(`${API}/budgets/${index}`).then((response) => {
const updateArray = [...budgets];
updateArray.splice(index, 1);
setBudgets(updateArray);
return true
},
(err) => console.error(err)
)
.catch((c) => console.warn('catch', c));
}
const updateBudget = (updatedBudget, index) => {
axios.put(`${API}/budgets/${index}`, updatedBudget).then((response) => {
const updateArray = [...budgets];
updateArray[index] = updatedBudget;
setBudgets(updateArray)
},
(err) => console.error(err)
).catch((c) => console.warn("catch", c))
}
useEffect(() => {
axios.get(`${API}/budgets`).then((response) => {
const {data} = response;
setBudgets(data)
})
}, []);
return(
<div>
<Router>
<NavBar/>
<main>
<Switch>
<Route exact path='/'>
<Home/>
</Route>
<Route exact path='/budgets'>
<Index budgets={budgets} />
</Route>
<Route path="/budgets/new">
<New addBudget={addBudget}/>
</Route>
<Route exact path="/budgets/:index">
<Show budgets={budgets} deleteBudget={deleteBudget}/>
</Route>
<Route path="/budgets/:index/edit">
<Edit budgets={budgets} updateBudget={updateBudget}/>
</Route>
<Route path="*">
<h1>Page not Found</h1>
</Route>
</Switch>
</main>
</Router>
</div>
)
}
export default App;