forked from vercel/commerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaces Route Handlers with Server Actions (vercel#1050)
- Loading branch information
Showing
9 changed files
with
742 additions
and
753 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use server'; | ||
|
||
import { addToCart, removeFromCart, updateCart } from 'lib/shopify'; | ||
import { cookies } from 'next/headers'; | ||
|
||
export const addItem = async (variantId: string | undefined): Promise<Error | undefined> => { | ||
const cartId = cookies().get('cartId')?.value; | ||
|
||
if (!cartId || !variantId) { | ||
return new Error('Missing cartId or variantId'); | ||
} | ||
try { | ||
await addToCart(cartId, [{ merchandiseId: variantId, quantity: 1 }]); | ||
} catch (e) { | ||
return new Error('Error adding item', { cause: e }); | ||
} | ||
}; | ||
|
||
export const removeItem = async (lineId: string): Promise<Error | undefined> => { | ||
const cartId = cookies().get('cartId')?.value; | ||
|
||
if (!cartId) { | ||
return new Error('Missing cartId'); | ||
} | ||
try { | ||
await removeFromCart(cartId, [lineId]); | ||
} catch (e) { | ||
return new Error('Error removing item', { cause: e }); | ||
} | ||
}; | ||
|
||
export const updateItemQuantity = async ({ | ||
lineId, | ||
variantId, | ||
quantity | ||
}: { | ||
lineId: string; | ||
variantId: string; | ||
quantity: number; | ||
}): Promise<Error | undefined> => { | ||
const cartId = cookies().get('cartId')?.value; | ||
|
||
if (!cartId) { | ||
return new Error('Missing cartId'); | ||
} | ||
try { | ||
await updateCart(cartId, [ | ||
{ | ||
id: lineId, | ||
merchandiseId: variantId, | ||
quantity | ||
} | ||
]); | ||
} catch (e) { | ||
return new Error('Error updating item quantity', { cause: e }); | ||
} | ||
}; |
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
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
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.