Skip to content

Commit

Permalink
chore: standardize actions (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemoya committed Apr 30, 2024
1 parent 24a5c4a commit 8349bbf
Show file tree
Hide file tree
Showing 24 changed files with 162 additions and 105 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-crabs-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

chore: standardize actions
17 changes: 8 additions & 9 deletions apps/core/app/[locale]/(default)/cart/_actions/remove-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,21 @@ export async function removeItem({
return { status: 'error', error: 'No lineItemEntityId found' };
}

const updatedCart = await deleteCartLineItem(cartId, lineItemEntityId);
const cart = await deleteCartLineItem(cartId, lineItemEntityId);

// If we remove the last item in a cart the cart is deleted
// so we need to remove the cartId cookie and clear shipping data
if (!updatedCart) {
// so we need to remove the cartId cookie
// TODO: We need to figure out if it actually failed.
if (!cart) {
cookies().delete('cartId');
cookies().delete('shippingCosts');
revalidateTag('cart');
}

revalidateTag('cart');

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

return { status: 'error' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ export async function applyCouponCode(formData: FormData) {
const checkout = await applyCheckoutCoupon(parsedData.checkoutEntityId, parsedData.couponCode);

if (!checkout?.entityId) {
return { status: 'error', error: 'Coupon code is invalid' };
return { status: 'error', error: 'Coupon code is invalid.' };
}

revalidateTag('checkout');

return { status: 'success', data: checkout };
} catch (e: unknown) {
if (e instanceof Error || e instanceof z.ZodError) {
return { status: 'error', error: e.message };
} catch (error: unknown) {
if (error instanceof Error || error instanceof z.ZodError) {
return { status: 'error', error: error.message };
}

return { status: 'error' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export async function removeCouponCode(formData: FormData) {
);

if (!checkout?.entityId) {
return { status: 'error', error: 'Error ocurred removing coupon' };
return { status: 'error', error: 'Error ocurred removing coupon.' };
}

revalidateTag('checkout');

return { status: 'success', data: checkout };
} catch (e: unknown) {
if (e instanceof Error || e instanceof z.ZodError) {
return { status: 'error', error: e.message };
} catch (error: unknown) {
if (error instanceof Error || error instanceof z.ZodError) {
return { status: 'error', error: error.message };
}

return { status: 'error' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ export async function updateItemQuantity({
selectedOptions && { selectedOptions },
);

const updatedCart = await updateCartLineItem(cartId, lineItemEntityId, {
const cart = await updateCartLineItem(cartId, lineItemEntityId, {
lineItem: cartLineItemData,
});

if (!updatedCart) {
if (!cart) {
return { status: 'error', error: 'Failed to change product quantity in Cart' };
}

revalidatePath('/cart');

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

return { status: 'error' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export async function getShippingStates(id: number) {
return { status: 'success', data: response };
} catch (error: unknown) {
if (error instanceof Error) {
return { status: 'failed', error: error.message };
return { status: 'error', error: error.message };
}

return { status: 'failed', error };
return { status: 'error', error: 'An error occurred while fetching the shipping states.' };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const ShippingInfo = ({
shippingId: shippingConsignment?.entityId ?? '',
});

if (status === 'failed') {
if (status === 'error') {
toast.error(t('errorMessage'), {
icon: <AlertCircle className="text-error-secondary" />,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ export const submitShippingInfo = async (
});
}

if (!result?.entityId) {
return { status: 'error', error: 'Failed to submit shipping info.' };
}

revalidateTag('checkout');

return { status: 'success', data: result };
} catch (e: unknown) {
if (e instanceof Error || e instanceof z.ZodError) {
return { status: 'failed', error: e.message };
} catch (error: unknown) {
if (error instanceof Error || error instanceof z.ZodError) {
return { status: 'error', error: error.message };
}

return { status: 'failed' };
return { status: 'error', error: 'Failed to submit shipping info.' };
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const ShippingOptions = ({ data, checkoutEntityId, currencyCode }: Props)
const onSubmit = async (formData: FormData) => {
const { status } = await submitShippingCosts(formData, checkoutEntityId, entityId);

if (status === 'failed') {
if (status === 'error') {
toast.error(t('errorMessage'), {
icon: <AlertCircle className="text-error-secondary" />,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ export const submitShippingCosts = async (
shippingOptionEntityId: parsedData.shippingOption,
});

if (!shippingCost?.entityId) {
return { status: 'error', error: 'Failed to submit shipping cost.' };
}

revalidateTag('checkout');

return { status: 'success', data: shippingCost };
} catch (e: unknown) {
if (e instanceof Error || e instanceof z.ZodError) {
return { status: 'failed', error: e.message };
} catch (error: unknown) {
if (error instanceof Error || error instanceof z.ZodError) {
return { status: 'error', error: error.message };
}

return { status: 'failed' };
return { status: 'error', error: 'Failed to submit shipping cost.' };
}
};
45 changes: 30 additions & 15 deletions apps/core/app/[locale]/(default)/compare/_actions/add-to-cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ export const addToCart = async (data: FormData) => {
const productEntityId = Number(data.get('product_id'));

const cartId = cookies().get('cartId')?.value;
const cart = await getCart(cartId);

let cart;

try {
cart = await getCart(cartId);

if (cart) {
await addCartLineItem(cart.entityId, {
cart = await addCartLineItem(cart.entityId, {
lineItems: [
{
productEntityId,
Expand All @@ -24,26 +27,38 @@ export const addToCart = async (data: FormData) => {
],
});

if (!cart?.entityId) {
return { status: 'error', error: 'Failed to add product to cart.' };
}

revalidateTag('cart');

return;
return { status: 'success', data: cart };
}

const newCart = await createCart([{ productEntityId, quantity: 1 }]);
cart = await createCart([{ productEntityId, quantity: 1 }]);

if (newCart) {
cookies().set({
name: 'cartId',
value: newCart.entityId,
httpOnly: true,
sameSite: 'lax',
secure: true,
path: '/',
});
if (!cart?.entityId) {
return { status: 'error', error: 'Failed to add product to cart.' };
}

cookies().set({
name: 'cartId',
value: cart.entityId,
httpOnly: true,
sameSite: 'lax',
secure: true,
path: '/',
});

revalidateTag('cart');
} catch (e) {
return { error: 'Something went wrong. Please try again.' };

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

return { status: 'error', error: 'Something went wrong. Please try again.' };
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const AddToCartForm = ({
const result = await addToCart(formData);
const quantity = Number(formData.get('quantity'));

if (result?.error) {
if (result.error) {
toast.error(result.error, { icon: <AlertCircle className="text-error-secondary" /> });

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import {
submitChangePassword,
} from '~/client/mutations/submit-change-password';

export interface State {
status: 'idle' | 'error' | 'success';
message?: string;
}

export const submitChangePasswordForm = async (_previousState: unknown, formData: FormData) => {
try {
const parsedData = ChangePasswordSchema.parse({
Expand Down Expand Up @@ -52,6 +47,6 @@ export const submitChangePasswordForm = async (_previousState: unknown, formData
};
}

return { status: 'error', message: 'Unknown error' };
return { status: 'error', message: 'Unknown error.' };
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { isRedirectError } from 'next/dist/client/components/redirect';

import { Credentials, signIn } from '~/auth';

export type State = { status: 'idle' } | { status: 'failed' };

export const submitLoginForm = async (_previousState: unknown, formData: FormData) => {
try {
const credentials = Credentials.parse({
Expand All @@ -25,7 +23,7 @@ export const submitLoginForm = async (_previousState: unknown, formData: FormDat
}

return {
status: 'failed',
status: 'error',
};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ export const submitResetPasswordForm = async ({
return { status: 'error', error: error.message };
}

return { status: 'error', error: 'Unknown error' };
return { status: 'error', error: 'Unknown error.' };
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const LoginForm = () => {

const t = useTranslations('Account.Login');

const isFormInvalid = state?.status === 'failed';
const isFormInvalid = state?.status === 'error';

const handleInputValidation = (e: ChangeEvent<HTMLInputElement>) => {
const validationStatus = e.target.validity.valueMissing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ export const submitContactForm = async ({
}

return {
status: 'failed',
status: 'error',
error: response.errors.map((error) => error.message).join('\n'),
};
} catch (e: unknown) {
if (e instanceof Error || e instanceof z.ZodError) {
return { status: 'failed', error: e.message };
} catch (error: unknown) {
if (error instanceof Error || error instanceof z.ZodError) {
return { status: 'error', error: error.message };
}

return { status: 'failed', error: 'Unknown error' };
return { status: 'error', error: 'Unknown error.' };
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const ContactUs = ({ data }: Props) => {
});
}

if (submit.status === 'failed') {
if (submit.status === 'error') {
setFormStatus({ status: 'error', message: submit.error ?? '' });
}

Expand Down
Loading

0 comments on commit 8349bbf

Please sign in to comment.