ajout de la gestion des favoris
All checks were successful
Build and Push App / build (push) Successful in 13m38s

This commit is contained in:
2026-04-27 21:44:48 +02:00
parent 38caf36bee
commit a814c7b577
11 changed files with 423 additions and 8 deletions

View File

@@ -3,12 +3,47 @@
import React, { useState } from 'react';
import Link from 'next/link';
import { CheckCircle, MapPin, Star, Phone, Share2, Facebook, Linkedin, Instagram, Twitter } from 'lucide-react';
import { CheckCircle, MapPin, Star, Phone, Share2, Facebook, Linkedin, Instagram, Twitter, Heart } from 'lucide-react';
import { useUser } from './UserProvider';
import { Business } from '../types';
import { toast } from 'react-hot-toast';
const BusinessCard: React.FC<{ business: Business }> = ({ business }) => {
const { user } = useUser();
const [isShareOpen, setIsShareOpen] = useState(false);
const [isFavorited, setIsFavorited] = useState(false);
const [isToggling, setIsToggling] = useState(false);
const toggleFavorite = async (e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
if (!user) {
toast.error("Connectez-vous pour ajouter des favoris");
return;
}
setIsToggling(true);
try {
const res = await fetch('/api/favorites', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-user-id': user.id
},
body: JSON.stringify({ businessId: business.id })
});
const data = await res.json();
if (!data.error) {
setIsFavorited(data.favorited);
toast.success(data.favorited ? 'Ajouté aux favoris' : 'Retiré des favoris');
}
} catch (error) {
toast.error('Erreur réseau');
} finally {
setIsToggling(false);
}
};
const toggleShare = (e: React.MouseEvent) => {
e.preventDefault();
@@ -70,6 +105,14 @@ const BusinessCard: React.FC<{ business: Business }> = ({ business }) => {
<Phone className="w-4 h-4" />
</a>
)}
<button
onClick={toggleFavorite}
disabled={isToggling}
className={`p-2 backdrop-blur-md border rounded-full transition-all shadow-sm ${isFavorited ? 'bg-red-500 border-red-500 text-white' : 'bg-white/20 border-white/20 text-white hover:bg-white hover:text-red-500'}`}
title={isFavorited ? "Retirer des favoris" : "Ajouter aux favoris"}
>
<Heart className={`w-4 h-4 ${isFavorited ? 'fill-current' : ''}`} />
</button>
<div className="relative">
<button
onClick={toggleShare}