{"version":3,"file":"useDataFetcher-49612346.js","sources":["../../src/composables/useDataFetcher.ts"],"sourcesContent":["import { ref } from 'vue';\r\nimport { carAdvisoryService } from '../foundation/apiCarAdvisoryService';\r\nimport { useCarReviewStore } from '../foundation/stores/carReviewStore';\r\nimport { IExpertReview, IExpertReviewWithImages } from '../foundation/interfaces/IExpertReview';\r\nimport { useCarComparisonStore } from '../foundation/stores/carComparisonStore';\r\nimport { ApiStatusTypes } from '../foundation/types/apiStatusTypes';\r\nimport { IReviewSearchResult } from '../foundation/interfaces/IReviewSearchResult';\r\nimport { IReviewResponse } from '../foundation/interfaces/IReviewSearchResult';\r\nimport { useCarSelectStore } from '../foundation/stores/carSelectStore';\r\n/**\r\n * Helpful composables for data fetching in Vue\r\n *\r\n */\r\nexport function useDataFetcher() {\r\n const comparisonStore = useCarComparisonStore();\r\n const reviewStore = useCarReviewStore();\r\n const apiStatus = ref<ApiStatusTypes>('LOADING');\r\n\r\n /**\r\n * Fetch Car Details and Expert Reviews by UID.\r\n *\r\n * @param url\r\n * @param initial\r\n *\r\n * @returns\r\n */\r\n function fetchCarDetailsAndExpertReviewsById(carId: string, reviewId: string) {\r\n const expertReviewData = ref<Partial<IExpertReview>>({});\r\n const carData = ref<Record<string, any> | undefined>(undefined);\r\n const expertReviewWithImages = ref<IExpertReviewWithImages[]>([]);\r\n const reviewSearchData = ref<IReviewSearchResult>();\r\n apiStatus.value = 'LOADING';\r\n\r\n // Not sure if we include this?\r\n // If we do, we need to incorporate cache-busting (which is planned in the future)\r\n if (carId && comparisonStore.fetchedVehicles[carId]) {\r\n carData.value = comparisonStore.fetchedVehicles[carId];\r\n }\r\n\r\n function filterNullReviewData(data: any) {\r\n if (data.Long_Format_Review) {\r\n data.Long_Format_Review = data.Long_Format_Review.filter((item: any) => item.body !== 'null');\r\n }\r\n\r\n return data;\r\n }\r\n\r\n async function executeApiCall() {\r\n try {\r\n const data: Record<string, any> = await carAdvisoryService.fetchVehicleById(carId);\r\n comparisonStore.updateFetchedVehicles(carId, data);\r\n carData.value = data;\r\n\r\n // Fetch expert review data here so that we can update the sticky nav if review exists\r\n await reviewStore.fetchReviewById(reviewId);\r\n const expReviewData: IExpertReview = reviewStore.getReviewById(reviewId);\r\n reviewSearchData.value = reviewStore.getRelevantReview(carData?.value?.Make, carData?.value?.Model, carData?.value?.Trim)?.document;\r\n\r\n if (expReviewData) {\r\n expertReviewData.value = filterNullReviewData(expReviewData);\r\n\r\n if (expertReviewData.value?.Long_Format_Review && expertReviewData.value?.Long_Format_Media) {\r\n expertReviewWithImages.value = [...expertReviewData.value.Long_Format_Review, ...expertReviewData.value.Long_Format_Media].sort(\r\n (a, b) => a.order - b.order\r\n );\r\n }\r\n }\r\n\r\n apiStatus.value = 'IDLE';\r\n } catch (err) {\r\n console.error('vehicleDataRequestsError', err);\r\n apiStatus.value = 'ERROR';\r\n }\r\n }\r\n\r\n // Run the async/await code in composable so it updates here and\r\n // doesn't block rendering in the component that consumes this.\r\n // Ref: https://www.vuemastery.com/blog/coding-better-composables-5-of-5/\r\n executeApiCall();\r\n\r\n return {\r\n carData,\r\n apiStatus,\r\n expertReviewData,\r\n expertReviewWithImages,\r\n reviewSearchData,\r\n };\r\n }\r\n\r\n async function fetchReviewDataForListing(cars: any[]): Promise<IReviewResponse> {\r\n const carSelectStore = useCarSelectStore();\r\n try {\r\n const sharedFilter = carSelectStore.getSearchCriteriaForMakeAndModel(cars)();\r\n const requestBody = {\r\n filter: sharedFilter,\r\n includeTotalCount: true,\r\n count: 100,\r\n skip: 0,\r\n //this will be fixed once API is working\r\n fields: [\r\n 'vehicle_uid',\r\n 'review_id',\r\n 'mobility_club',\r\n 'vehicle_model_year',\r\n 'vehicle_make',\r\n 'vehicle_model',\r\n 'vehicle_trim',\r\n 'vehicle_model_correct_case',\r\n 'vehicle_make_correct_case',\r\n 'vehicle_trim_correct_case',\r\n 'date_of_car_review',\r\n 'score',\r\n ],\r\n };\r\n return carAdvisoryService.fetchVehiclesReviewsBySearch(requestBody);\r\n } catch (err) {\r\n console.error('vehicleDataRequestsError', err);\r\n }\r\n return {} as IReviewResponse;\r\n }\r\n\r\n return {\r\n fetchCarDetailsAndExpertReviewsById,\r\n fetchReviewDataForListing,\r\n };\r\n}\r\n"],"names":["useDataFetcher","comparisonStore","useCarComparisonStore","reviewStore","useCarReviewStore","apiStatus","ref","fetchCarDetailsAndExpertReviewsById","carId","reviewId","expertReviewData","carData","expertReviewWithImages","reviewSearchData","filterNullReviewData","data","item","executeApiCall","carAdvisoryService","expReviewData","_d","_a","_b","_c","_e","_f","a","b","err","fetchReviewDataForListing","cars","carSelectStore","useCarSelectStore","requestBody"],"mappings":"qIAaO,SAASA,GAAiB,CAC/B,MAAMC,EAAkBC,IAClBC,EAAcC,IACdC,EAAYC,EAAoB,SAAS,EAUtC,SAAAC,EAAoCC,EAAeC,EAAkB,CACtE,MAAAC,EAAmBJ,EAA4B,CAAA,CAAE,EACjDK,EAAUL,EAAqC,MAAS,EACxDM,EAAyBN,EAA+B,CAAA,CAAE,EAC1DO,EAAmBP,IACzBD,EAAU,MAAQ,UAIdG,GAASP,EAAgB,gBAAgBO,KACnCG,EAAA,MAAQV,EAAgB,gBAAgBO,IAGlD,SAASM,EAAqBC,EAAW,CACvC,OAAIA,EAAK,qBACFA,EAAA,mBAAqBA,EAAK,mBAAmB,OAAQC,GAAcA,EAAK,OAAS,MAAM,GAGvFD,CACT,CAEA,eAAeE,GAAiB,iBAC1B,GAAA,CACF,MAAMF,EAA4B,MAAMG,EAAmB,iBAAiBV,CAAK,EACjEP,EAAA,sBAAsBO,EAAOO,CAAI,EACjDJ,EAAQ,MAAQI,EAGV,MAAAZ,EAAY,gBAAgBM,CAAQ,EACpC,MAAAU,EAA+BhB,EAAY,cAAcM,CAAQ,EACvEI,EAAiB,OAAQO,EAAAjB,EAAY,mBAAkBkB,EAAAV,GAAA,YAAAA,EAAS,QAAT,YAAAU,EAAgB,MAAMC,EAAAX,GAAA,YAAAA,EAAS,QAAT,YAAAW,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