1
- import React , { useEffect , useState } from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
import { useLazyQuery , gql } from '@apollo/client' ;
3
3
import { toast } from 'react-toastify' ;
4
4
import 'react-toastify/dist/ReactToastify.css' ;
5
5
6
- const GetDispatchedProductByDateForm = ( ) => {
6
+ const FindOffersForDate = ( ) => {
7
7
const [ date , setDate ] = useState ( '' ) ;
8
- const [ dispatchedProduct , setDispatchedProduct ] = useState ( null ) ;
8
+ const [ offers , setOffers ] = useState ( null ) ;
9
9
10
10
const formatDateString = ( dateString ) => {
11
- return dateString . replace ( / - / g, '/' ) ; // Replace hyphens with slashes
11
+ return dateString . replace ( / - / g, '/' ) ;
12
12
} ;
13
13
14
- const GET_DISPATCHED_PRODUCT_BY_DATE = gql `
14
+ const FIND_OFFERS_FOR_DATE = gql `
15
15
query {
16
- getDispatchedProductByDate (date: "${ formatDateString ( date ) } ") {
17
- id
18
- dispatchDate
16
+ findOffersForDate (date: "${ formatDateString ( date ) } ") {
17
+ offerCode
18
+ validUntil
19
19
}
20
20
}
21
21
` ;
22
22
23
- const [ getDispatchedProductByDate , { loading } ] = useLazyQuery ( GET_DISPATCHED_PRODUCT_BY_DATE , {
23
+ const [ findOffersForDate , { loading } ] = useLazyQuery ( FIND_OFFERS_FOR_DATE , {
24
24
fetchPolicy : 'network-only' ,
25
25
onCompleted : ( data ) => {
26
- setDispatchedProduct ( data . getDispatchedProductByDate ) ;
26
+ setOffers ( data . findOffersForDate ) ;
27
27
} ,
28
28
onError : ( ) => {
29
- setDispatchedProduct ( null ) ;
30
- toast . error ( 'Error fetching dispatched product ' ) ;
29
+ setOffers ( null ) ;
30
+ toast . error ( 'Error fetching offers ' ) ;
31
31
} ,
32
32
} ) ;
33
33
@@ -40,12 +40,12 @@ const GetDispatchedProductByDateForm = () => {
40
40
return ;
41
41
}
42
42
43
- getDispatchedProductByDate ( { variables : { date } } ) ;
43
+ findOffersForDate ( { variables : { date } } ) ;
44
44
} ;
45
45
46
46
return (
47
47
< div className = "max-w-md mx-auto p-8 bg-white shadow-lg rounded-lg" >
48
- < h1 className = "text-2xl font-bold mb-4" > Get Dispatched Product by Date </ h1 >
48
+ < h1 className = "text-2xl font-bold mb-4" > Find offers valid until a certain date </ h1 >
49
49
< form onSubmit = { handleSubmit } >
50
50
< div className = "mb-4" >
51
51
< label className = "block text-gray-700 text-sm font-bold mb-2" htmlFor = "date" >
@@ -71,18 +71,21 @@ const GetDispatchedProductByDateForm = () => {
71
71
</ button >
72
72
</ div >
73
73
</ form >
74
- { dispatchedProduct && (
75
- < div className = "mt-8" >
76
- < h2 className = "text-xl font-bold mb-4" > Dispatched Product</ h2 >
77
- < div className = "p-4 border rounded-lg bg-gray-50 shadow-sm" >
78
- < p > < strong > ID:</ strong > { dispatchedProduct . id } </ p >
79
- < p > < strong > Dispatch Date:</ strong > { new Date ( dispatchedProduct . dispatchDate ) . toLocaleDateString ( ) } </ p >
74
+ { offers && offers . map ( offer => {
75
+ return ( < >
76
+ < div className = "mt-8" >
77
+ < div className = "p-4 border rounded-lg bg-gray-50 shadow-sm" >
78
+ < p > < strong > Offer code:</ strong > { offer . offerCode } </ p >
79
+ < p > < strong > Valid Until:</ strong > { new Date ( offer . validUntil ) . toLocaleDateString ( ) } </ p >
80
+ </ div >
80
81
</ div >
81
- </ div >
82
- ) }
82
+ </ >
83
+ )
84
+ } )
85
+ }
83
86
</ div >
84
87
) ;
85
88
} ;
86
89
87
- export default GetDispatchedProductByDateForm ;
90
+ export default FindOffersForDate ;
88
91
0 commit comments