mirror of
https://github.com/piyush-eon/tanstack-query-weather-app.git
synced 2025-11-24 05:11:19 +00:00
44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
// src/components/weather/favorite-button.tsx
|
|
import { Star } from "lucide-react";
|
|
import { Button } from "@/components/ui/button";
|
|
import type { WeatherData } from "@/api/types";
|
|
import { useFavorites } from "@/hooks/use-favorite";
|
|
import { toast } from "sonner";
|
|
|
|
interface FavoriteButtonProps {
|
|
data: WeatherData;
|
|
}
|
|
|
|
export function FavoriteButton({ data }: FavoriteButtonProps) {
|
|
const { addFavorite, removeFavorite, isFavorite } = useFavorites();
|
|
const isCurrentlyFavorite = isFavorite(data.coord.lat, data.coord.lon);
|
|
|
|
const handleToggleFavorite = () => {
|
|
if (isCurrentlyFavorite) {
|
|
removeFavorite.mutate(`${data.coord.lat}-${data.coord.lon}`);
|
|
toast.error(`Removed ${data.name} from Favorites`);
|
|
} else {
|
|
addFavorite.mutate({
|
|
name: data.name,
|
|
lat: data.coord.lat,
|
|
lon: data.coord.lon,
|
|
country: data.sys.country,
|
|
});
|
|
toast.success(`Added ${data.name} to Favorites`);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Button
|
|
variant={isCurrentlyFavorite ? "default" : "outline"}
|
|
size="icon"
|
|
onClick={handleToggleFavorite}
|
|
className={isCurrentlyFavorite ? "bg-yellow-500 hover:bg-yellow-600" : ""}
|
|
>
|
|
<Star
|
|
className={`h-4 w-4 ${isCurrentlyFavorite ? "fill-current" : ""}`}
|
|
/>
|
|
</Button>
|
|
);
|
|
}
|