diff options
| author | eug-vs <eug-vs@keemail.me> | 2021-03-14 06:30:22 +0300 | 
|---|---|---|
| committer | eug-vs <eug-vs@keemail.me> | 2021-03-14 06:30:22 +0300 | 
| commit | 610584c3ce986cdea431180f545e023cee14d5d2 (patch) | |
| tree | a4c1b2a3de7b4ccf7cef8a4bdf5d366cc62492b5 /src/hooks | |
| parent | 790cb41dfe3283fea96789fbacc28a077c474e44 (diff) | |
| download | commercel-ui-610584c3ce986cdea431180f545e023cee14d5d2.tar.gz | |
feat: implement Product Add/Edit
Diffstat (limited to 'src/hooks')
| -rw-r--r-- | src/hooks/useAPIClient.ts | 18 | 
1 files changed, 16 insertions, 2 deletions
diff --git a/src/hooks/useAPIClient.ts b/src/hooks/useAPIClient.ts index a27453e..b36cfec 100644 --- a/src/hooks/useAPIClient.ts +++ b/src/hooks/useAPIClient.ts @@ -1,4 +1,5 @@  import useSWR, { responseInterface } from 'swr'; +import _ from 'lodash';  import { get } from '../requests';  type Response<T> = responseInterface<T, Error>; @@ -16,6 +17,19 @@ export interface Product {  const fetcher = (endpoint: string) => get(endpoint).then(response => response.data); -export const useProducts = (): Response<Product[]> => { -  return useSWR('/products', fetcher); +export const useProducts = (options = {}): Response<Product[]> => { +  return useSWR('/products', fetcher, options);  }; + +export const useProduct = (_id: string): Response<Product> => { +  const { data: preloadedProducts } = useProducts({ revalidateOnMount: false }); +  const result = useSWR(`/products/${_id}`, fetcher); +  if (!result.data && result.isValidating) { +    // If we are waiting for the first result, check if we can maybe +    // get the data from already cached list for the time-being +    const product = _.find(preloadedProducts, { _id }); +    return { ...result, data: product }; +  } +  return result; +}; +  |