prettier fix
This commit is contained in:
@@ -1,30 +1,37 @@
|
||||
"use client"
|
||||
'use client';
|
||||
|
||||
import { Badge, Text } from "@medusajs/ui"
|
||||
import { toast } from '@kit/ui/sonner';
|
||||
import React from "react";
|
||||
import React from 'react';
|
||||
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { convertToLocale } from '@lib/util/money';
|
||||
import { StoreCart, StorePromotion } from '@medusajs/types';
|
||||
import { Badge, Text } from '@medusajs/ui';
|
||||
import Trash from '@modules/common/icons/trash';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { convertToLocale } from "@lib/util/money"
|
||||
import { StoreCart, StorePromotion } from "@medusajs/types"
|
||||
import Trash from "@modules/common/icons/trash"
|
||||
import { Button } from '@kit/ui/button';
|
||||
import { Form, FormControl, FormField, FormItem } from "@kit/ui/form";
|
||||
import { Form, FormControl, FormField, FormItem } from '@kit/ui/form';
|
||||
import { Input } from '@kit/ui/input';
|
||||
import { toast } from '@kit/ui/sonner';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
import { Input } from "@kit/ui/input";
|
||||
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";
|
||||
|
||||
import {
|
||||
addPromotionCodeAction,
|
||||
removePromotionCodeAction,
|
||||
} from './discount-code-actions';
|
||||
|
||||
const DiscountCodeSchema = z.object({
|
||||
code: z.string().min(1),
|
||||
})
|
||||
});
|
||||
|
||||
export default function DiscountCode({ cart }: {
|
||||
export default function DiscountCode({
|
||||
cart,
|
||||
}: {
|
||||
cart: StoreCart & {
|
||||
promotions: StorePromotion[]
|
||||
}
|
||||
promotions: StorePromotion[];
|
||||
};
|
||||
}) {
|
||||
const { t } = useTranslation('cart');
|
||||
|
||||
@@ -33,11 +40,11 @@ export default function DiscountCode({ cart }: {
|
||||
const removePromotionCode = async (code: string) => {
|
||||
const appliedCodes = promotions
|
||||
.filter((p) => p.code !== undefined)
|
||||
.map((p) => p.code!)
|
||||
.map((p) => p.code!);
|
||||
|
||||
const loading = toast.loading(t('cart:discountCode.removeLoading'));
|
||||
|
||||
const result = await removePromotionCodeAction(code, appliedCodes)
|
||||
const result = await removePromotionCodeAction(code, appliedCodes);
|
||||
|
||||
toast.dismiss(loading);
|
||||
if (result.success) {
|
||||
@@ -45,21 +52,20 @@ export default function DiscountCode({ cart }: {
|
||||
} else {
|
||||
toast.error(t('cart:discountCode.removeError'));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const addPromotionCode = async (code: string) => {
|
||||
const loading = toast.loading(t('cart:discountCode.addLoading'));
|
||||
const result = await addPromotionCodeAction(code)
|
||||
const result = await addPromotionCodeAction(code);
|
||||
|
||||
toast.dismiss(loading);
|
||||
if (result.success) {
|
||||
toast.success(t('cart:discountCode.addSuccess'));
|
||||
form.reset()
|
||||
form.reset();
|
||||
} else {
|
||||
toast.error(t('cart:discountCode.addError'));
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
const form = useForm<z.infer<typeof DiscountCodeSchema>>({
|
||||
defaultValues: {
|
||||
@@ -69,40 +75,41 @@ export default function DiscountCode({ cart }: {
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="w-full h-full bg-white flex flex-col txt-medium gap-y-4">
|
||||
<p className="text-sm text-muted-foreground">
|
||||
<div className="txt-medium flex h-full w-full flex-col gap-y-4 bg-white">
|
||||
<p className="text-muted-foreground text-sm">
|
||||
<Trans i18nKey={'cart:discountCode.subtitle'} />
|
||||
</p>
|
||||
|
||||
<Form {...form}>
|
||||
<form
|
||||
onSubmit={form.handleSubmit((data) => addPromotionCode(data.code))}
|
||||
className="w-full mb-2 flex gap-x-2 sm:flex-row flex-col gap-y-2 flex-1"
|
||||
className="mb-2 flex w-full flex-1 flex-col gap-x-2 gap-y-2 sm:flex-row"
|
||||
>
|
||||
<FormField
|
||||
name={'code'}
|
||||
render={({ field }) => (
|
||||
<FormItem className="flex-1">
|
||||
<FormControl>
|
||||
<Input required type="text" {...field} placeholder={t('cart:discountCode.placeholder')} />
|
||||
<Input
|
||||
required
|
||||
type="text"
|
||||
{...field}
|
||||
placeholder={t('cart:discountCode.placeholder')}
|
||||
/>
|
||||
</FormControl>
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Button
|
||||
type="submit"
|
||||
variant="secondary"
|
||||
className="h-min"
|
||||
>
|
||||
<Button type="submit" variant="secondary" className="h-min">
|
||||
<Trans i18nKey={'cart:discountCode.apply'} />
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
|
||||
{promotions.length > 0 && (
|
||||
<div className="w-full flex items-center mt-4">
|
||||
<div className="flex flex-col w-full gap-y-2">
|
||||
<div className="mt-4 flex w-full items-center">
|
||||
<div className="flex w-full flex-col gap-y-2">
|
||||
<p>
|
||||
<Trans i18nKey={'cart:discountCode.appliedCodes'} />
|
||||
</p>
|
||||
@@ -111,32 +118,32 @@ export default function DiscountCode({ cart }: {
|
||||
return (
|
||||
<div
|
||||
key={promotion.id}
|
||||
className="flex items-center justify-between w-full max-w-full mb-2"
|
||||
className="mb-2 flex w-full max-w-full items-center justify-between"
|
||||
data-testid="discount-row"
|
||||
>
|
||||
<Text className="flex gap-x-1 items-baseline text-sm w-4/5 pr-1">
|
||||
<Text className="flex w-4/5 items-baseline gap-x-1 pr-1 text-sm">
|
||||
<span className="truncate" data-testid="discount-code">
|
||||
<Badge
|
||||
color={promotion.is_automatic ? "green" : "grey"}
|
||||
color={promotion.is_automatic ? 'green' : 'grey'}
|
||||
size="small"
|
||||
className="px-4 text-sm"
|
||||
>
|
||||
{promotion.code}
|
||||
</Badge>{" "}
|
||||
</Badge>{' '}
|
||||
(
|
||||
{promotion.application_method?.value !== undefined &&
|
||||
promotion.application_method.currency_code !==
|
||||
undefined && (
|
||||
undefined && (
|
||||
<>
|
||||
{promotion.application_method.type ===
|
||||
"percentage"
|
||||
{promotion.application_method.type === 'percentage'
|
||||
? `${promotion.application_method.value}%`
|
||||
: convertToLocale({
|
||||
amount: Number(promotion.application_method.value),
|
||||
currency_code:
|
||||
promotion.application_method
|
||||
.currency_code,
|
||||
})}
|
||||
amount: Number(
|
||||
promotion.application_method.value,
|
||||
),
|
||||
currency_code:
|
||||
promotion.application_method.currency_code,
|
||||
})}
|
||||
</>
|
||||
)}
|
||||
)
|
||||
@@ -152,10 +159,10 @@ export default function DiscountCode({ cart }: {
|
||||
className="flex items-center"
|
||||
onClick={() => {
|
||||
if (!promotion.code) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
removePromotionCode(promotion.code)
|
||||
removePromotionCode(promotion.code);
|
||||
}}
|
||||
data-testid="remove-discount-button"
|
||||
>
|
||||
@@ -166,11 +173,11 @@ export default function DiscountCode({ cart }: {
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user