All files / src/hooks useMovieToolbar.ts

2.5% Statements 1/40
0% Branches 0/22
0% Functions 0/6
3.03% Lines 1/33

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                      5x                                                                                                                                                                                                            
import React from "react";
import { useTranslation } from "react-i18next";
 
import useFavoriteMovieMutation from "@/mutations/useFavoriteMovieMutation";
import useMovieWatchListMutation from "@/mutations/useMovieWatchListMutation";
import useRateMovieMutation from "@/mutations/useRateMovieMutation";
import useDeleteMovieRatingMutation from "@/mutations/useDeleteMovieRatingMutation";
 
import useUserStore from "@/store/useUserStore";
import useMovieStore from "@/store/useMovieStore";
 
const useMovieToolbar = (movieId: number) => {
  const { t } = useTranslation();
  const { sessionId, accountId, isAuthenticated, accentColor, username, setAccentColor } =
    useUserStore();
  const { isFavorite, isInWatchlist, isRated } = useMovieStore();
 
  const [rating, setRating] = React.useState<any>(isRated);
  const [showRating, setShowRating] = React.useState<boolean>(false);
 
  const favoriteTooltip = isAuthenticated
    ? t("add_movie_to_favorite")
    : t("login_to_add_movie_to_favorite");
  const watchListTooltip = isAuthenticated
    ? t("add_movie_to_watch_list")
    : t("login_to_add_movie_to_watchlist");
  const rateMovieTooltip = isAuthenticated ? t("rate_movie") : t("login_to_rate_movie");
 
  const { rateMovieMutation } = useRateMovieMutation();
  const { deleteRatingMutation } = useDeleteMovieRatingMutation();
  const { setFavoriteMutation, isPending: addingFavoriteLoading } = useFavoriteMovieMutation();
  const { setMovieWatchListMutation, isPending: addingWatchListLoading } =
    useMovieWatchListMutation();
 
  React.useEffect(() => {
    if (isRated !== null) setRating(isRated);
  }, [isRated]);
 
  const handleSetMovieToFavorite = () => {
    if (!isAuthenticated) return;
 
    setFavoriteMutation({
      accountId,
      sessionId,
      id: movieId,
      favorite: !isFavorite,
    });
  };
 
  const handleSetMovieToWatchList = () => {
    if (!isAuthenticated) return;
 
    setMovieWatchListMutation({
      accountId,
      sessionId,
      id: movieId,
      isInWatchlist: !isInWatchlist,
    });
  };
 
  const handleMovieRating = (ratingValue: number | null) => {
    if (!isAuthenticated) return;
    setRating(ratingValue);
 
    if (ratingValue === null) {
      const deleteResponse: any = deleteRatingMutation({
        id: movieId,
        sessionId,
      });
 
      if (deleteResponse.status.success) setRating(ratingValue);
    } else {
      const ratingResponse: any = rateMovieMutation({
        id: movieId,
        rating: ratingValue,
        sessionId,
      });
 
      if (ratingResponse.status.success) setRating(ratingValue);
    }
  };
 
  const onRateMovie = () => {
    if (isAuthenticated) return setShowRating(!showRating);
    return;
  };
 
  return {
    t,
    accentColor,
    isAuthenticated,
    sessionId,
    accountId,
    username,
    setAccentColor,
    isFavorite,
    isInWatchlist,
    isRated,
    addingFavoriteLoading,
    addingWatchListLoading,
    rating,
    showRating,
    handleSetMovieToFavorite,
    handleSetMovieToWatchList,
    handleMovieRating,
    onRateMovie,
    favoriteTooltip,
    watchListTooltip,
    rateMovieTooltip,
  };
};
 
export default useMovieToolbar;