@@ -8,18 +8,43 @@ import {
88 AreaLoader ,
99 Inset ,
1010 Tiles ,
11+ TextField ,
12+ SliderField ,
1113} from "@buildo/bento-design-system" ;
14+ import useGetRestaurantList from "../hooks" ;
1215import RestaurantPreview from "../components/RestaurantPreview" ;
13- import PriceFilter from "../components/PriceFilter" ;
14- import LocationFilter from "../components/LocationFilter" ;
15- import RangeDistanceFilter from "../components/RangeDistanceFilter" ;
1616import { useTranslation } from "react-i18next" ;
17- import React from "react " ;
18- import useGetRestaurantList from "../hooks " ;
17+ import { validators , useFormo , success } from "@buildo/formo " ;
18+ import { useEffect } from "react " ;
1919
2020function Home ( ) {
2121 const { t } = useTranslation ( ) ;
22- const { isLoading, isError, data } = useGetRestaurantList ( 10 ) ;
22+ const initialValues = {
23+ prices : [ true , true , true , true ] ,
24+ location : "Milan" ,
25+ radius : 0 ,
26+ } ;
27+
28+ const { fieldProps, handleSubmit, values } = useFormo (
29+ {
30+ initialValues,
31+ fieldValidators : ( ) => ( {
32+ location : validators . minLength ( 2 , "City name is too short" ) ,
33+ } ) ,
34+ } ,
35+ {
36+ onSubmit : async ( values ) => {
37+ refetch ( ) ;
38+ return success ( values ) ;
39+ } ,
40+ }
41+ ) ;
42+
43+ useEffect ( ( ) => {
44+ refetch ( ) ;
45+ } , [ ] ) ; // eslint-disable-line react-hooks/exhaustive-deps
46+
47+ const { isLoading, isError, data, refetch } = useGetRestaurantList ( values ) ;
2348
2449 if ( isLoading ) {
2550 return < AreaLoader message = "Loading..." > </ AreaLoader > ;
@@ -36,30 +61,6 @@ function Home() {
3661 ) ;
3762 }
3863
39- const [ filters , setFilters ] = React . useState ( {
40- prices : [ false , false , false , false ] ,
41- location : "" ,
42- range : 0 ,
43- } ) ;
44-
45- const setPricesFilter = ( prices : boolean [ ] ) =>
46- setFilters ( {
47- ...filters ,
48- prices,
49- } ) ;
50-
51- const setRangeFilter = ( range : number ) =>
52- setFilters ( {
53- ...filters ,
54- range,
55- } ) ;
56-
57- const setLocationFilter = ( location : string ) =>
58- setFilters ( {
59- ...filters ,
60- location,
61- } ) ;
62-
6364 const cards = data ?. businesses . map ( ( element ) => {
6465 return < RestaurantPreview key = { "home-" + element . alias } { ...element } /> ;
6566 } ) ;
@@ -71,25 +72,33 @@ function Home() {
7172 >
7273 < Box height = "full" padding = { 24 } >
7374 < Stack space = { 16 } align = "left" >
74- < PriceFilter price = { filters . prices } setPrice = { setPricesFilter } />
75+ { /* price filters */ }
76+
7577 < Divider />
76- < LocationFilter
77- location = { filters . location }
78- setLocation = { setLocationFilter }
78+ { /* location filters */ }
79+ < TextField
80+ { ...fieldProps ( "location" ) }
81+ label = { t ( "Location.Label" ) }
82+ placeholder = { t ( "Location.Placeholder" ) }
7983 />
8084 < Divider />
85+ { /* range filters */ }
8186 < Box width = "full" >
82- < RangeDistanceFilter
83- distance = { filters . range }
84- setDistance = { setRangeFilter }
87+ < SliderField
88+ type = "single"
89+ label = { t ( "RangeDistance.Label" ) }
90+ minValue = { 0 }
91+ maxValue = { 10 }
92+ step = { 1 }
93+ { ...fieldProps ( "radius" ) }
8594 />
8695 </ Box >
8796 < Divider />
8897 < Button
8998 kind = "solid"
9099 hierarchy = "primary"
91100 label = { t ( "SearchButton" ) }
92- onPress = { ( ) => window . alert ( "" + filters . location ) }
101+ onPress = { handleSubmit }
93102 />
94103 </ Stack >
95104 </ Box >
0 commit comments