Update like and markPlayed buttons

This commit is contained in:
Prayag Prajapati committed 2023-09-06 23:41:11 +05:30
1 parent 2527d429bc
commit b6962b9ab2
9 files changed
+222 -330

No files matched your search

-11
View File
@@ -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",
-2
View File
@@ -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"
-15
View File
@@ -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 () => {
+2 -144
View File
@@ -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 = () => {
</IconButton>
</Box>
<Box sx={{ display: "flex", gap: 2 }}>
<AnimatePresence mode="wait">
{syncLoading ? (
<Box
key={`laoding`}
component={motion.div}
position="relative"
display="inline-flex"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{
opacity: 0,
}}
transition={{
duration: 0.2,
}}
>
<CircularProgress
sx={{
opacity: syncLoading
? 1
: 0,
}}
/>
<IconButton
sx={{
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<MdiCogSync />
</IconButton>
</Box>
) : syncSuccess ? (
<Box
key={`success`}
component={motion.div}
position="relative"
display="inline-flex"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{
opacity: 0,
transition: {
delay: 0.4,
},
}}
transition={{
duration: 0.2,
}}
sx={{
background: green[500],
borderRadius: "1000px",
}}
>
<CircularProgress
sx={{
opacity: syncLoading
? 1
: 0,
}}
/>
<IconButton
sx={{
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<MdiCheck />
</IconButton>
</Box>
) : syncError ? (
<Box
key={`error`}
component={motion.div}
position="relative"
display="inline-flex"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{
opacity: 0,
transition: {
delay: 0.4,
},
}}
transition={{
duration: 0.2,
}}
sx={{
background: red[900],
borderRadius: "1000px",
}}
>
<CircularProgress
sx={{
opacity: syncLoading
? 1
: 0,
}}
/>
<IconButton
sx={{
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<MdiAlertDecagram />
</IconButton>
</Box>
) : (
<></>
)}
</AnimatePresence>
<IconButton>
<IconButton disabled>
<MdiHeartOutline />
</IconButton>
<IconButton
+47 -70
View File
@@ -1,6 +1,8 @@
/** @format */
import PropTypes from "prop-types";
import { useEffect } from "react";
import IconButton from "@mui/material/IconButton";
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api";
@@ -9,19 +11,14 @@ import { pink } from "@mui/material/colors";
import { MdiHeart } from "../icons/mdiHeart";
import { MdiHeartOutline } from "../icons/mdiHeartOutline";
import { useSnackbar } from "notistack";
import { useAppLoadingStore } from "../../utils/store/appLoading";
import CircularProgress from "@mui/material/CircularProgress";
const LikeButton = ({ itemId, isFavorite, queryKey, userId, itemName }) => {
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 (
<IconButton
onClick={(e) => {
e.stopPropagation();
mutation.mutate();
<div
style={{
transition: "opacity 250ms",
position: "relative",
display: "inline-flex",
}}
disabled={mutation.isLoading}
>
{isFavorite ? (
<MdiHeart sx={{ color: pink[700] }} />
) : (
<MdiHeartOutline />
)}
</IconButton>
<CircularProgress
style={{
opacity: mutation.isLoading ? 1 : 0,
transition: "opacity 200ms",
}}
disableShrink
/>
<IconButton
onClick={(e) => {
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 ? (
<MdiHeart sx={{ color: pink[700] }} />
) : (
<MdiHeartOutline />
)}
</IconButton>
</div>
);
};
+46 -62
View File
@@ -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 (
<IconButton
onClick={(e) => {
e.stopPropagation();
mutation.mutate();
<div
style={{
transition: "opacity 250ms",
position: "relative",
display: "inline-flex",
}}
disabled={mutation.isLoading}
>
<MdiCheck
sx={{
color: isPlayed ? green[400] : "white",
<CircularProgress
style={{
opacity: mutation.isLoading ? 1 : 0,
transition: "opacity 200ms",
}}
disableShrink
/>
</IconButton>
<IconButton
onClick={(e) => {
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,
}}
>
<MdiCheck
sx={{
color: isPlayed ? green[400] : "white",
}}
/>
</IconButton>
</div>
);
};
+1 -1
View File
@@ -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,
+126 -11
View File
@@ -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 }) => {
/>
</div>
<div className="item-detail-hero-info-container">
<div className="item-name">
<Typography
variant="h3"
noWrap={true}
fontWeight={600}
<div>
<div className="item-name">
<Typography
variant="h3"
noWrap={true}
fontWeight={600}
>
{item.Name}
</Typography>
</div>
<Stack
direction="row"
gap={1}
divider={
<div
style={{
width: "4px",
height: "4px",
background: "white",
alignSelf: "center",
aspectRatio: 1,
borderRadius: "10px",
}}
></div>
}
className="hero-carousel-info"
mt={1}
>
{item.Name}
<Typography
style={{ opacity: "0.7" }}
variant="subtitle1"
>
{!!item.ProductionYear
? item.ProductionYear
: "Unknown"}
</Typography>
<Chip
variant="filled"
label={item.OfficialRating ?? "Not Rated"}
/>
<div
style={{
display: "flex",
gap: "0.25em",
alignItems: "center",
}}
className="hero-carousel-info-rating"
>
{!!item.CommunityRating ? (
<>
<MdiStar
sx={{
color: yellow[700],
}}
/>
<Typography
style={{ opacity: "0.7" }}
variant="subtitle1"
>
{Math.round(
item.CommunityRating *
10,
) / 10}
</Typography>
</>
) : (
<Typography
style={{ opacity: "0.7" }}
variant="subtitle1"
>
No Community Rating
</Typography>
)}
</div>
{!!item.RunTimeTicks && (
<Typography
style={{ opacity: "0.7" }}
variant="subtitle1"
>
{getRuntime(item.RunTimeTicks)}
</Typography>
)}
{!!item.RunTimeTicks && (
<Typography
style={{ opacity: "0.7" }}
variant="subtitle1"
>
{endsAt(item.RunTimeTicks)}
</Typography>
)}
</Stack>
<Typography
style={{ opacity: "0.7" }}
variant="subtitle1"
mt={1}
>
{item.Genres.join(", ")}
</Typography>
</div>
<div
style={{
margin: "2em 0",
}}
className="item-button-container"
>
<LikeButton
queryKey={["item", item.Id]}
itemId={item.Id}
itemName={item.Name}
isFavorite={item.UserData?.IsFavorite}
userId={userId}
/>
<MarkPlayedButton
queryKey={["item", item.Id]}
itemId={item.Id}
itemName={item.Name}
isPlayed={item.UserData?.Played}
userId={userId}
/>
</div>
<div
style={{
marginTop: "1em",
}}
>
<Typography
variant="subtitle1"
style={{ opacity: "0.7" }}
>
<Typography variant="subtitle1">
{item.Overview}
</Typography>
</div>
-14
View File
@@ -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 })),
}));