forked from AdaGold/video-store-consumer
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathApp.js
172 lines (156 loc) · 4.51 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import VideoList from './components/VideoList';
import SearchForm from './components/SearchForm';
import { Route, Link } from 'react-router-dom';
import CustomerCollection from './components/CustomerCollection';
import NavBar from './components/NavBar';
import Cart from './components/Cart';
import { Container, Row, Col, Alert } from 'react-bootstrap';
import './App.css';
import { useLocation } from 'react-router-dom';
const API_URL_BASE = 'http://localhost:3000/videos';
const App = () => {
const [videoList, setVideoList] = useState([]);
const [customerList, setCustomerList] = useState([]);
const [errorMessage, setErrorMessage] = useState(null);
const [selectedCustomer, setSelectedCustomer] = useState('');
const [selectedVideo, setSelectedVideo] = useState('');
const [videoResults, setVideoResults] = useState([]);
useEffect(() => {
axios
.get(API_URL_BASE)
.then((response) => {
const apiVideoList = response.data;
setVideoList(apiVideoList);
})
.catch((error) => {
setErrorMessage(error.message);
});
axios
.get('http://localhost:3000/customers')
.then((response) => {
const apiCustomerList = response.data;
console.log(apiCustomerList);
setCustomerList(apiCustomerList);
})
.catch((error) => {
setErrorMessage(error.message);
console.log(error.message);
});
}, []);
const selectCustomerToCart = (customer) => {
setSelectedCustomer(customer);
};
const selectVideoToCart = (video) => {
setSelectedVideo(video);
};
const checkout = (customer, video) => {
const params = {};
params['customer_id'] = customer.id;
params['due_date'] = '2/1/2022';
console.log(params);
axios
.post(`http://localhost:3000/rentals/${video.title}/check-out`, params)
.then((response) => {
setErrorMessage(`Checkout ${video.title} to ${customer.name}.`);
})
.catch((error) => {
setErrorMessage(error.message);
});
setSelectedCustomer('');
setSelectedVideo('');
};
const searchVideo = (query) => {
axios
.get(`${API_URL_BASE}?query=${query}`)
.then((response) => {
const searchResults = response.data;
setVideoResults(searchResults);
setErrorMessage('');
})
.catch((error) => {
setErrorMessage(error.message);
});
};
const selectVideo = (video) => {
axios
.post(`${API_URL_BASE}`, video)
.then((response) => {
setErrorMessage(`Succesfully added ${video.title} to the library.`);
})
.catch((error) => {
setErrorMessage(error.message);
});
};
return (
<main>
<header>
<h1>
<Link to={'/'}>Aloha Video Store</Link>
</h1>
</header>
<div>
<Alert variant="info">{errorMessage}</Alert>
</div>
<nav>
<NavBar location={useLocation()} />
</nav>
<Container>
<Row>
<Col>
<Route
exact={true}
path={'/'}
render={() => <h2>Welcome to Aloha Video Store!</h2>}
/>
<Route
path={'/library'}
render={(props) => (
<VideoList
{...props}
videoList={videoList}
selectedVideo={selectedVideo}
onClickSelect={selectVideoToCart}
/>
)}
/>
<Route
path={'/customers'}
render={(props) => (
<CustomerCollection
{...props}
customerList={customerList}
selectedCustomer={selectedCustomer}
onClickSelect={selectCustomerToCart}
/>
)}
/>
</Col>
<Col md="auto"></Col>
<Col xs lg="2">
<Cart
customer={selectedCustomer}
video={selectedVideo}
onClickCheckout={checkout}
/>
</Col>
</Row>
</Container>
<Route
path={'/search'}
render={(props) => (
<SearchForm
{...props}
videoList={videoList}
searchVideoCallback={searchVideo}
videoResults={videoResults}
videoSelectionCallback={selectVideo}
/>
)}
/>
<footer>Copyright</footer>
</main>
);
};
export default App;