Created
May 17, 2022 18:52
-
-
Save forrestwilkins/29e2821e12e293c3501378e7426fd15f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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