Skip to content

Commit

Permalink
feat: replace cart with VIBES cart
Browse files Browse the repository at this point in the history
  • Loading branch information
apledger committed Jan 2, 2025
1 parent 6c4a009 commit 6392cb6
Show file tree
Hide file tree
Showing 31 changed files with 754 additions and 1,870 deletions.
50 changes: 38 additions & 12 deletions core/app/[locale]/(default)/cart/_actions/redirect-to-checkout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use server';

import { getLocale } from 'next-intl/server';
import { SubmissionResult } from '@conform-to/react';
import { parseWithZod } from '@conform-to/zod';
import { cookies } from 'next/headers';
import { getLocale, getTranslations } from 'next-intl/server';
import { z } from 'zod';

import { getSessionCustomerAccessToken } from '~/auth';
Expand All @@ -20,23 +23,46 @@ const CheckoutRedirectMutation = graphql(`
}
`);

export const redirectToCheckout = async (formData: FormData) => {
export const redirectToCheckout = async (
_lastResult: SubmissionResult | null,
formData: FormData,
): Promise<SubmissionResult | null> => {
const locale = await getLocale();
const cartId = z.string().parse(formData.get('cartId'));
const t = await getTranslations('Cart.Errors');
const cookieStore = await cookies();

const customerAccessToken = await getSessionCustomerAccessToken();

const { data } = await client.fetch({
document: CheckoutRedirectMutation,
variables: { cartId },
fetchOptions: { cache: 'no-store' },
customerAccessToken,
});
const submission = parseWithZod(formData, { schema: z.object({}) });

const cartId = cookieStore.get('cartId')?.value;

if (!cartId) {
return submission.reply({ formErrors: [t('cartNotFound')] });
}

let url;

const url = data.cart.createCartRedirectUrls.redirectUrls?.redirectedCheckoutUrl;
try {
const { data } = await client.fetch({
document: CheckoutRedirectMutation,
variables: { cartId },
fetchOptions: { cache: 'no-store' },
customerAccessToken,
});

url = data.cart.createCartRedirectUrls.redirectUrls?.redirectedCheckoutUrl;
} catch (error) {
if (error instanceof Error) {
return submission.reply({ formErrors: [error.message] });
}

return submission.reply({ formErrors: [String(error)] });
}

if (!url) {
throw new Error('Invalid checkout url.');
return submission.reply({ formErrors: [t('failedToRedirectToCheckout')] });
}

redirect({ href: url, locale });
return redirect({ href: url, locale });
};
13 changes: 8 additions & 5 deletions core/app/[locale]/(default)/cart/_actions/remove-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { expireTag } from 'next/cache';
import { cookies } from 'next/headers';
import { getTranslations } from 'next-intl/server';

import { getSessionCustomerAccessToken } from '~/auth';
import { client } from '~/client';
Expand All @@ -26,18 +27,20 @@ type DeleteCartLineItemInput = Variables['input'];
export async function removeItem({
lineItemEntityId,
}: Omit<DeleteCartLineItemInput, 'cartEntityId'>) {
const t = await getTranslations('Cart.Errors');

const customerAccessToken = await getSessionCustomerAccessToken();

try {
const cookieStore = await cookies();
const cartId = cookieStore.get('cartId')?.value;

if (!cartId) {
return { status: 'error', error: 'No cartId cookie found' };
throw new Error(t('cartNotFound'));
}

if (!lineItemEntityId) {
return { status: 'error', error: 'No lineItemEntityId found' };
throw new Error(t('lineItemNotFound'));
}

const response = await client.fetch({
Expand All @@ -63,12 +66,12 @@ export async function removeItem({

expireTag(TAGS.cart);

return { status: 'success', data: cart };
return cart;
} catch (error: unknown) {
if (error instanceof Error) {
return { status: 'error', error: error.message };
throw new Error(error.message);
}

return { status: 'error' };
throw new Error(t('somethingWentWrong'));
}
}
Loading

0 comments on commit 6392cb6

Please sign in to comment.