Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save forrestwilkins/29e2821e12e293c3501378e7426fd15f to your computer and use it in GitHub Desktop.
Save forrestwilkins/29e2821e12e293c3501378e7426fd15f to your computer and use it in GitHub Desktop.
export const useGetProducts = (): [Product[] | undefined, boolean, unknown] => {
const { data, error, isLoading } = useQuery(ApiRoutes.Products, () =>
api.getProducts().then((res) => res)
);
const products = useAppSelector(selectProducts);
const dispatch = useAppDispatch();
useEffect(() => {
if (data) {
dispatch(setProducts(data));
}
}, [data, dispatch]);
return [products, isLoading, error];
};
const ProductsList = (props: BoxProps) => {
const [products, isProductsLoading] = useGetProducts();
if (isProductsLoading) {
return <ProgressBar />;
}
return (
<Box {...props}>
{products?.map((product) => (
<ProductCard product={product} key={product.id} />
))}
</Box>
);
};
export default ProductsList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment