All files / client/src/components favorites.tsx

0% Statements 0/40
0% Branches 0/29
0% Functions 0/15
0% Lines 0/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176                                                                                                                                                                                                                                                                                                                                                               
import React from "react";
import { useMutation, useQuery } from "@tanstack/react-query";
import { Button } from "@/components/ui/button";
import { Star } from "lucide-react";
import { apiRequest, queryClient } from "@/lib/queryClient";
import type { UserFavorite, FavoriteType } from "@shared/schema";
import { useToast } from "@/hooks/use-toast";
 
interface FavoriteButtonProps {
  favoriteType: FavoriteType;
  favoriteId: string;
  favoriteName: string;
  metadata?: string;
  size?: "default" | "sm" | "lg" | "icon";
  variant?: "default" | "outline" | "ghost";
  showLabel?: boolean;
}
 
export function FavoriteButton({
  favoriteType,
  favoriteId,
  favoriteName,
  metadata,
  size = "icon",
  variant = "ghost",
  showLabel = false,
}: FavoriteButtonProps) {
  const { toast } = useToast();
 
  const { data: favorites = [] } = useQuery<UserFavorite[]>({
    queryKey: ["/api/favorites", favoriteType],
    queryFn: async () => {
      const res = await apiRequest("GET", `/api/favorites?type=${favoriteType}`);
      return res.json();
    },
  });
 
  const isFavorited = favorites.some(f => f.favoriteId === favoriteId);
 
  const addFavoriteMutation = useMutation({
    mutationFn: async () => {
      const res = await apiRequest("POST", "/api/favorites", {
        favoriteType,
        favoriteId,
        favoriteName,
        metadata: metadata || null,
      });
      return res.json();
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["/api/favorites"] });
      toast({
        title: "Added to favorites",
        description: `${favoriteName} has been added to your favorites.`,
      });
    },
    onError: (error: any) => {
      if (error.message?.includes("409")) {
        toast({
          title: "Already favorited",
          description: "This item is already in your favorites.",
          variant: "destructive",
        });
      } else {
        toast({
          title: "Error",
          description: "Failed to add to favorites.",
          variant: "destructive",
        });
      }
    },
  });
 
  const removeFavoriteMutation = useMutation({
    mutationFn: async () => {
      const favorite = favorites.find(f => f.favoriteId === favoriteId);
      if (!favorite) throw new Error("Favorite not found");
      
      const res = await apiRequest("DELETE", `/api/favorites/${favorite.id}`);
      return res;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["/api/favorites"] });
      toast({
        title: "Removed from favorites",
        description: `${favoriteName} has been removed from your favorites.`,
      });
    },
    onError: () => {
      toast({
        title: "Error",
        description: "Failed to remove from favorites.",
        variant: "destructive",
      });
    },
  });
 
  const handleToggle = (e: React.MouseEvent<HTMLButtonElement>) => {
    e.stopPropagation();
    if (isFavorited) {
      removeFavoriteMutation.mutate();
    } else {
      addFavoriteMutation.mutate();
    }
  };
 
  return (
    <Button
      variant={variant}
      size={size}
      onClick={handleToggle}
      disabled={addFavoriteMutation.isPending || removeFavoriteMutation.isPending}
      className={isFavorited ? "text-yellow-500 hover:text-yellow-600" : ""}
    >
      <Star className={`h-4 w-4 ${isFavorited ? "fill-current" : ""}`} />
      {showLabel && (
        <span className="ml-2">
          {isFavorited ? "Unfavorite" : "Favorite"}
        </span>
      )}
    </Button>
  );
}
 
interface FavoritesListProps {
  favoriteType: FavoriteType;
  onSelectFavorite?: (favorite: UserFavorite) => void;
}
 
export function FavoritesList({ favoriteType, onSelectFavorite }: FavoritesListProps) {
  const { data: favorites = [], isLoading } = useQuery<UserFavorite[]>({
    queryKey: ["/api/favorites", favoriteType],
    queryFn: async () => {
      const res = await apiRequest("GET", `/api/favorites?type=${favoriteType}`);
      return res.json();
    },
  });
 
  if (isLoading) {
    return <p className="text-sm text-muted-foreground">Loading favorites...</p>;
  }
 
  if (favorites.length === 0) {
    return (
      <p className="text-sm text-muted-foreground">
        No favorites yet. Click the star icon to add favorites!
      </p>
    );
  }
 
  return (
    <div className="space-y-2">
      {favorites.map(favorite => (
        <div
          key={favorite.id}
          className="flex items-center justify-between p-3 border rounded-lg hover:bg-muted/50 cursor-pointer"
          onClick={() => onSelectFavorite?.(favorite)}
        >
          <div className="flex items-center gap-2">
            <Star className="h-4 w-4 fill-yellow-500 text-yellow-500" />
            <span className="font-medium">{favorite.favoriteName}</span>
          </div>
          <FavoriteButton
            favoriteType={favorite.favoriteType as FavoriteType}
            favoriteId={favorite.favoriteId}
            favoriteName={favorite.favoriteName}
            metadata={favorite.metadata || undefined}
            size="sm"
            variant="ghost"
          />
        </div>
      ))}
    </div>
  );
}