refactor: product page data fetching

This commit is contained in:
OllyNicholass
2024-10-15 23:36:24 +01:00
parent ec684f9074
commit 43bd4e8026
3 changed files with 24 additions and 13 deletions
+7
View File
@@ -0,0 +1,7 @@
import Stripe from 'stripe';
export type ProductInfo = {
product: Stripe.Product;
price: Stripe.Price;
isSubscription: boolean;
};
@@ -1,5 +1,6 @@
import { redirect } from '@sveltejs/kit'; import { redirect } from '@sveltejs/kit';
import { hasProductAccess } from '$lib/utils/supabase/admin'; import { hasProductAccess } from '$lib/utils/supabase/admin';
import type { ProductInfo } from '$lib/types/Products/ProductInfo';
export const load = async ({ params, locals: { safeGetSession }, fetch }) => { export const load = async ({ params, locals: { safeGetSession }, fetch }) => {
const { session } = await safeGetSession(); const { session } = await safeGetSession();
@@ -8,7 +9,7 @@ export const load = async ({ params, locals: { safeGetSession }, fetch }) => {
} }
const productResponse = await fetch(`/products/${params.productId}`); const productResponse = await fetch(`/products/${params.productId}`);
const product = await productResponse.json(); const { product, isSubscription, price }: ProductInfo = await productResponse.json();
// Create the item object // Create the item object
const item = { const item = {
@@ -17,17 +18,13 @@ export const load = async ({ params, locals: { safeGetSession }, fetch }) => {
categoryDescription: product.description, categoryDescription: product.description,
imgAlt: product.description, imgAlt: product.description,
imgSrc: product.images[0], imgSrc: product.images[0],
price: 0,
priceId: product.default_price, priceId: product.default_price,
quantity: 1, quantity: 1,
isSubscription: product.isSubscription isSubscription: isSubscription,
price: (price.unit_amount ? price.unit_amount / 100 : NaN),
interval: price.recurring ? price.recurring.interval : null,
} }
// Fetch the price of the product
const priceResponse = await fetch(`/products/prices/${product.default_price}`);
const price = await priceResponse.json();
// Set the price of the product
item.price = price.unit_amount / 100;
// Check if the user has access to the 'EXAMPLE PRODUCT' product // Check if the user has access to the 'EXAMPLE PRODUCT' product
const hasPurchasedProduct = await hasProductAccess(session.user.id, params.productId); const hasPurchasedProduct = await hasProductAccess(session.user.id, params.productId);
+11 -4
View File
@@ -1,14 +1,21 @@
import { json } from '@sveltejs/kit'; import { json } from '@sveltejs/kit';
import { stripe as stripeClient } from '$lib/utils/stripe'; import { stripe as stripeClient } from '$lib/utils/stripe';
import Stripe from 'stripe'; import Stripe from 'stripe';
import { type ProductInfo } from '$lib/types/Products/ProductInfo';
export const GET = async ({ params }) => { export const GET = async ({ params }) => {
const { productId } = params; const { productId } = params;
const product = await stripeClient.products.retrieve(productId); const product = await stripeClient.products.retrieve(productId);
const price = await stripeClient.prices.retrieve(product.default_price); const price = await stripeClient.prices.retrieve(product.default_price as string);
const isSubscription = price.type === ('recurring' as Stripe.Price.type); const isSubscription = price.type === ('recurring' as Stripe.Price.Type);
product.isSubscription = isSubscription;
return json(product); const productInfo: ProductInfo = {
product,
price,
isSubscription
}
return json(productInfo);
}; };