11
22import { useState } from 'react' ;
33import { Button } from '@/components/ui/button' ;
4- import { Input } from '@/components/ui/input' ;
5- import { Label } from '@/components/ui/label' ;
6- import { Textarea } from '@/components/ui/textarea' ;
74import { Dialog , DialogContent , DialogDescription , DialogFooter , DialogHeader , DialogTitle , DialogTrigger } from '@/components/ui/dialog' ;
8- import { Switch } from '@/components/ui/switch' ;
9- import { useCampaigns } from '@/hooks/useCampaigns' ;
10- import { useStripePayment } from '@/hooks/useStripePayment' ;
11- import { useToast } from '@/hooks/use-toast' ;
125import { Plus , CreditCard } from 'lucide-react' ;
13- import { CampaignSuccessModal } from '@/components/CampaignSuccessModal' ;
6+ import { useCampaignForm } from '@/hooks/useCampaignForm' ;
7+ import { CampaignFormFields } from '@/components/CampaignFormFields' ;
148
159export const CreateCampaignDialog = ( ) => {
1610 const [ open , setOpen ] = useState ( false ) ;
17- const [ loading , setLoading ] = useState ( false ) ;
18- const [ formData , setFormData ] = useState ( {
19- name : '' ,
20- description : '' ,
21- targetUrl : '' ,
22- isActive : true ,
23- } ) ;
24-
25- const { createCampaign } = useCampaigns ( ) ;
26- const { setupPaymentForCampaign, loading : paymentLoading } = useStripePayment ( ) ;
27- const { toast } = useToast ( ) ;
28-
29- const handleFormSubmit = async ( e : React . FormEvent ) => {
30- e . preventDefault ( ) ;
31- setLoading ( true ) ;
32-
33- try {
34- console . log ( '🎯 Création de la campagne en mode draft...' ) ;
35-
36- // Créer la campagne en mode draft
37- const campaignId = await createCampaign ( {
38- ...formData ,
39- isDraft : true ,
40- paymentConfigured : false ,
41- } ) ;
42-
43- console . log ( '✅ Campagne draft créée:' , campaignId ) ;
44-
45- // Rediriger directement vers Stripe pour la configuration de paiement
46- await setupPaymentForCampaign ( campaignId , formData . name ) ;
47-
48- } catch ( error : any ) {
49- console . error ( '❌ Erreur création campagne:' , error ) ;
50- toast ( {
51- title : "Erreur" ,
52- description : error . message || "Impossible de créer la campagne" ,
53- variant : "destructive" ,
54- } ) ;
55- } finally {
56- setLoading ( false ) ;
57- }
58- } ;
11+ const {
12+ formData,
13+ loading,
14+ paymentLoading,
15+ updateFormData,
16+ resetForm,
17+ handleSubmit,
18+ } = useCampaignForm ( ) ;
5919
6020 const resetDialog = ( ) => {
61- setFormData ( { name : '' , description : '' , targetUrl : '' , isActive : true } ) ;
21+ resetForm ( ) ;
6222 setOpen ( false ) ;
6323 } ;
6424
@@ -81,50 +41,11 @@ export const CreateCampaignDialog = () => {
8141 </ DialogDescription >
8242 </ DialogHeader >
8343
84- < form onSubmit = { handleFormSubmit } >
85- < div className = "grid gap-4 py-4" >
86- < div className = "space-y-2" >
87- < Label htmlFor = "name" > Nom de la campagne</ Label >
88- < Input
89- id = "name"
90- value = { formData . name }
91- onChange = { ( e ) => setFormData ( { ...formData , name : e . target . value } ) }
92- placeholder = "Ex: Vente Produit A"
93- required
94- />
95- </ div >
96-
97- < div className = "space-y-2" >
98- < Label htmlFor = "description" > Description</ Label >
99- < Textarea
100- id = "description"
101- value = { formData . description }
102- onChange = { ( e ) => setFormData ( { ...formData , description : e . target . value } ) }
103- placeholder = "Description de la campagne..."
104- rows = { 3 }
105- />
106- </ div >
107-
108- < div className = "space-y-2" >
109- < Label htmlFor = "targetUrl" > URL de destination</ Label >
110- < Input
111- id = "targetUrl"
112- value = { formData . targetUrl }
113- onChange = { ( e ) => setFormData ( { ...formData , targetUrl : e . target . value } ) }
114- placeholder = "https://monsite.com/produit"
115- required
116- />
117- </ div >
118-
119- < div className = "flex items-center space-x-2" >
120- < Switch
121- id = "isActive"
122- checked = { formData . isActive }
123- onCheckedChange = { ( checked ) => setFormData ( { ...formData , isActive : checked } ) }
124- />
125- < Label htmlFor = "isActive" > Campagne active</ Label >
126- </ div >
127- </ div >
44+ < form onSubmit = { handleSubmit } >
45+ < CampaignFormFields
46+ formData = { formData }
47+ onUpdateFormData = { updateFormData }
48+ />
12849
12950 < DialogFooter className = "gap-2" >
13051 < Button
0 commit comments