forked from AdaGold/video-store-consumer
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathApp.js
115 lines (100 loc) · 3.25 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
import React, {useState } from 'react';
import { BrowserRouter as Router, Link, Route, Switch } from 'react-router-dom';
import logo from './logo.svg';
import axios from 'axios';
import './App.css';
import AddMovieForm from './components/AddMovieForm';
import VideoCollection from './components/VideoCollection';
import CustomerCollection from './components/CustomerCollection';
import { Navbar, Nav, Button } from 'react-bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';
const API_URL_BASE = 'http://localhost:3000/';
const App = () => {
const [selectedVideo, setSelectedVideo] = useState('None');
const [selectedCustomer, setSelectedCustomer] = useState({
id: null,
name: 'None',
});
const [checkoutMessage, setcheckoutMessage] = useState(null);
const selectVideo = (title) => {
setcheckoutMessage('');
setSelectedVideo(title);
};
const selectCustomer = (id, name) => {
setcheckoutMessage('');
setSelectedCustomer({
id: id,
name: name,
})
};
const checkOut = () => {
const today = new Date();
today.setDate(today.getDate() + 7);
if (selectedCustomer.name === 'None' || selectedVideo === 'None') {
setcheckoutMessage('Need to select a customer and video')
return null
}
axios.post(API_URL_BASE + 'rentals/' + selectedVideo + '/check-out?customer_id=' + selectedCustomer.id + '&due_date=' + today.toString())
.then((response) => {
console.log(response);
setcheckoutMessage(`Checked out ${selectedVideo} successfully to ${selectedCustomer.name}!`);
setSelectedVideo('None');
setSelectedCustomer({
id: null,
name: 'None',
});
})
.catch((error) => {
console.log(error);
setcheckoutMessage(error.message);
});
};
const checkoutMessageNav = () => {
if (checkoutMessage) {
return (<div className='checkout'>{checkoutMessage}</div>);
} else {
return (
<div>
<span>Selected Video: {selectedVideo}</span>
<span>Selected Customer: {selectedCustomer.name}</span>
</div>)
}
};
return (
<Router>
<div className="App">
<Navbar fixed='top' bg="dark" variant="dark">
<Nav className="mr-auto">
<Nav.Link as={Link}to='/'>Home</Nav.Link>
<Nav.Link as={Link} to='/library'>Library</Nav.Link>
<Nav.Link as={Link} to='/customers'>Customers</Nav.Link>
<Nav.Link as={Link} to='/search'>Search</Nav.Link>
</Nav>
{/* <Navbar.Header>Hello</Navbar.Header> */}
{checkoutMessageNav()}
<Button variant="outline-info" onClick={checkOut}>Check Out</Button>
</Navbar>
<Switch>
<Route path="/library">
<VideoCollection onSelectVideo={selectVideo} showButton='select'/>
</Route>
<Route path="/customers">
<CustomerCollection onSelectCustomer={selectCustomer}/>
</Route>
<Route path="/search">
<AddMovieForm showButton='add'/>
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</div>
</Router>
);
}
function Home() {
return (
<img src={logo} className="App-logo" alt="logo" />
);
}
export default App;