-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(sa): updates to algolia starter (#91)
- Loading branch information
Showing
182 changed files
with
154,568 additions
and
8,919 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,117 @@ | ||
"use server" | ||
|
||
import { revalidateTag, unstable_cache } from "next/cache" | ||
import { revalidateTag } from "next/cache" | ||
import { cookies } from "next/headers" | ||
import { storefrontClient } from "clients/storefront" | ||
import { COOKIE_CART_ID, TAGS } from "constants/index" | ||
import { isDemoMode } from "utils/demo-utils" | ||
import { createCart, createCartItem, deleteCartItem, getCart, getProduct, updateCartItem } from "lib/shopify" | ||
|
||
export const getCart = unstable_cache(async (cartId: string) => storefrontClient.getCart(cartId), [TAGS.CART], { revalidate: 60 * 15, tags: [TAGS.CART] }) | ||
export async function getOrCreateCart() { | ||
const cartId = cookies().get(COOKIE_CART_ID)?.value | ||
const cart = cartId ? await getCart(cartId) : await createCart() | ||
|
||
export async function addCartItem(prevState: any, variantId: string) { | ||
if (isDemoMode()) return { ok: false, message: "Demo mode active. Filtering, searching, and adding to cart disabled." } | ||
if (!variantId) return { ok: false } | ||
if (!cartId) { | ||
const newCartId = cart?.id | ||
if (newCartId) { | ||
cookies().set(COOKIE_CART_ID, newCartId) | ||
revalidateTag(TAGS.CART) | ||
} | ||
} | ||
|
||
let cartId = cookies().get(COOKIE_CART_ID)?.value | ||
let cart | ||
return { cartId: cart?.id, cart } | ||
} | ||
|
||
if (cartId) cart = await storefrontClient.getCart(cartId) | ||
export async function getItemAvailability({ | ||
cartId, | ||
variantId, | ||
productId, | ||
}: { | ||
cartId: string | null | undefined | ||
variantId: string | null | undefined | ||
productId: string | null | undefined | ||
}) { | ||
if (!variantId) { | ||
return { inCartQuantity: 0, inStockQuantity: 0 } | ||
} | ||
|
||
if (!cartId || !cart) { | ||
cart = await storefrontClient.createCart([]) | ||
cartId = cart?.id | ||
cartId && cookies().set(COOKIE_CART_ID, cartId) | ||
if (!cartId) { | ||
const product = await getProduct(productId!) | ||
const inStockQuantity = product?.variants?.find((variant) => variant.id === variantId)?.quantityAvailable ?? Infinity | ||
return { | ||
inCartQuantity: 0, | ||
inStockQuantity, | ||
} | ||
} | ||
|
||
revalidateTag(TAGS.CART) | ||
const cart = await getCart(cartId) | ||
const cartItem = cart?.items?.find((item) => item.merchandise.id === variantId) | ||
|
||
return { | ||
inCartQuantity: cartItem?.quantity ?? 0, | ||
inStockQuantity: cartItem?.merchandise.quantityAvailable ?? Infinity, | ||
} | ||
} | ||
|
||
const itemAvailability = await getItemAvailability(cartId, variantId) | ||
export async function addCartItem(prevState: any, variantId: string, productId: string) { | ||
if (isDemoMode()) { | ||
return { | ||
ok: false, | ||
message: "Demo mode active. Filtering, searching, and adding to cart disabled.", | ||
} | ||
} | ||
|
||
if (!itemAvailability || itemAvailability.inCartQuantity >= itemAvailability.inStockQuantity) | ||
if (!variantId) return { ok: false } | ||
|
||
const { cartId } = await getOrCreateCart() | ||
|
||
if (!cartId) return { ok: false } | ||
|
||
const availability = await getItemAvailability({ cartId, variantId, productId }) | ||
if (!availability || availability.inCartQuantity >= availability.inStockQuantity) { | ||
return { | ||
ok: false, | ||
message: "This product is out of stock", | ||
} | ||
} | ||
|
||
await storefrontClient.createCartItem(cartId!, [{ merchandiseId: variantId, quantity: 1 }]) | ||
await createCartItem(cartId, [{ merchandiseId: variantId, quantity: 1 }]) | ||
revalidateTag(TAGS.CART) | ||
|
||
return { ok: true } | ||
} | ||
|
||
export async function getItemAvailability(cartId: string | null | undefined, variantId: string | null | undefined) { | ||
if (!cartId || !variantId) return { inCartQuantity: 0, inStockQuantity: Infinity } | ||
|
||
const cart = await storefrontClient.getCart(cartId) | ||
const cartItem = cart?.items?.find((item) => item.merchandise.id === variantId) | ||
|
||
return { inCartQuantity: cartItem?.quantity ?? 0, inStockQuantity: cartItem?.merchandise.quantityAvailable ?? Infinity } | ||
} | ||
|
||
export async function removeCartItem(prevState: any, itemId: string) { | ||
const cartId = cookies().get(COOKIE_CART_ID)?.value | ||
|
||
if (!cartId) return { ok: false } | ||
|
||
await storefrontClient.deleteCartItem(cartId!, [itemId]) | ||
await deleteCartItem(cartId, [itemId]) | ||
revalidateTag(TAGS.CART) | ||
|
||
return { ok: true } | ||
} | ||
|
||
export async function updateItemQuantity(prevState: any, payload: { itemId: string; variantId: string; quantity: number }) { | ||
export async function updateItemQuantity(prevState: any, payload: { itemId: string; variantId: string; quantity: number; productId: string }) { | ||
const cartId = cookies().get(COOKIE_CART_ID)?.value | ||
|
||
if (!cartId) return { ok: false } | ||
|
||
const { itemId, variantId, quantity } = payload | ||
const { itemId, variantId, quantity, productId } = payload | ||
|
||
if (quantity === 0) { | ||
await storefrontClient.deleteCartItem(cartId, [itemId]) | ||
await deleteCartItem(cartId, [itemId]) | ||
revalidateTag(TAGS.CART) | ||
return { ok: true } | ||
} | ||
|
||
const itemAvailability = await getItemAvailability(cartId, variantId) | ||
if (!itemAvailability || quantity > itemAvailability.inStockQuantity) | ||
const itemAvailability = await getItemAvailability({ cartId, variantId, productId }) | ||
if (!itemAvailability || quantity > itemAvailability.inStockQuantity) { | ||
return { | ||
ok: false, | ||
message: "This product is out of stock", | ||
} | ||
} | ||
|
||
await storefrontClient.updateCartItem(cartId, [{ id: itemId, merchandiseId: variantId, quantity }]) | ||
|
||
await updateCartItem(cartId, [{ id: itemId, merchandiseId: variantId, quantity }]) | ||
revalidateTag(TAGS.CART) | ||
|
||
return { ok: true } | ||
} |
26 changes: 0 additions & 26 deletions
26
starters/shopify-algolia/app/actions/collection.actions.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.