@@ -6,19 +6,20 @@ import { Label } from '@/components/ui/label';
66import { Textarea } from '@/components/ui/textarea' ;
77import { toast } from '@/hooks/use-toast' ;
88import { ExternalLink , Copy , CheckCircle } from 'lucide-react' ;
9+ import { supabase } from '@/integrations/supabase/client' ;
910
1011interface ShopifyPrivateAppDialogProps {
1112 open : boolean ;
1213 onOpenChange : ( open : boolean ) => void ;
1314 shopName : string ;
14- onConnect : ( accessToken : string , shopDomain : string ) => void ;
15+ onSuccess : ( ) => void ; // Changé de onConnect vers onSuccess
1516}
1617
1718export const ShopifyPrivateAppDialog = ( {
1819 open,
1920 onOpenChange,
2021 shopName,
21- onConnect
22+ onSuccess
2223} : ShopifyPrivateAppDialogProps ) => {
2324 const [ accessToken , setAccessToken ] = useState ( '' ) ;
2425 const [ loading , setLoading ] = useState ( false ) ;
@@ -52,21 +53,44 @@ export const ShopifyPrivateAppDialog = ({
5253
5354 setLoading ( true ) ;
5455 try {
55- // Pas de validation côté client à cause des problèmes CORS
56- // La validation se fera côté serveur
56+ // Récupérer l'utilisateur connecté
57+ const { data : { user } } = await supabase . auth . getUser ( ) ;
58+ if ( ! user ) {
59+ toast ( {
60+ title : "Erreur" ,
61+ description : "Vous devez être connecté" ,
62+ variant : "destructive"
63+ } ) ;
64+ return ;
65+ }
66+
67+ // Appeler l'edge function pour sauvegarder l'intégration
68+ const response = await supabase . functions . invoke ( 'shopify-private-app' , {
69+ body : {
70+ shopDomain,
71+ accessToken : accessToken . trim ( ) ,
72+ campaignId : 'test-campaign-123' , // TODO: récupérer le vrai campaign ID
73+ userId : user . id
74+ }
75+ } ) ;
76+
77+ if ( response . error ) {
78+ throw new Error ( response . error . message || 'Erreur lors de la connexion' ) ;
79+ }
80+
5781 toast ( {
58- title : "Connexion en cours... " ,
59- description : "Vérification du token avec votre boutique" ,
82+ title : "Shopify connecté ! " ,
83+ description : `Boutique ${ response . data . shopInfo . name } connectée avec succès` ,
6084 } ) ;
6185
62- onConnect ( accessToken , shopDomain ) ;
6386 onOpenChange ( false ) ;
87+ onSuccess ( ) ;
6488
6589 } catch ( error ) {
66- console . error ( 'Erreur connexion:' , error ) ;
90+ console . error ( 'Erreur connexion Shopify :' , error ) ;
6791 toast ( {
6892 title : "Erreur de connexion" ,
69- description : "Une erreur s'est produite ",
93+ description : error instanceof Error ? error . message : "Vérifiez votre token et réessayez ",
7094 variant : "destructive"
7195 } ) ;
7296 } finally {
0 commit comments