update cart discount for prod build, add loading toast

This commit is contained in:
2025-09-10 06:32:55 +03:00
parent 229b3d7c27
commit 2aad0329f3
5 changed files with 70 additions and 42 deletions

View File

@@ -0,0 +1,24 @@
"use server"
import { applyPromotions } from "@lib/data/cart"
export async function addPromotionCodeAction(code: string) {
try {
await applyPromotions([code]);
return { success: true, message: 'Discount code applied successfully' };
} catch (error) {
console.error('Error applying promotion code:', error);
return { success: false, message: 'Failed to apply discount code' };
}
}
export async function removePromotionCodeAction(codeToRemove: string, appliedCodes: string[]) {
try {
const updatedCodes = appliedCodes.filter((appliedCode) => appliedCode !== codeToRemove);
await applyPromotions(updatedCodes);
return { success: true, message: 'Discount code removed successfully' };
} catch (error) {
console.error('Error removing promotion code:', error);
return { success: false, message: 'Failed to remove discount code' };
}
}

View File

@@ -2,9 +2,8 @@
import { Badge, Text } from "@medusajs/ui"
import { toast } from '@kit/ui/sonner';
import React, { useActionState } from "react";
import React from "react";
import { applyPromotions, submitPromotionForm } from "@lib/data/cart"
import { convertToLocale } from "@lib/util/money"
import { StoreCart, StorePromotion } from "@medusajs/types"
import Trash from "@modules/common/icons/trash"
@@ -16,6 +15,7 @@ import { useTranslation } from "react-i18next";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { addPromotionCodeAction, removePromotionCodeAction } from "./discount-code-actions";
const DiscountCodeSchema = z.object({
code: z.string().min(1),
@@ -31,42 +31,35 @@ export default function DiscountCode({ cart }: {
const { promotions = [] } = cart;
const removePromotionCode = async (code: string) => {
const validPromotions = promotions.filter(
(promotion) => promotion.code !== code,
)
const appliedCodes = promotions
.filter((p) => p.code !== undefined)
.map((p) => p.code!)
await applyPromotions(
validPromotions.filter((p) => p.code === undefined).map((p) => p.code!),
{
onSuccess: () => {
const loading = toast.loading(t('cart:discountCode.removeLoading'));
const result = await removePromotionCodeAction(code, appliedCodes)
toast.dismiss(loading);
if (result.success) {
toast.success(t('cart:discountCode.removeSuccess'));
},
onError: () => {
} else {
toast.error(t('cart:discountCode.removeError'));
},
}
)
}
const addPromotionCode = async (code: string) => {
const codes = promotions
.filter((p) => p.code === undefined)
.map((p) => p.code!)
codes.push(code.toString())
const loading = toast.loading(t('cart:discountCode.addLoading'));
const result = await addPromotionCodeAction(code)
await applyPromotions(codes, {
onSuccess: () => {
toast.dismiss(loading);
if (result.success) {
toast.success(t('cart:discountCode.addSuccess'));
},
onError: () => {
toast.error(t('cart:discountCode.addError'));
},
});
form.reset()
} else {
toast.error(t('cart:discountCode.addError'));
}
}
const [message, formAction] = useActionState(submitPromotionForm, null)
const form = useForm<z.infer<typeof DiscountCodeSchema>>({
defaultValues: {
@@ -135,7 +128,7 @@ export default function DiscountCode({ cart }: {
"percentage"
? `${promotion.application_method.value}%`
: convertToLocale({
amount: promotion.application_method.value,
amount: Number(promotion.application_method.value),
currency_code:
promotion.application_method
.currency_code,

View File

@@ -25,13 +25,19 @@
"timeoutAction": "Continue"
},
"discountCode": {
"title": "Gift card or promotion code",
"label": "Add Promotion Code(s)",
"title": "Gift card or promo code",
"label": "Add Promo Code(s)",
"apply": "Apply",
"subtitle": "If you wish, you can add a promotion code",
"placeholder": "Enter promotion code",
"remove": "Remove promotion code",
"appliedCodes": "Promotion(s) applied:"
"subtitle": "If you wish, you can add a promo code",
"placeholder": "Enter promo code",
"remove": "Remove promo code",
"appliedCodes": "Promotions(s) applied:",
"removeError": "Failed to remove promo code",
"removeSuccess": "Promo code removed",
"removeLoading": "Removing promo code...",
"addError": "Failed to add promo code",
"addSuccess": "Promo code added",
"addLoading": "Setting promo code..."
},
"items": {
"synlabAnalyses": {

View File

@@ -4,8 +4,8 @@
"emptyCartMessage": "Sinu ostukorv on tühi",
"emptyCartMessageDescription": "Lisa tooteid ostukorvi, et jätkata.",
"subtotal": "Vahesumma",
"promotionsTotal": "Soodustuse summa",
"total": "Summa",
"promotionsTotal": "Soodustuse summa",
"table": {
"item": "Toode",
"quantity": "Kogus",
@@ -34,8 +34,10 @@
"appliedCodes": "Rakendatud sooduskoodid:",
"removeError": "Sooduskoodi eemaldamine ebaõnnestus",
"removeSuccess": "Sooduskood eemaldatud",
"removeLoading": "Sooduskoodi eemaldamine",
"addError": "Sooduskoodi rakendamine ebaõnnestus",
"addSuccess": "Sooduskood rakendatud"
"addSuccess": "Sooduskood rakendatud",
"addLoading": "Rakendan sooduskoodi..."
},
"items": {
"synlabAnalyses": {

View File

@@ -5,6 +5,7 @@
"emptyCartMessageDescription": "Добавьте товары в корзину, чтобы продолжить.",
"subtotal": "Промежуточный итог",
"total": "Сумма",
"promotionsTotal": "Скидка",
"table": {
"item": "Товар",
"quantity": "Количество",
@@ -33,8 +34,10 @@
"appliedCodes": "Примененные промокоды:",
"removeError": "Не удалось удалить промокод",
"removeSuccess": "Промокод удален",
"removeLoading": "Удаление промокода...",
"addError": "Не удалось применить промокод",
"addSuccess": "Промокод применен"
"addSuccess": "Промокод применен",
"addLoading": "Применение промокода..."
},
"items": {
"synlabAnalyses": {