Skip to content

Commit c7b5b66

Browse files
committed
add a return movie functionality
1 parent ff06833 commit c7b5b66

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

frontend/src/App.jsx

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ class App extends Component {
5858
this.refreshData();
5959
}
6060

61+
handleReturn = async (item) =>{
62+
await fetch('/rent/return', {
63+
method: 'POST',
64+
headers: {
65+
'Content-Type': 'application/json'
66+
},
67+
body: JSON.stringify({
68+
catalog_id: item.id
69+
})
70+
});
71+
this.refreshData();
72+
73+
}
74+
75+
6176
refreshData = async () => {
6277
const catalogPromise = fetch('/catalog')
6378
.then(res => res.json())
@@ -132,12 +147,14 @@ class App extends Component {
132147
cost={cost}
133148
titles={rental.data}
134149
loaded={rental.loaded}
150+
onReturn={this.handleReturn}
135151
/>
136152
<TitleList
137153
title="Store"
138154
titles={catalog.data}
139155
loaded={catalog.loaded}
140156
onRent={this.handleRent}
157+
141158
/>
142159
</div>
143160
</Route>
@@ -173,7 +190,7 @@ const DevToast = () => {
173190

174191
class TitleList extends Component {
175192
renderList() {
176-
const { titles = [], loaded, onRent } = this.props;
193+
const { titles = [], loaded, onRent, onReturn } = this.props;
177194
const movies = titles.filter(item => !item?.rented);
178195

179196
if (loaded) {
@@ -193,6 +210,7 @@ class TitleList extends Component {
193210
item={item}
194211
backdrop={backDrop}
195212
onRent={onRent}
213+
onReturn={onReturn}
196214
/>
197215
);
198216
});
@@ -220,7 +238,7 @@ class TitleList extends Component {
220238
}
221239
}
222240

223-
const Item = ({ item, onRent, backdrop }) => {
241+
const Item = ({ item, onRent, onReturn, backdrop }) => {
224242
return (
225243
<div className="Item">
226244
<div className="Item__container" style={{ backgroundImage: `url(./${backdrop})` }}>
@@ -242,7 +260,7 @@ const Item = ({ item, onRent, backdrop }) => {
242260
<div className="Item__button Item__button--rented button">
243261
Watch Now
244262
</div>
245-
<div className="Item__button button" onClick={() => handleReturn(item)}>
263+
<div className="Item__button button" onClick={() => onReturn(item)}>
246264
Return
247265
</div>
248266
</>
@@ -253,32 +271,6 @@ const Item = ({ item, onRent, backdrop }) => {
253271
);
254272
}
255273

256-
function handleReturn(item) {
257-
fetch('/rent/return', {
258-
method: 'POST',
259-
headers: {
260-
'Content-Type': 'application/json'
261-
},
262-
body: JSON.stringify({
263-
catalog_id: item.id
264-
})
265-
})
266-
.then(response => {
267-
if (!response.ok) {
268-
throw new Error('Network response was not ok');
269-
}
270-
return response.json();
271-
})
272-
.then(data => {
273-
console.log(`Returned movie with ID: ${item.id}`);
274-
// Refresh data to update the UI
275-
this.refreshData();
276-
})
277-
.catch(error => {
278-
console.error('There was a problem with the return request:', error);
279-
});
280-
}
281-
282274
const CartIcon = () => {
283275
return (
284276
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 21 22" width="16">

0 commit comments

Comments
 (0)