update cart discount for prod build, add loading toast
This commit is contained in:
24
app/home/(user)/_components/cart/discount-code-actions.ts
Normal file
24
app/home/(user)/_components/cart/discount-code-actions.ts
Normal 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' };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,9 +2,8 @@
|
|||||||
|
|
||||||
import { Badge, Text } from "@medusajs/ui"
|
import { Badge, Text } from "@medusajs/ui"
|
||||||
import { toast } from '@kit/ui/sonner';
|
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 { convertToLocale } from "@lib/util/money"
|
||||||
import { StoreCart, StorePromotion } from "@medusajs/types"
|
import { StoreCart, StorePromotion } from "@medusajs/types"
|
||||||
import Trash from "@modules/common/icons/trash"
|
import Trash from "@modules/common/icons/trash"
|
||||||
@@ -16,6 +15,7 @@ import { useTranslation } from "react-i18next";
|
|||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import { addPromotionCodeAction, removePromotionCodeAction } from "./discount-code-actions";
|
||||||
|
|
||||||
const DiscountCodeSchema = z.object({
|
const DiscountCodeSchema = z.object({
|
||||||
code: z.string().min(1),
|
code: z.string().min(1),
|
||||||
@@ -31,42 +31,35 @@ export default function DiscountCode({ cart }: {
|
|||||||
const { promotions = [] } = cart;
|
const { promotions = [] } = cart;
|
||||||
|
|
||||||
const removePromotionCode = async (code: string) => {
|
const removePromotionCode = async (code: string) => {
|
||||||
const validPromotions = promotions.filter(
|
const appliedCodes = promotions
|
||||||
(promotion) => promotion.code !== code,
|
.filter((p) => p.code !== undefined)
|
||||||
)
|
.map((p) => p.code!)
|
||||||
|
|
||||||
await applyPromotions(
|
const loading = toast.loading(t('cart:discountCode.removeLoading'));
|
||||||
validPromotions.filter((p) => p.code === undefined).map((p) => p.code!),
|
|
||||||
{
|
const result = await removePromotionCodeAction(code, appliedCodes)
|
||||||
onSuccess: () => {
|
|
||||||
|
toast.dismiss(loading);
|
||||||
|
if (result.success) {
|
||||||
toast.success(t('cart:discountCode.removeSuccess'));
|
toast.success(t('cart:discountCode.removeSuccess'));
|
||||||
},
|
} else {
|
||||||
onError: () => {
|
|
||||||
toast.error(t('cart:discountCode.removeError'));
|
toast.error(t('cart:discountCode.removeError'));
|
||||||
},
|
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const addPromotionCode = async (code: string) => {
|
const addPromotionCode = async (code: string) => {
|
||||||
const codes = promotions
|
const loading = toast.loading(t('cart:discountCode.addLoading'));
|
||||||
.filter((p) => p.code === undefined)
|
const result = await addPromotionCodeAction(code)
|
||||||
.map((p) => p.code!)
|
|
||||||
codes.push(code.toString())
|
|
||||||
|
|
||||||
await applyPromotions(codes, {
|
toast.dismiss(loading);
|
||||||
onSuccess: () => {
|
if (result.success) {
|
||||||
toast.success(t('cart:discountCode.addSuccess'));
|
toast.success(t('cart:discountCode.addSuccess'));
|
||||||
},
|
|
||||||
onError: () => {
|
|
||||||
toast.error(t('cart:discountCode.addError'));
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
form.reset()
|
form.reset()
|
||||||
|
} else {
|
||||||
|
toast.error(t('cart:discountCode.addError'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const [message, formAction] = useActionState(submitPromotionForm, null)
|
|
||||||
|
|
||||||
const form = useForm<z.infer<typeof DiscountCodeSchema>>({
|
const form = useForm<z.infer<typeof DiscountCodeSchema>>({
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
@@ -135,7 +128,7 @@ export default function DiscountCode({ cart }: {
|
|||||||
"percentage"
|
"percentage"
|
||||||
? `${promotion.application_method.value}%`
|
? `${promotion.application_method.value}%`
|
||||||
: convertToLocale({
|
: convertToLocale({
|
||||||
amount: promotion.application_method.value,
|
amount: Number(promotion.application_method.value),
|
||||||
currency_code:
|
currency_code:
|
||||||
promotion.application_method
|
promotion.application_method
|
||||||
.currency_code,
|
.currency_code,
|
||||||
|
|||||||
@@ -25,13 +25,19 @@
|
|||||||
"timeoutAction": "Continue"
|
"timeoutAction": "Continue"
|
||||||
},
|
},
|
||||||
"discountCode": {
|
"discountCode": {
|
||||||
"title": "Gift card or promotion code",
|
"title": "Gift card or promo code",
|
||||||
"label": "Add Promotion Code(s)",
|
"label": "Add Promo Code(s)",
|
||||||
"apply": "Apply",
|
"apply": "Apply",
|
||||||
"subtitle": "If you wish, you can add a promotion code",
|
"subtitle": "If you wish, you can add a promo code",
|
||||||
"placeholder": "Enter promotion code",
|
"placeholder": "Enter promo code",
|
||||||
"remove": "Remove promotion code",
|
"remove": "Remove promo code",
|
||||||
"appliedCodes": "Promotion(s) applied:"
|
"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": {
|
"items": {
|
||||||
"synlabAnalyses": {
|
"synlabAnalyses": {
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
"emptyCartMessage": "Sinu ostukorv on tühi",
|
"emptyCartMessage": "Sinu ostukorv on tühi",
|
||||||
"emptyCartMessageDescription": "Lisa tooteid ostukorvi, et jätkata.",
|
"emptyCartMessageDescription": "Lisa tooteid ostukorvi, et jätkata.",
|
||||||
"subtotal": "Vahesumma",
|
"subtotal": "Vahesumma",
|
||||||
"promotionsTotal": "Soodustuse summa",
|
|
||||||
"total": "Summa",
|
"total": "Summa",
|
||||||
|
"promotionsTotal": "Soodustuse summa",
|
||||||
"table": {
|
"table": {
|
||||||
"item": "Toode",
|
"item": "Toode",
|
||||||
"quantity": "Kogus",
|
"quantity": "Kogus",
|
||||||
@@ -34,8 +34,10 @@
|
|||||||
"appliedCodes": "Rakendatud sooduskoodid:",
|
"appliedCodes": "Rakendatud sooduskoodid:",
|
||||||
"removeError": "Sooduskoodi eemaldamine ebaõnnestus",
|
"removeError": "Sooduskoodi eemaldamine ebaõnnestus",
|
||||||
"removeSuccess": "Sooduskood eemaldatud",
|
"removeSuccess": "Sooduskood eemaldatud",
|
||||||
|
"removeLoading": "Sooduskoodi eemaldamine",
|
||||||
"addError": "Sooduskoodi rakendamine ebaõnnestus",
|
"addError": "Sooduskoodi rakendamine ebaõnnestus",
|
||||||
"addSuccess": "Sooduskood rakendatud"
|
"addSuccess": "Sooduskood rakendatud",
|
||||||
|
"addLoading": "Rakendan sooduskoodi..."
|
||||||
},
|
},
|
||||||
"items": {
|
"items": {
|
||||||
"synlabAnalyses": {
|
"synlabAnalyses": {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
"emptyCartMessageDescription": "Добавьте товары в корзину, чтобы продолжить.",
|
"emptyCartMessageDescription": "Добавьте товары в корзину, чтобы продолжить.",
|
||||||
"subtotal": "Промежуточный итог",
|
"subtotal": "Промежуточный итог",
|
||||||
"total": "Сумма",
|
"total": "Сумма",
|
||||||
|
"promotionsTotal": "Скидка",
|
||||||
"table": {
|
"table": {
|
||||||
"item": "Товар",
|
"item": "Товар",
|
||||||
"quantity": "Количество",
|
"quantity": "Количество",
|
||||||
@@ -33,8 +34,10 @@
|
|||||||
"appliedCodes": "Примененные промокоды:",
|
"appliedCodes": "Примененные промокоды:",
|
||||||
"removeError": "Не удалось удалить промокод",
|
"removeError": "Не удалось удалить промокод",
|
||||||
"removeSuccess": "Промокод удален",
|
"removeSuccess": "Промокод удален",
|
||||||
|
"removeLoading": "Удаление промокода...",
|
||||||
"addError": "Не удалось применить промокод",
|
"addError": "Не удалось применить промокод",
|
||||||
"addSuccess": "Промокод применен"
|
"addSuccess": "Промокод применен",
|
||||||
|
"addLoading": "Применение промокода..."
|
||||||
},
|
},
|
||||||
"items": {
|
"items": {
|
||||||
"synlabAnalyses": {
|
"synlabAnalyses": {
|
||||||
|
|||||||
Reference in New Issue
Block a user