Skip to content

Commit c6e38a6

Browse files
authored
chore: reorganize and cleanup files (#2408)
1 parent dd42b25 commit c6e38a6

11 files changed

Lines changed: 60 additions & 170 deletions

File tree

.changeset/sour-jeans-show.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
"@bigcommerce/catalyst-core": minor
3+
---
4+
5+
Reorganize and cleanup files:
6+
- Moved `core/context/search-context` to `core/lib/search`.
7+
- Moved `core/client/mutations/add-cart-line-item.ts` and `core/client/mutations/create-cart.ts` into `core/lib/cart/*`.
8+
- Removed `core/client/queries/get-cart.ts` in favor of a smaller, more focused query within `core/lib/cart/validate-cart.ts`.
9+
10+
### Migration
11+
- Replace imports from `~/context/search-context` to `~/lib/search`.
12+
- Replace imports from `~/client/mutations/` to `~/lib/cart/`.
13+
- Remove any direct imports from `~/client/queries/get-cart.ts` and use the new `validate-cart.ts` query instead. If you need the previous `getCart` function, you can copy it from the old file and adapt it to your needs.

core/app/providers.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { PropsWithChildren } from 'react';
44

55
import { Toaster } from '@/vibes/soul/primitives/toaster';
6-
import { SearchProvider } from '~/context/search-context';
6+
import { SearchProvider } from '~/lib/search';
77

88
export function Providers({ children }: PropsWithChildren) {
99
return (

core/client/queries/get-cart.ts

Lines changed: 0 additions & 157 deletions
This file was deleted.

core/client/mutations/add-cart-line-item.ts renamed to core/lib/cart/add-cart-line-item.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { getSessionCustomerAccessToken } from '~/auth';
2-
3-
import { client } from '..';
4-
import { graphql, VariablesOf } from '../graphql';
2+
import { client } from '~/client';
3+
import { graphql, VariablesOf } from '~/client/graphql';
54

65
const AddCartLineItemMutation = graphql(`
76
mutation AddCartLineItemMutation($input: AddCartLineItemsInput!) {
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { getSessionCustomerAccessToken } from '~/auth';
2+
import { client } from '~/client';
3+
import { graphql, VariablesOf } from '~/client/graphql';
24
import { getPreferredCurrencyCode } from '~/lib/currency';
35

4-
import { client } from '..';
5-
import { graphql, VariablesOf } from '../graphql';
6-
76
const CreateCartMutation = graphql(`
87
mutation CreateCartMutation($createCartInput: CreateCartInput!) {
98
cart {

core/lib/cart/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import { unstable_expireTag } from 'next/cache';
44

55
import { auth, getAnonymousSession, updateAnonymousSession, updateSession } from '~/auth';
6-
import { addCartLineItem, AddCartLineItemsInput } from '~/client/mutations/add-cart-line-item';
7-
import { createCart, CreateCartInput } from '~/client/mutations/create-cart';
8-
import { getCart } from '~/client/queries/get-cart';
96
import { TAGS } from '~/client/tags';
7+
import { addCartLineItem, AddCartLineItemsInput } from '~/lib/cart/add-cart-line-item';
8+
import { createCart, CreateCartInput } from '~/lib/cart/create-cart';
9+
import { validateCartId } from '~/lib/cart/validate-cart';
1010

1111
import { MissingCartError } from './error';
1212

@@ -50,7 +50,7 @@ export async function addToOrCreateCart(
5050
data: CreateCartInput | AddCartLineItemsInput['data'],
5151
): Promise<void> {
5252
const cartId = await getCartId();
53-
const cart = await getCart(cartId);
53+
const cart = await validateCartId(cartId);
5454

5555
if (cart) {
5656
const response = await addCartLineItem(cart.entityId, data);

core/lib/cart/validate-cart.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { getSessionCustomerAccessToken } from '~/auth';
2+
import { client } from '~/client';
3+
import { graphql } from '~/client/graphql';
4+
import { TAGS } from '~/client/tags';
5+
6+
const ValidateCartQuery = graphql(`
7+
query ValidateCartQuery($cartId: String) {
8+
site {
9+
cart(entityId: $cartId) {
10+
entityId
11+
}
12+
}
13+
}
14+
`);
15+
16+
export async function validateCartId(cartId?: string) {
17+
const customerAccessToken = await getSessionCustomerAccessToken();
18+
19+
const response = await client.fetch({
20+
document: ValidateCartQuery,
21+
variables: { cartId },
22+
customerAccessToken,
23+
fetchOptions: {
24+
cache: 'no-store',
25+
next: {
26+
tags: [TAGS.cart],
27+
},
28+
},
29+
});
30+
31+
return response.data.site.cart;
32+
}

core/vibes/soul/primitives/navigation/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import { Logo } from '@/vibes/soul/primitives/logo';
2727
import { Price } from '@/vibes/soul/primitives/price-label';
2828
import { ProductCard } from '@/vibes/soul/primitives/product-card';
2929
import { Link } from '~/components/link';
30-
import { useSearch } from '~/context/search-context';
3130
import { usePathname, useRouter } from '~/i18n/routing';
31+
import { useSearch } from '~/lib/search';
3232

3333
interface Link {
3434
label: string;

core/vibes/soul/sections/not-found/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { Button } from '@/vibes/soul/primitives/button';
44
import { SectionLayout } from '@/vibes/soul/sections/section-layout';
5-
import { useSearch } from '~/context/search-context';
5+
import { useSearch } from '~/lib/search';
66

77
export interface NotFoundProps {
88
title?: string;

0 commit comments

Comments
 (0)