forked from AdaGold/video-store-consumer
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathApp.js
126 lines (102 loc) · 3.32 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import React, { Component, useState } from 'react';
import './App.css';
import axios from 'axios'
import 'bootstrap/dist/css/bootstrap.min.css';
import {Nav, Navbar} from 'react-bootstrap'
import CustomerList from './components/CustomerList';
import VideoLibrary from './components/VideoLibrary';
import SelectedVideo from './components/SelectedVideo';
import Search from './components/Search'
import SelectedCustomer from './components/SelectedCustomer';
import {
BrowserRouter as Router,
Route,
Switch,
Link,
} from 'react-router-dom';
import Detail from './components/Detail';
const App = () => {
const [selectedVideo, setSelectedVideo] = useState(null)
const [selectedCustomer, setSelectedCustomer] = useState(null)
const [errorMessage, setErrorMessage] = useState('')
const handleVideoChange = ((selectedData) => {
setSelectedVideo(selectedData.value)
})
const handleCustomerChange = ((selectedData) => {
setSelectedCustomer(selectedData.value)
})
const resetState = () => {
setSelectedVideo(null);
setSelectedCustomer(null);
};
const addRental = () => {
axios
.post(`http://localhost:3000/rentals/${selectedVideo.title}/check-out`, {
customer_id: selectedCustomer.id,
due_date: new Date(
Date.now() + 1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/ * 7
),
})
.then((res) => {
window.location = '/detail'
//resetState();
})
.catch((error) => {
setErrorMessage(error.message);
});
};
return (
<div className="App">
<Router>
<Navbar bg="dark">
<Nav>
<Nav.Link>
<Link to="/" exact={true}>Video Store</Link>
</Nav.Link>
<Nav.Link>
<Link to="/search" exact={true}>Search</Link>
</Nav.Link>
<Nav.Link>
<Link to="/customers" exact={true}>Customers</Link>
</Nav.Link>
<Nav.Link>
<Link to="/library" exact={true}>Video Library</Link>
</Nav.Link>
<Nav.Link>
Selected Video: {selectedVideo &&(selectedVideo.title)}
</Nav.Link>
<Nav.Link>
Selected Customer: {selectedCustomer &&(selectedCustomer.name)}
</Nav.Link>
<Nav.Link>
{selectedVideo && selectedCustomer ?
<button className="customer-button" onClick={() => addRental()}>
Checkout
</button> : ''}
</Nav.Link>
</Nav>
</Navbar>
<Switch>
<Route exact={true} path="/">
<p>Homepage!</p>
</Route>
<Route path="/search">
<Search />
</Route>
<Route exact path="/customers">
<CustomerList onSelectCustomer={handleCustomerChange} selectedCustomer={selectedCustomer}/>
</Route>
<Route exact path="/library">
<VideoLibrary addRental={addRental} selectedCustomer={selectedCustomer} onSelectVideo={handleVideoChange} selectedVideo={selectedVideo}/>
</Route>
<Route exact path="/detail">
<Detail />
</Route>
</Switch>
<SelectedVideo video={selectedVideo}/>
<SelectedCustomer customer={selectedCustomer}/>
</Router>
</div>
);
}
export default App;