Skip to content

Commit

Permalink
Merge pull request #160 from simonv3/favorite-tracks-state
Browse files Browse the repository at this point in the history
fix: trigger favorite update across active views of favorite tracks
  • Loading branch information
simonv3 authored Jun 9, 2022
2 parents 847d74d + 9a68001 commit d21c710
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
30 changes: 28 additions & 2 deletions src/components/common/FavoriteTrack.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { css } from "@emotion/css";
import { useGlobalStateContext } from "contexts/globalState";
import React from "react";
import { FaStar, FaRegStar } from "react-icons/fa";
import { addTrackToUserFavorites } from "../../services/Api";
import {
addTrackToUserFavorites,
checkTrackIdsForFavorite,
} from "../../services/Api";
import IconButton from "./IconButton";

export const spinner = css`
Expand Down Expand Up @@ -30,6 +34,11 @@ export const SpinningStar: React.FC<{ spinning: boolean; full: boolean }> = ({
export const FavoriteTrack: React.FC<{ track: TrackWithUserCounts }> = ({
track,
}) => {
const {
state: { checkFavoriteStatusFlag },
dispatch,
} = useGlobalStateContext();

const [isFavorite, setIsFavorite] = React.useState(track.favorite);
const [loadingFavorite, setLoadingFavorite] = React.useState(false);

Expand All @@ -42,9 +51,26 @@ export const FavoriteTrack: React.FC<{ track: TrackWithUserCounts }> = ({
await addTrackToUserFavorites(track.id);
setIsFavorite((val) => !val);
setLoadingFavorite(false);
dispatch({ type: "incrementFavoriteStatusFlag" });
},
[track.id]
[track.id, dispatch]
);

const onFavoriteStatusFlagChange = React.useCallback(async () => {
const resolution = await checkTrackIdsForFavorite([track.id]);
if (resolution.length > 0 && resolution[0].track_id === track.id) {
setIsFavorite(true);
} else {
setIsFavorite(false);
}
}, [track]);

React.useEffect(() => {
if (checkFavoriteStatusFlag) {
onFavoriteStatusFlagChange();
}
}, [checkFavoriteStatusFlag, onFavoriteStatusFlagChange]);

return (
<IconButton compact onClick={onClickStar}>
<SpinningStar spinning={loadingFavorite} full={isFavorite} />
Expand Down
19 changes: 17 additions & 2 deletions src/contexts/globalState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ export interface GlobalState {
draggingTrackId?: number;
currentlyPlayingIndex?: number;
userPlaylists?: { id: string; title: string }[];
checkFavoriteStatusFlag?: number;
}

type SetLoggedInUser = {
type: "setLoggedInUser";
user?: LoggedInUser;
};

type IncrementFavoriteStatusFlag = {
type: "incrementFavoriteStatusFlag";
};

type ClearQueue = {
type: "clearQueue";
};
Expand Down Expand Up @@ -112,7 +117,8 @@ type Actions =
| IncrementCurrentlyPlayingIndex
| DecrementCurrentlyPlayingIndex
| SetUserCredits
| SetUserPlaylists;
| SetUserPlaylists
| IncrementFavoriteStatusFlag;

export const stateReducer = produce((draft: GlobalState, action: Actions) => {
switch (action.type) {
Expand Down Expand Up @@ -217,12 +223,20 @@ export const stateReducer = produce((draft: GlobalState, action: Actions) => {
case "setUserPlaylists":
draft.userPlaylists = action.playlists;
break;
case "incrementFavoriteStatusFlag":
draft.checkFavoriteStatusFlag = (draft.checkFavoriteStatusFlag ?? 0) + 1;
break;
default:
break;
}
localStorage.setItem(
"state",
JSON.stringify({ ...draft, playing: undefined }) // We don't want playing to be persisted
JSON.stringify({
...draft,
// We don't want these to be persisted
checkFavoriteStatusFlag: undefined,
playing: undefined,
})
);
return draft;
});
Expand All @@ -247,6 +261,7 @@ export const GlobalStateProvider: React.FC<GlobalStateProviderProps> = ({
storedState.playerQueueIds = [];
}
} catch (e) {}

const [state, dispatch] = React.useReducer(
stateReducer,
storedState ?? { playerQueueIds: [] }
Expand Down

0 comments on commit d21c710

Please sign in to comment.