import { Ref , computed } from '@vue/composition-api' ; import { ComposableFunctionArgs , configureFactoryParams , Context , CustomQuery , FactoryParams , Logger , sharedRef , } from '@vue-storefront/core' ; import { PlatformApi } from '@vue-storefront/core/lib/src/types' ; import { UseReview , UseReviewErrors } from '../types/composables' ; export interface UseReviewFactoryParams < REVIEW , REVIEWS_SEARCH_PARAMS , REVIEWS_USER_SEARCH_PARAMS , REVIEW_ADD_PARAMS , REVIEW_METADATA , API extends PlatformApi = any > extends FactoryParams < API > { searchReviews : ( context : Context , params : ComposableFunctionArgs < REVIEWS_SEARCH_PARAMS > ) => Promise < REVIEW > ; addReview : ( context : Context , params : ComposableFunctionArgs < REVIEW_ADD_PARAMS > ) => Promise < REVIEW > ; loadReviewMetadata : ( context : Context , params ? : ComposableFunctionArgs < { } > ) => Promise < REVIEW_METADATA [ ] > ; loadCustomerReviews : ( context : Context , params : ComposableFunctionArgs < REVIEWS_USER_SEARCH_PARAMS > ) => Promise < REVIEW > ; } export function useReviewFactory < REVIEW , REVIEWS_SEARCH_PARAMS , REVIEWS_USER_SEARCH_PARAMS , REVIEW_ADD_PARAMS , REVIEW_METADATA , API extends PlatformApi = any > ( factoryParams ) { return function useReview ( id ) : UseReview < REVIEW , REVIEWS_SEARCH_PARAMS , REVIEWS_USER_SEARCH_PARAMS , REVIEW_ADD_PARAMS , REVIEW_METADATA , API > { const reviews = sharedRef ( [ ] , `useReviews-reviews-${ id }` ) ; const metadatas = sharedRef ( [ ] , `useReviews-metadata-${ id }` ) ; const loading = sharedRef ( false , `useReviews-loading-${ id }` ) ; const error = sharedRef ( { search : null , addReview : null , loadReviewMetadata : null , loadCustomerReviews : null , } , `useReviews-error-${ id }` ) ; const _factoryParams = configureFactoryParams ( factoryParams ) ; const search = async ( searchParams ? ) : Promise < void > => { Logger . debug ( `useReview/${ id }/search` , searchParams ) ; try { loading . value = true ; reviews . value = await _factoryParams . searchReviews ( searchParams ) ; error . value . search = null ; } catch ( err ) { error . value . search = err ; Logger . error ( `useReview/${ id }/search` , err ) ; } finally { loading . value = false ; } } ; const loadCustomerReviews = async ( searchParams ? ) : Promise < void > => { Logger . debug ( `useReview/${ id }/loadCustomerReviews` , searchParams ) ; try { loading . value = true ; reviews . value = await _factoryParams . loadCustomerReviews ( searchParams ) ; error . value . loadCustomerReviews = null ; } catch ( err ) { error . value . loadCustomerReviews = err ; Logger . error ( `useReview/${ id }/loadCustomerReviews` , err ) ; } finally { loading . value = false ; } } ; const loadReviewMetadata = async ( params ? ) : Promise < void > => { Logger . debug ( `useReview/${ id }/loadReviewMetadata` ) ; try { loading . value = true ; metadatas . value = await _factoryParams . loadReviewMetadata ( params ) ; error . value . loadReviewMetadata = null ; } catch ( err ) { error . value . loadReviewMetadata = err ; Logger . error ( `useReview/${ id }/loadReviewMetadata` , err ) ; } finally { loading . value = false ; } } ; const addReview = async ( params ) : Promise < void > => { Logger . debug ( `useReview/${ id }/addReview` , params ) ; try { loading . value = true ; reviews . value = await _factoryParams . addReview ( params ) ; error . value . addReview = null ; } catch ( err ) { error . value . addReview = err ; Logger . error ( `useReview/${ id }/addReview` , err ) ; } finally { loading . value = false ; } } ; return { search , addReview , loadReviewMetadata , loadCustomerReviews , metadata : computed ( ( ) => metadatas . value ) , reviews : computed ( ( ) => reviews . value ) , loading : computed ( ( ) => loading . value ) , error : computed ( ( ) => error . value ) , } ; } ; }