1
- import { useState , useEffect } from 'react' ;
2
- import { getProducts } from '../api' ;
3
- import { useNavigate } from 'react-router-dom' ;
1
+ import { useEffect } from 'react' ;
4
2
import { Card } from "./Card"
5
3
import { TagRadios } from '../radio' ;
6
4
import { Chip } from '@material-ui/core' ;
7
5
import Tooltip from '@mui/material/Tooltip' ;
6
+ import { queryProducts , queryProductsWithLimit } from '../query' ;
8
7
9
8
/* eslint-disable jsx-a11y/anchor-is-valid */
10
9
/* eslint-disable @typescript-eslint/no-unused-vars */
@@ -20,38 +19,13 @@ interface Props {
20
19
setTotal : ( state : any ) => void ;
21
20
}
22
21
23
-
24
22
export const Home = ( props : Props ) => {
25
- const [ error , setError ] = useState < string > ( '' ) ;
26
- const [ skip , setSkip ] = useState ( 0 ) ;
27
- const [ limit , setLimit ] = useState ( 15 ) ;
28
- const Navigate = useNavigate ( ) ;
29
-
30
- const queryProducts = async ( ) => {
31
- try {
32
- const result = await getProducts ( limit , skip , props . gender , props . category ) ;
33
- props . setProducts ( result . products )
34
- props . setTotal ( result . total )
35
- } catch ( err ) {
36
- setError ( String ( err ) ) ;
37
- }
38
- } ;
39
-
40
- const queryProductsWithLimit = async ( ) => {
41
- try {
42
- setSkip ( skip + limit ) ;
43
- queryProducts ( ) ;
44
- } catch ( err ) {
45
- console . log ( err ) ;
46
- } ;
47
- } ;
48
-
49
23
// Execute this one when the component loads up
50
24
useEffect ( ( ) => {
51
25
// clear filters
52
26
props . setGender ( "" ) ;
53
27
props . setCategory ( "" ) ;
54
- queryProductsWithLimit ( ) ;
28
+ queryProductsWithLimit ( props ) ;
55
29
} , [ ] ) ;
56
30
57
31
return (
@@ -72,14 +46,19 @@ export const Home = (props: Props) => {
72
46
< TagRadios
73
47
gender = { props . gender }
74
48
category = { props . category }
49
+ products = { props . products }
50
+ total = { props . total }
75
51
setGender = { props . setGender }
76
- setCategory = { props . setCategory } />
52
+ setCategory = { props . setCategory }
53
+ setProducts = { props . setProducts }
54
+ setTotal = { props . setTotal }
55
+ />
77
56
</ div >
78
57
) : (
79
58
< > </ >
80
59
) }
81
60
< Tooltip title = "Fetch more products from Redis" arrow >
82
- < a className = "btn btn-primary m-2" onClick = { ( ) => queryProductsWithLimit ( ) } >
61
+ < a className = "btn btn-primary m-2" onClick = { ( ) => queryProductsWithLimit ( props ) } >
83
62
Load More Products
84
63
</ a >
85
64
</ Tooltip >
@@ -102,7 +81,7 @@ export const Home = (props: Props) => {
102
81
variant = 'outlined'
103
82
clickable
104
83
color = 'primary'
105
- onDelete = { ( ) => { props . setCategory ( "" ) ; queryProducts ( ) } }
84
+ onDelete = { ( ) => { props . setCategory ( "" ) ; queryProducts ( props , props . gender , "" ) } }
106
85
disabled = { props . category == '' }
107
86
/>
108
87
) :(
@@ -115,7 +94,7 @@ export const Home = (props: Props) => {
115
94
variant = 'outlined'
116
95
clickable
117
96
color = 'primary'
118
- onDelete = { ( ) => { props . setGender ( "" ) ; queryProducts ( ) } }
97
+ onDelete = { ( ) => { props . setGender ( "" ) ; queryProducts ( props , "" , props . category ) } }
119
98
disabled = { props . gender == '' }
120
99
/>
121
100
) :(
0 commit comments