From b6962b9ab23becaf31c63012687401364cb0f911 Mon Sep 17 00:00:00 2001 From: Prayag Prajapati Date: Wed, 6 Sep 2023 23:41:11 +0530 Subject: [PATCH] Update like and markPlayed buttons --- package-lock.json | 11 -- package.json | 2 - src/App.jsx | 15 -- src/components/appBar/appBar.jsx | 146 +------------------- src/components/buttons/likeButton.jsx | 117 +++++++--------- src/components/buttons/markPlayedButton.jsx | 108 ++++++--------- src/components/card/card.jsx | 2 +- src/components/layouts/item/hero.jsx | 137 ++++++++++++++++-- src/utils/store/appLoading.js | 14 -- 9 files changed, 222 insertions(+), 330 deletions(-) delete mode 100644 src/utils/store/appLoading.js diff --git a/package-lock.json b/package-lock.json index 03071e1..3b2ab59 100644 --- a/package-lock.json +++ b/package-lock.json @@ -40,8 +40,6 @@ }, "devDependencies": { "@tauri-apps/cli": "^1.3.1", - "@types/react": "^18.2.21", - "@types/react-dom": "^18.2.6", "@vitejs/plugin-react": "^4.0.3", "sass": "^1.66.1", "vite": "^4.4.2" @@ -1743,15 +1741,6 @@ "csstype": "^3.0.2" } }, - "node_modules/@types/react-dom": { - "version": "18.2.7", - "resolved": "https://registry.npmjs.org/@types/react-dom/-/react-dom-18.2.7.tgz", - "integrity": "sha512-GRaAEriuT4zp9N4p1i8BDBYmEyfo+xQ3yHjJU4eiK5NDa1RmUZG+unZABUTK4/Ox/M+GaHwb6Ow8rUITrtjszA==", - "dev": true, - "dependencies": { - "@types/react": "*" - } - }, "node_modules/@types/react-is": { "version": "18.2.1", "resolved": "https://registry.npmjs.org/@types/react-is/-/react-is-18.2.1.tgz", diff --git a/package.json b/package.json index 5a463f1..a48ad25 100644 --- a/package.json +++ b/package.json @@ -41,8 +41,6 @@ }, "devDependencies": { "@tauri-apps/cli": "^1.3.1", - "@types/react": "^18.2.21", - "@types/react-dom": "^18.2.6", "@vitejs/plugin-react": "^4.0.3", "sass": "^1.66.1", "vite": "^4.4.2" diff --git a/src/App.jsx b/src/App.jsx index bd7a839..918bf00 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -72,7 +72,6 @@ import { delUser, getUser } from "./utils/storage/user.js"; import axios from "axios"; import { useBackdropStore } from "./utils/store/backdrop.js"; -import { useAppLoadingStore } from "./utils/store/appLoading.js"; import { usePlaybackDataLoadStore } from "./utils/store/playback.js"; const jellyfin = new Jellyfin({ @@ -134,24 +133,10 @@ function App() { const [serverReachable, setServerReachable] = useState(true); - const [setIsAppLoading, setIsAppSuccess, setIsAppError] = - useAppLoadingStore((state) => [ - state.setIsLoading, - state.setIsSuccess, - state.setIsError, - ]); const [playbackDataLoading] = usePlaybackDataLoadStore((state) => [ state.isLoading, ]); - useEffect(() => { - const appLoadingStateTimer = setInterval(() => { - setIsAppSuccess(false); - setIsAppError(false); - }, 8000); - return () => clearInterval(appLoadingStateTimer); - }, [setIsAppSuccess, setIsAppError]); - const { enqueueSnackbar } = useSnackbar(); const createApi = async () => { diff --git a/src/components/appBar/appBar.jsx b/src/components/appBar/appBar.jsx index 5249c18..6eb63da 100644 --- a/src/components/appBar/appBar.jsx +++ b/src/components/appBar/appBar.jsx @@ -18,12 +18,11 @@ import CircularProgress from "@mui/material/CircularProgress"; import useScrollTrigger from "@mui/material/useScrollTrigger"; import { MdiMagnify } from "../icons/mdiMagnify"; -import { theme } from "../../theme"; import { useLocation, useNavigate } from "react-router-dom"; import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api"; -import { useQuery } from "@tanstack/react-query"; +import { useIsMutating, useQuery } from "@tanstack/react-query"; import "./appBar.module.scss"; import { MdiAccount } from "../icons/mdiAccount"; @@ -36,12 +35,6 @@ import { useDrawerStore } from "../../utils/store/drawer"; import { delServer } from "../../utils/storage/servers"; import { delUser } from "../../utils/storage/user"; import { MdiDelete } from "../icons/mdiDelete"; -import { MdiCogSync } from "../icons/mdiCogSync"; -import { useAppLoadingStore } from "../../utils/store/appLoading"; -import { MdiCheck } from "../icons/mdiCheck"; -import { MdiAlertDecagram } from "../icons/mdiAlertDecagram"; -import { green, red } from "@mui/material/colors"; - export const AppBar = () => { const navigate = useNavigate(); @@ -80,10 +73,6 @@ export const AppBar = () => { setDrawerOpen(true); }; - const [syncLoading, syncSuccess, syncError] = useAppLoadingStore( - (state) => [state.isLoading, state.isSuccess, state.isError], - ); - useEffect(() => { if ( location.pathname.includes("login") || @@ -162,138 +151,7 @@ export const AppBar = () => { - - {syncLoading ? ( - - - - - - - ) : syncSuccess ? ( - - - - - - - ) : syncError ? ( - - - - - - - ) : ( - <> - )} - - - + { const queryClient = useQueryClient(); const { enqueueSnackbar } = useSnackbar(); - const [setIsLoading, setIsSuccess, setIsError] = useAppLoadingStore( - (state) => [state.setIsLoading, state.setIsSuccess, state.setIsError], - ); - const handleLiking = async () => { let result; - console.log(isFavorite); if (isFavorite) { result = await getUserLibraryApi(window.api).unmarkFavoriteItem({ userId: userId, @@ -36,44 +33,6 @@ const LikeButton = ({ itemId, isFavorite, queryKey, userId, itemName }) => { }; const mutation = useMutation({ mutationFn: handleLiking, - onMutate: () => { - setIsLoading(true); - setIsError(false); - setIsSuccess(false); - - queryClient.cancelQueries(queryKey); - const currentLatestMedia = queryClient.getQueryData(queryKey); - queryClient.setQueryData(queryKey, (oldMedia) => { - try { - oldMedia.Items.map((oitem) => { - if (oitem.Id == itemId) { - return { - ...oitem, - UserData: { - ...oitem.UserData, - IsFavorite: !isFavorite, - }, - }; - } - return oitem; - }); - } catch (error) { - oldMedia.map((oitem) => { - if (oitem.Id == itemId) { - return { - ...oitem, - UserData: { - ...oitem.UserData, - IsFavorite: !isFavorite, - }, - }; - } - return oitem; - }); - } - }); - return { currentLatestMedia }; - }, onError: (error, a, context) => { enqueueSnackbar(`${error}`, { variant: "error", @@ -84,39 +43,57 @@ const LikeButton = ({ itemId, isFavorite, queryKey, userId, itemName }) => { variant: "error", }, ); - - queryClient.setQueryData(queryKey, context.previousData); - setIsLoading(false); - setIsError(true); - console.log("set"); - console.error(error); }, - onSuccess: () => { - console.log("Successfully updated", itemId); - queryClient.invalidateQueries(queryKey); - setIsLoading(false); - setIsSuccess(true); - }, - onSettled: () => { - console.log("set"); - setIsLoading(false); + onSettled: async () => { + return await queryClient.invalidateQueries({ + queryKey: queryKey, + }); }, + mutationKey: ["likeButton", itemId], }); + return ( - { - e.stopPropagation(); - mutation.mutate(); +
- {isFavorite ? ( - - ) : ( - - )} - + + { + if (!mutation.isLoading) { + e.stopPropagation(); + mutation.mutate(); + } + }} + style={{ + position: "absolute", + top: 0, + left: 0, + right: 0, + bottom: 0, + display: "flex", + alignItems: "center", + justifyContent: "center", + opacity: mutation.isLoading ? 0.5 : 1, + }} + > + {isFavorite ? ( + + ) : ( + + )} + +
); }; diff --git a/src/components/buttons/markPlayedButton.jsx b/src/components/buttons/markPlayedButton.jsx index dded071..4999344 100644 --- a/src/components/buttons/markPlayedButton.jsx +++ b/src/components/buttons/markPlayedButton.jsx @@ -1,6 +1,8 @@ /** @format */ import PropTypes from "prop-types"; +import { useEffect } from "react"; + import IconButton from "@mui/material/IconButton"; import CircularProgress from "@mui/material/CircularProgress"; @@ -10,16 +12,11 @@ import { green } from "@mui/material/colors"; import { getPlaystateApi } from "@jellyfin/sdk/lib/utils/api/playstate-api"; import { MdiCheck } from "../icons/mdiCheck"; import { useSnackbar } from "notistack"; -import { useAppLoadingStore } from "../../utils/store/appLoading"; const MarkPlayedButton = ({ itemId, isPlayed, queryKey, userId, itemName }) => { const queryClient = useQueryClient(); const { enqueueSnackbar } = useSnackbar(); - const [setIsLoading, setIsSuccess, setIsError] = useAppLoadingStore( - (state) => [state.setIsLoading, state.setIsSuccess, state.setIsError], - ); - const handleMarking = async () => { if (!isPlayed) { await getPlaystateApi(window.api).markPlayedItem({ @@ -35,79 +32,66 @@ const MarkPlayedButton = ({ itemId, isPlayed, queryKey, userId, itemName }) => { }; const mutation = useMutation({ mutationFn: handleMarking, - onMutate: () => { - setIsLoading(true); - setIsError(false); - setIsSuccess(false); - queryClient.cancelQueries(queryKey); - const previousData = queryClient.getQueryData(queryKey); - queryClient.setQueryData(queryKey, (oldMedia) => { - try { - oldMedia.Items.map((oitem) => { - if (oitem.Id == itemId) { - return { - ...oitem, - UserData: { - ...oitem.UserData, - Played: !isPlayed, - }, - }; - } - return oitem; - }); - } catch (error) { - oldMedia.map((oitem) => { - if (oitem.Id == itemId) { - return { - ...oitem, - UserData: { - ...oitem.UserData, - Played: !isPlayed, - }, - }; - } - return oitem; - }); - } - }); - return { previousData }; - }, onError: (error, a, context) => { + enqueueSnackbar(`${error}`, { + variant: "error", + }); enqueueSnackbar( `An error occured while updating "${itemName}"`, { variant: "error", }, ); - queryClient.setQueryData(queryKey, context.previousData); - setIsLoading(false); - setIsError(true); console.error(error); }, - onSuccess: () => { - console.log("Successfully updated", itemId); - queryClient.invalidateQueries(queryKey); - setIsLoading(false); - setIsSuccess(true); - }, - onSettled: () => { - setIsLoading(false); + onSettled: async () => { + return await queryClient.invalidateQueries({ + queryKey: queryKey, + }); }, + mutationKey: ["markPlayedButton", itemId], }); return ( - { - e.stopPropagation(); - mutation.mutate(); +
- - + { + if (!mutation.isLoading) { + e.stopPropagation(); + mutation.mutate(); + } + }} + style={{ + position: "absolute", + top: 0, + left: 0, + right: 0, + bottom: 0, + display: "flex", + alignItems: "center", + justifyContent: "center", + opacity: mutation.isLoading ? 0.5 : 1, + }} + > + + +
); }; diff --git a/src/components/card/card.jsx b/src/components/card/card.jsx index 8f43d44..1ed3b8f 100644 --- a/src/components/card/card.jsx +++ b/src/components/card/card.jsx @@ -287,7 +287,7 @@ export const Card = ({ Card.propTypes = { item: PropTypes.any.isRequired, cardTitle: PropTypes.string, - cardCaption: PropTypes.string, + cardCaption: PropTypes.any, imageType: PropTypes.string, imageBlurhash: PropTypes.string, cardType: PropTypes.string, diff --git a/src/components/layouts/item/hero.jsx b/src/components/layouts/item/hero.jsx index f9c9c29..13aa940 100644 --- a/src/components/layouts/item/hero.jsx +++ b/src/components/layouts/item/hero.jsx @@ -1,8 +1,15 @@ /** @format */ +import Chip from "@mui/material/Chip"; +import Stack from "@mui/material/Stack"; +import Typography from "@mui/material/Typography"; import { BaseItemKind } from "@jellyfin/sdk/lib/generated-client"; import { Card } from "../../card/card"; -import Typography from "@mui/material/Typography"; +import { MdiStar } from "../../icons/mdiStar"; +import { yellow } from "@mui/material/colors"; +import { endsAt, getRuntime } from "../../../utils/date/time"; +import LikeButton from "../../buttons/likeButton"; +import MarkPlayedButton from "../../buttons/markPlayedButton"; const Hero = ({ item, queryKey, userId }) => { return ( @@ -30,24 +37,132 @@ const Hero = ({ item, queryKey, userId }) => { />
-
- +
+ + {item.Name} + +
+
+ } + className="hero-carousel-info" + mt={1} > - {item.Name} + + {!!item.ProductionYear + ? item.ProductionYear + : "Unknown"} + + +
+ {!!item.CommunityRating ? ( + <> + + + {Math.round( + item.CommunityRating * + 10, + ) / 10} + + + ) : ( + + No Community Rating + + )} +
+ {!!item.RunTimeTicks && ( + + {getRuntime(item.RunTimeTicks)} + + )} + {!!item.RunTimeTicks && ( + + {endsAt(item.RunTimeTicks)} + + )} + + + {item.Genres.join(", ")}
+
+ + +
- + {item.Overview}
diff --git a/src/utils/store/appLoading.js b/src/utils/store/appLoading.js deleted file mode 100644 index 1cf27f5..0000000 --- a/src/utils/store/appLoading.js +++ /dev/null @@ -1,14 +0,0 @@ -/** @format */ - -import { create } from "zustand"; - -export const useAppLoadingStore = create((set) => ({ - isLoading: false, - isSuccess: false, - isError: false, - setIsLoading: (loading) => - set((state) => ({ ...state, isLoading: loading })), - setIsSuccess: (success) => - set((state) => ({ ...state, isSuccess: success })), - setIsError: (error) => set((state) => ({ ...state, isError: error })), -}));