Skip to content

Commit

Permalink
chore: refactor to accept external user data and avoid making the cal…
Browse files Browse the repository at this point in the history
…l internally EB-832
  • Loading branch information
Orion Wolf-Hubbard committed Jan 8, 2025
1 parent 4282cc9 commit 32b7285
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/favorites/src/lib/Favorites.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Favorite } from './utils';
import { useFavoritesQuery, useSubmitFavorites, sendUpdateMessage, openMaxModal } from './utils';
import { AV_INTERNAL_GLOBALS, MAX_FAVORITES } from './constants';

type StatusUnion = 'idle' | 'error' | 'loading' | 'success';
type StatusUnion = 'idle' | 'error' | 'loading' | 'reloading' | 'success';

type FavoritesContextType = {
favorites?: Favorite[];
Expand All @@ -21,14 +21,21 @@ const FavoritesContext = createContext<FavoritesContextType | null>(null);
export const FavoritesProvider = ({
children,
onFavoritesChange,
settingsFavorites,
settingsStatus,
}: {
children: React.ReactNode;
onFavoritesChange?: (favorites: Favorite[]) => void;
settingsFavorites?: Favorite[];
settingsStatus?: StatusUnion;
}): JSX.Element => {
const [lastClickedFavoriteId, setLastClickedFavoriteId] = useState<string>('');

const queryClient = useQueryClient();
const { data: favorites, status: queryStatus } = useFavoritesQuery();
const { data: favorites, status: queryStatus } = settingsStatus ? {
data: settingsFavorites,
status: settingsStatus,
} : useFavoritesQuery();

const { submitFavorites, status: mutationStatus } = useSubmitFavorites({
onMutationStart(targetFavoriteId) {
Expand Down Expand Up @@ -155,6 +162,7 @@ export const useFavorites = (

let status: MergedStatusUnion = 'initLoading';
if (queryStatus === 'loading') status = 'initLoading';
if (queryStatus === 'reloading') status = 'reloading';
if (mutationStatus === 'loading') status = 'reloading';
if (queryStatus === 'error' || mutationStatus === 'error') status = 'error';
if (queryStatus === 'success' && (mutationStatus === 'success' || mutationStatus === 'idle')) status = 'success';
Expand Down

0 comments on commit 32b7285

Please sign in to comment.