@@ -58,6 +58,21 @@ class App extends Component {
58
58
this . refreshData ( ) ;
59
59
}
60
60
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
+
61
76
refreshData = async ( ) => {
62
77
const catalogPromise = fetch ( '/catalog' )
63
78
. then ( res => res . json ( ) )
@@ -132,12 +147,14 @@ class App extends Component {
132
147
cost = { cost }
133
148
titles = { rental . data }
134
149
loaded = { rental . loaded }
150
+ onReturn = { this . handleReturn }
135
151
/>
136
152
< TitleList
137
153
title = "Store"
138
154
titles = { catalog . data }
139
155
loaded = { catalog . loaded }
140
156
onRent = { this . handleRent }
157
+
141
158
/>
142
159
</ div >
143
160
</ Route >
@@ -173,7 +190,7 @@ const DevToast = () => {
173
190
174
191
class TitleList extends Component {
175
192
renderList ( ) {
176
- const { titles = [ ] , loaded, onRent } = this . props ;
193
+ const { titles = [ ] , loaded, onRent, onReturn } = this . props ;
177
194
const movies = titles . filter ( item => ! item ?. rented ) ;
178
195
179
196
if ( loaded ) {
@@ -193,6 +210,7 @@ class TitleList extends Component {
193
210
item = { item }
194
211
backdrop = { backDrop }
195
212
onRent = { onRent }
213
+ onReturn = { onReturn }
196
214
/>
197
215
) ;
198
216
} ) ;
@@ -220,7 +238,7 @@ class TitleList extends Component {
220
238
}
221
239
}
222
240
223
- const Item = ( { item, onRent, backdrop } ) => {
241
+ const Item = ( { item, onRent, onReturn , backdrop } ) => {
224
242
return (
225
243
< div className = "Item" >
226
244
< div className = "Item__container" style = { { backgroundImage : `url(./${ backdrop } )` } } >
@@ -242,7 +260,7 @@ const Item = ({ item, onRent, backdrop }) => {
242
260
< div className = "Item__button Item__button--rented button" >
243
261
Watch Now
244
262
</ div >
245
- < div className = "Item__button button" onClick = { ( ) => handleReturn ( item ) } >
263
+ < div className = "Item__button button" onClick = { ( ) => onReturn ( item ) } >
246
264
Return
247
265
</ div >
248
266
</ >
@@ -253,32 +271,6 @@ const Item = ({ item, onRent, backdrop }) => {
253
271
) ;
254
272
}
255
273
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
-
282
274
const CartIcon = ( ) => {
283
275
return (
284
276
< svg xmlns = "http://www.w3.org/2000/svg" fill = "none" viewBox = "0 0 21 22" width = "16" >
0 commit comments