Skip to content

Commit

Permalink
Refactor to avoid refetch
Browse files Browse the repository at this point in the history
  • Loading branch information
gabro committed Nov 21, 2023
1 parent 13aa9b0 commit 5c3f8f7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export function useGetRestaurantList(filtersParams: {
async (): Promise<PreviewList> => {
const prom: JSON = await getRestaurantList(filtersParams);
return fromJsonToPropPreview(prom);
},
{ enabled: false }
}
);
}

Expand Down
14 changes: 6 additions & 8 deletions src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useGetRestaurantList } from "../hooks";
import RestaurantPreview from "../components/RestaurantPreview";
import { useTranslation } from "react-i18next";
import { validators, useFormo, success } from "@buildo/formo";
import { useEffect } from "react";
import { useState } from "react";
import { PreviewPropComponent } from "../models";

function Home() {
Expand All @@ -27,7 +27,9 @@ function Home() {
radius: 10,
};

const { fieldProps, handleSubmit, values } = useFormo(
const [activeFilters, setActiveFilters] = useState(initialValues);

const { fieldProps, handleSubmit } = useFormo(
{
initialValues,
fieldValidators: () => ({
Expand All @@ -36,17 +38,13 @@ function Home() {
},
{
onSubmit: async (values) => {
refetch();
setActiveFilters(values);
return success(values);
},
}
);

useEffect(() => {
refetch();
}, []); // eslint-disable-line react-hooks/exhaustive-deps

const { isLoading, isError, data, refetch } = useGetRestaurantList(values);
const { isLoading, isError, data } = useGetRestaurantList(activeFilters);

if (isLoading) {
return <AreaLoader message="Loading..."></AreaLoader>;
Expand Down

0 comments on commit 5c3f8f7

Please sign in to comment.