forked from AdaGold/video-store-consumer
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathApp.js
150 lines (131 loc) · 4.02 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import React, { useState } from 'react';
import {
BrowserRouter as Router,
Switch,
Route,
Link,
withRouter,
} from 'react-router-dom';
import axios from 'axios';
import moment from 'moment';
import 'bootstrap/dist/css/bootstrap.min.css';
import Container from 'react-bootstrap/Container';
import {
Navbar,
Nav,
NavDropdown,
Form,
FormControl,
Button,
Card,
} from 'react-bootstrap';
import Library from './components/Library'
import Customers from './components/Customers'
import Search from './components/Search'
import './App.css';
const App = () => {
const [selectedCustomerID, setSelectedCustomerID] = useState(null);
const [selectedCustomerName, setSelectedCustomerName] = useState(null);
const [selectedVideo, setSelectedVideo] = useState(null);
const [errorMessage, setErrorMessage] = useState(null);
const [dueDate, setDueDate] = useState(null);
const localAPI = 'http://localhost:3000'
const selectCustomerName = (name) => {
setSelectedCustomerName(name);
};
const selectCustomerID = (id) => {
setSelectedCustomerID(id);
};
const selectVideo = (video) => {
setSelectedVideo(video);
};
const getDueDate = () => {
const date = new Date()
date.setDate(date.getDate() + 7)
return (moment(date).format('MMM D, YYYY'))
}
const checkOutVideo = () => {
const date = getDueDate()
// setDueDate(toString(date));
console.log(date)
// console.log(dueDate)
axios.post(`${localAPI}/rentals/${selectedVideo}/check-out?customer_id=${selectedCustomerID}&due_date=${date}`)
.then((response) => {
console.log(`Movie titled ${selectedVideo} checked out to cusomter: ${selectedCustomerName}`)
setErrorMessage(`Movie titled ${selectedVideo} checked out to cusomter: ${selectedCustomerName}`)
})
.catch((error) => {
setErrorMessage(`Unable to checkout movie titled ${selectedVideo} to Customer ID: ${selectedCustomerID}`);
console.log(`Unable to checkout movie titled ${selectedVideo} to Customer ID: ${selectedCustomerID}`)
});
};
const checkOutVideoBtn = () => {
return (
<button onClick={ checkOutVideo } >Check Out</button>
)
}
return (
<Router>
<body>
<header>
<nav>
<ul>
<li><h1 className="title">Rose Video</h1></li>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/library">Library</Link>
</li>
<li>
<Link to="/customers">Customers</Link>
</li>
<li>
<Link to="/search">Search</Link>
</li>
</ul>
</nav>
</header>
<span className="selected" >
<span>Selected Video: { selectedVideo } </span>
<span>Selected Customer: { selectedCustomerName }</span>
<span>{ selectedVideo !== null && selectedCustomerID !== null ? checkOutVideoBtn() : null }</span>
</span>
<div className="selected error-msg">
{ errorMessage }
</div>
<main>
{/* A <Switch> looks through its children <Route>s and
renders the first one that matches the current URL. */}
<Switch>
<Route path="/library">
<Library url={localAPI} setSelectedVideoCallback={selectVideo} />
</Route>
<Route path="/customers">
<Customers url={localAPI} setCustomerIDCallback={selectCustomerID} setCustomerNameCallback={selectCustomerName} />
</Route>
<Route path="/search" >
<Search url={localAPI} />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</main>
</body>
</Router>
);
}
function Home() {
return (<Container>
<Card >
<Card.Body>
<Card.Title>Welcome to R&R Video</Card.Title>
<Card.Text>
Be Kind, Rewind
</Card.Text>
</Card.Body>
</Card>
</Container> )
}
export default App;