@shopify/shop-minis-react
Version:
React component library for Shopify Shop Minis with Tailwind CSS v4 support (source-only, requires TypeScript)
57 lines (49 loc) • 1.37 kB
text/typescript
import {useCallback} from 'react'
import {useShopActions} from '../../internal/useShopActions'
import {useShopActionsPaginatedDataFetching} from '../../internal/useShopActionsPaginatedDataFetching'
import {
ProductList,
PaginatedDataHookOptionsBase,
PaginatedDataHookReturnsBase,
} from '../../types'
import {MiniEntityNotFoundError} from '../../utils/errors'
interface UseProductListParams extends PaginatedDataHookOptionsBase {
/**
* The GID of the product list. E.g. `gid://shopapp/ProductList/123`.
*/
id?: string
/**
* The public ID of the product list.
*/
publicId?: string
}
interface UseProductListResult extends PaginatedDataHookReturnsBase {
productList: ProductList | null
}
export const useProductList = (
params?: UseProductListParams
): UseProductListResult => {
const {getProductList} = useShopActions()
const {skip, ...shopActionParams} = params ?? {}
const validator = useCallback((data: ProductList | null) => {
if (data === null) {
throw new MiniEntityNotFoundError({
hook: 'useProductList',
message: 'Product list not found',
})
}
}, [])
const {data, ...rest} = useShopActionsPaginatedDataFetching(
getProductList,
shopActionParams,
{
skip,
hook: 'useProductList',
validator,
}
)
return {
...rest,
productList: data,
}
}