export default function useGetProduct() {
const route = useRoute();
const handle = route.params.product;
const regionsStore = useRegionsStore();
const { regionId, countryCode } = storeToRefs(regionsStore);
const queryParams = new URLSearchParams({
fields: `*variants.calculated_price`,
region_id: regionId.value,
country_code: countryCode.value,
});
//tried a function as well
const getQueryParams = () => {
return new URLSearchParams({
fields: `*variants.calculated_price`,
region_id: regionId.value,
country_code: countryCode.value,
}).toString();
};
const { MEDUSA_PUBLISHABLE_KEY } = useRuntimeConfig().public;
const { error, data, refresh } = useFetch(
`http://localhost:9000/store/products?handle=${handle}&${getQueryParams()}`,
{
credentials: "include",
headers: {
"x-publishable-api-key": MEDUSA_PUBLISHABLE_KEY,
},
}
);
const getProduct = async () => {
//values gets uodated
console.log(regionId.value, countryCode.value);
console.log(getQueryParams());
await refresh();
if (error.value) {
console.error("error", error.value);
} else {
return data.value.products[0];
}
};
return { getProduct };
}