From 8ea692eda21b7d7bb988e073e5663519ab5999ae Mon Sep 17 00:00:00 2001 From: NotII <46204250+NotII@users.noreply.github.com> Date: Sat, 8 Mar 2025 05:14:50 +0000 Subject: [PATCH] cluster fuck --- .../promotions/EditPromotionForm.tsx | 116 ++++++++++-------- .../dashboard/promotions/NewPromotionForm.tsx | 113 +++++++++-------- .../dashboard/promotions/PromotionsList.tsx | 32 ++--- 3 files changed, 144 insertions(+), 117 deletions(-) diff --git a/components/dashboard/promotions/EditPromotionForm.tsx b/components/dashboard/promotions/EditPromotionForm.tsx index 83f0558..01dbfa1 100644 --- a/components/dashboard/promotions/EditPromotionForm.tsx +++ b/components/dashboard/promotions/EditPromotionForm.tsx @@ -16,13 +16,7 @@ import { FormMessage, } from '@/components/ui/form'; import { Input } from '@/components/ui/input'; -import { - Select, - SelectContent, - SelectItem, - SelectTrigger, - SelectValue -} from '@/components/ui/select'; +import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { Switch } from '@/components/ui/switch'; import { Textarea } from '@/components/ui/textarea'; import { toast } from '@/components/ui/use-toast'; @@ -63,6 +57,7 @@ interface EditPromotionFormProps { export default function EditPromotionForm({ promotion, onSuccess, onCancel }: EditPromotionFormProps) { const [isSubmitting, setIsSubmitting] = useState(false); + const [discountType, setDiscountType] = useState<'percentage' | 'fixed'>(promotion.discountType); // Format dates from ISO to YYYY-MM-DD for input elements const formatDateForInput = (dateString: string | null) => { @@ -86,6 +81,16 @@ export default function EditPromotionForm({ promotion, onSuccess, onCancel }: Ed }, }); + // Keep local state in sync with form + useEffect(() => { + const subscription = form.watch((value, { name }) => { + if (name === 'discountType') { + setDiscountType(value.discountType as 'percentage' | 'fixed'); + } + }); + return () => subscription.unsubscribe(); + }, [form, form.watch]); + // Form submission handler async function onSubmit(data: z.infer) { setIsSubmitting(true); @@ -112,22 +117,23 @@ export default function EditPromotionForm({ promotion, onSuccess, onCancel }: Ed return (
- -
+ +
( - Promotion Code + Promotion Code field.onChange(e.target.value.toUpperCase())} + className="h-10" /> - + Enter a unique code for your promotion. Only letters and numbers. @@ -136,33 +142,32 @@ export default function EditPromotionForm({ promotion, onSuccess, onCancel }: Ed />
-
+
( - - Discount Type - - - Choose between percentage or fixed amount discount - + + Discount Type + + { + field.onChange(value); + setDiscountType(value as 'percentage' | 'fixed'); + }} + value={field.value} + className="flex flex-col space-y-2" + > +
+ + +
+
+ + +
+
+
)} @@ -172,19 +177,20 @@ export default function EditPromotionForm({ promotion, onSuccess, onCancel }: Ed control={form.control} name="discountValue" render={({ field }) => ( - - Discount Value + + Discount Value - - {form.watch('discountType') === 'percentage' + + {discountType === 'percentage' ? 'Enter a percentage (1-100%)' : 'Enter an amount in £'} @@ -197,32 +203,31 @@ export default function EditPromotionForm({ promotion, onSuccess, onCancel }: Ed control={form.control} name="minOrderAmount" render={({ field }) => ( - - Minimum Order Amount (£) + + Minimum Order Amount (£) - + - + Minimum purchase required )} /> -
- -
+ ( - - Maximum Usage Count + + Maximum Usage Count { @@ -231,22 +236,24 @@ export default function EditPromotionForm({ promotion, onSuccess, onCancel }: Ed }} /> - + Leave empty for unlimited usage (currently used: {promotion.usageCount} times) )} /> +
+
( - Start Date + Start Date - + @@ -258,10 +265,11 @@ export default function EditPromotionForm({ promotion, onSuccess, onCancel }: Ed name="endDate" render={({ field }) => ( - End Date (Optional) + End Date (Optional) { @@ -270,7 +278,7 @@ export default function EditPromotionForm({ promotion, onSuccess, onCancel }: Ed }} /> - + Leave empty for no expiration diff --git a/components/dashboard/promotions/NewPromotionForm.tsx b/components/dashboard/promotions/NewPromotionForm.tsx index 6fb7d63..ea5dda8 100644 --- a/components/dashboard/promotions/NewPromotionForm.tsx +++ b/components/dashboard/promotions/NewPromotionForm.tsx @@ -1,6 +1,6 @@ 'use client'; -import { useState } from 'react'; +import { useState, useEffect } from 'react'; import { z } from 'zod'; import { zodResolver } from '@hookform/resolvers/zod'; import { useForm } from 'react-hook-form'; @@ -25,6 +25,7 @@ import { } from '@/components/ui/select'; import { Switch } from '@/components/ui/switch'; import { Textarea } from '@/components/ui/textarea'; +import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'; import { toast } from '@/components/ui/use-toast'; import { PromotionFormData } from '@/lib/types/promotion'; import { fetchClient } from '@/lib/client-service'; @@ -62,6 +63,7 @@ interface NewPromotionFormProps { export default function NewPromotionForm({ onSuccess, onCancel }: NewPromotionFormProps) { const [isSubmitting, setIsSubmitting] = useState(false); + const [discountType, setDiscountType] = useState<'percentage' | 'fixed'>('percentage'); // Initialize form with default values const form = useForm>({ @@ -79,6 +81,16 @@ export default function NewPromotionForm({ onSuccess, onCancel }: NewPromotionFo }, }); + // Keep local state in sync with form + useEffect(() => { + const subscription = form.watch((value, { name }) => { + if (name === 'discountType') { + setDiscountType(value.discountType as 'percentage' | 'fixed'); + } + }); + return () => subscription.unsubscribe(); + }, [form, form.watch]); + // Form submission handler async function onSubmit(data: z.infer) { setIsSubmitting(true); @@ -105,22 +117,23 @@ export default function NewPromotionForm({ onSuccess, onCancel }: NewPromotionFo return ( - -
+ +
( - Promotion Code + Promotion Code field.onChange(e.target.value.toUpperCase())} + className="h-10" /> - + Enter a unique code for your promotion. Only letters and numbers. @@ -129,33 +142,32 @@ export default function NewPromotionForm({ onSuccess, onCancel }: NewPromotionFo />
-
+
( - - Discount Type - - - Choose between percentage or fixed amount discount - + + Discount Type + + { + field.onChange(value); + setDiscountType(value as 'percentage' | 'fixed'); + }} + value={field.value} + className="flex flex-col space-y-2" + > +
+ + +
+
+ + +
+
+
)} @@ -165,19 +177,20 @@ export default function NewPromotionForm({ onSuccess, onCancel }: NewPromotionFo control={form.control} name="discountValue" render={({ field }) => ( - - Discount Value + + Discount Value - - {form.watch('discountType') === 'percentage' + + {discountType === 'percentage' ? 'Enter a percentage (1-100%)' : 'Enter an amount in £'} @@ -185,37 +198,36 @@ export default function NewPromotionForm({ onSuccess, onCancel }: NewPromotionFo )} /> - + ( - - Minimum Order Amount (£) + + Minimum Order Amount (£) - + - + Minimum purchase required )} /> -
- -
+ ( - - Maximum Usage Count + + Maximum Usage Count { @@ -224,22 +236,24 @@ export default function NewPromotionForm({ onSuccess, onCancel }: NewPromotionFo }} /> - + Leave empty for unlimited usage )} /> +
+
( - Start Date + Start Date - + @@ -251,10 +265,11 @@ export default function NewPromotionForm({ onSuccess, onCancel }: NewPromotionFo name="endDate" render={({ field }) => ( - End Date (Optional) + End Date (Optional) { @@ -263,7 +278,7 @@ export default function NewPromotionForm({ onSuccess, onCancel }: NewPromotionFo }} /> - + Leave empty for no expiration diff --git a/components/dashboard/promotions/PromotionsList.tsx b/components/dashboard/promotions/PromotionsList.tsx index d2399fa..426684c 100644 --- a/components/dashboard/promotions/PromotionsList.tsx +++ b/components/dashboard/promotions/PromotionsList.tsx @@ -224,33 +224,37 @@ export default function PromotionsList() { {/* New Promotion Dialog */} - - - Create New Promotion + + + Create New Promotion Add a promotional code to offer discounts to your customers. - setShowNewDialog(false)} /> +
+ setShowNewDialog(false)} /> +
{/* Edit Promotion Dialog */} editingPromotion && handleCloseEditDialog()}> - - - Edit Promotion + + + Edit Promotion Modify this promotional code's details. - {editingPromotion && ( - - )} +
+ {editingPromotion && ( + + )} +