Skip to content

Commit 789fc41

Browse files
Fix: Campaign creation not working
1 parent 28335ab commit 789fc41

File tree

1 file changed

+47
-78
lines changed

1 file changed

+47
-78
lines changed

src/hooks/useCampaignFormSimple.ts

Lines changed: 47 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
import { useState } from 'react';
33
import { useAuth } from '@/hooks/useAuth';
4-
import { useCampaigns } from '@/hooks/useCampaigns';
54
import { usePaymentMethods } from '@/hooks/usePaymentMethods';
65
import { useToast } from '@/hooks/use-toast';
6+
import { supabase } from '@/integrations/supabase/client';
77

88
export interface CampaignFormData {
99
name: string;
@@ -25,7 +25,6 @@ export const useCampaignFormSimple = () => {
2525
});
2626

2727
const { user } = useAuth();
28-
const { createCampaign } = useCampaigns();
2928
const { paymentMethods, refreshPaymentMethods } = usePaymentMethods();
3029
const { toast } = useToast();
3130

@@ -42,109 +41,79 @@ export const useCampaignFormSimple = () => {
4241

4342
const handleSubmit = async (e: React.FormEvent) => {
4443
e.preventDefault();
45-
setLoading(true);
46-
47-
try {
48-
console.log('🚀 SIMPLE: Début création campagne...');
49-
50-
if (!formData.name) {
51-
throw new Error('Le nom de la campagne est requis');
52-
}
53-
54-
if (!formData.targetUrl) {
55-
throw new Error('L\'URL de destination est requise');
56-
}
57-
58-
// Vérifier les cartes disponibles
59-
await refreshPaymentMethods();
60-
61-
if (paymentMethods.length === 0) {
62-
throw new Error('Aucune carte de paiement disponible');
63-
}
64-
65-
if (paymentMethods.length === 1) {
66-
// Une seule carte disponible, l'utiliser directement
67-
console.log('💳 SIMPLE: Utilisation carte unique:', paymentMethods[0].id);
68-
69-
const campaignId = await createCampaign({
70-
name: formData.name,
71-
description: formData.description,
72-
targetUrl: formData.targetUrl,
73-
isActive: formData.isActive,
74-
isDraft: false,
75-
paymentConfigured: true,
76-
defaultCommissionRate: 10,
77-
stripePaymentMethodId: paymentMethods[0].id,
78-
});
79-
80-
console.log('✅ SIMPLE: Campagne créée avec ID:', campaignId.id);
81-
82-
// Déclencher la modale de succès
83-
setCreatedCampaign({ id: campaignId.id, name: formData.name });
84-
setShowSuccessModal(true);
85-
86-
toast({
87-
title: "Campagne créée avec succès !",
88-
description: "Votre campagne est maintenant active.",
89-
});
90-
} else {
91-
// Plusieurs cartes disponibles, afficher le sélecteur
92-
console.log('💳 SIMPLE: Plusieurs cartes disponibles, affichage sélecteur');
93-
setShowPaymentSelector(true);
94-
}
44+
45+
if (!user?.uid) {
46+
toast({
47+
title: "Erreur",
48+
description: "Vous devez être connecté pour créer une campagne",
49+
variant: "destructive",
50+
});
51+
return;
52+
}
9553

96-
} catch (error: any) {
97-
console.error('❌ SIMPLE: Erreur création campagne:', error);
54+
if (!formData.name.trim() || !formData.targetUrl.trim()) {
9855
toast({
9956
title: "Erreur",
100-
description: error.message || "Impossible de créer la campagne",
57+
description: "Veuillez remplir tous les champs obligatoires",
10158
variant: "destructive",
10259
});
103-
} finally {
104-
setLoading(false);
60+
return;
10561
}
106-
};
10762

108-
const handleCardSelection = async (cardId: string) => {
10963
setLoading(true);
64+
11065
try {
111-
console.log('💳 SIMPLE: Carte sélectionnée:', cardId);
66+
console.log('🚀 SIMPLE: Création campagne avec:', formData);
11267

113-
const campaignId = await createCampaign({
114-
name: formData.name,
115-
description: formData.description,
116-
targetUrl: formData.targetUrl,
117-
isActive: formData.isActive,
118-
isDraft: false,
119-
paymentConfigured: true,
120-
defaultCommissionRate: 10,
121-
stripePaymentMethodId: cardId,
122-
});
123-
124-
console.log('✅ SIMPLE: Campagne créée avec carte sélectionnée:', campaignId);
68+
// Créer la campagne directement dans Supabase
69+
const { data: campaign, error } = await supabase
70+
.from('campaigns')
71+
.insert({
72+
name: formData.name,
73+
description: formData.description || '',
74+
target_url: formData.targetUrl,
75+
is_active: formData.isActive,
76+
user_id: user.uid,
77+
is_draft: false,
78+
payment_configured: true,
79+
default_commission_rate: 0.10
80+
})
81+
.select()
82+
.single();
83+
84+
if (error) {
85+
console.error('❌ SIMPLE: Erreur création campagne:', error);
86+
throw new Error('Erreur lors de la création de la campagne');
87+
}
88+
89+
console.log('✅ SIMPLE: Campagne créée:', campaign);
12590

126-
// Fermer le sélecteur et ouvrir la modale de succès
127-
setShowPaymentSelector(false);
128-
setCreatedCampaign({ id: campaignId.id, name: formData.name });
91+
// Déclencher la modale de succès
92+
setCreatedCampaign({ id: campaign.id, name: campaign.name });
12993
setShowSuccessModal(true);
13094

13195
toast({
13296
title: "Campagne créée avec succès !",
133-
description: "Votre campagne est maintenant active avec la carte sélectionnée.",
97+
description: "Votre campagne est maintenant active.",
13498
});
13599

136100
} catch (error: any) {
137-
console.error('❌ SIMPLE: Erreur création avec carte sélectionnée:', error);
101+
console.error('❌ SIMPLE: Erreur inattendue:', error);
138102
toast({
139103
title: "Erreur",
140-
description: error.message || "Impossible de créer la campagne",
104+
description: error.message || "Une erreur inattendue s'est produite",
141105
variant: "destructive",
142106
});
143107
} finally {
144108
setLoading(false);
145109
}
146110
};
147111

112+
const handleCardSelection = async (cardId: string) => {
113+
console.log('💳 SIMPLE: Sélection carte (simplifié):', cardId);
114+
setShowPaymentSelector(false);
115+
};
116+
148117
const handleSuccessModalClose = () => {
149118
setShowSuccessModal(false);
150119
setCreatedCampaign(null);

0 commit comments

Comments
 (0)