Skip to content

Commit

Permalink
Merge pull request #2517 from refugies-info/luis/ri-548-des-tres-nomb…
Browse files Browse the repository at this point in the history
…reux-appels-a-la-fetchuserfavorites-saga

rI-548: fix(client): 🐛 Fix handling of initial user favorites fetch
  • Loading branch information
kaaloo authored Dec 13, 2024
2 parents a6c8f4a + 3f2f94a commit 51e5255
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/client/src/__fixtures__/reduxStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const initialMockTranslationState = {
};

const initialUserFavoritesState: UserFavoritesState = {
favorites: [],
favorites: null,
};

const initialMockSearchReults: SearchResultsState = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const UserFavorites = (props: Props) => {
const isLoadingUpdate = useSelector(isLoadingSelector(LoadingStatusKey.UPDATE_USER_FAVORITES));
const favorites = useSelector(userFavoritesSelector);
const isLoading = useMemo(
() => (isLoadingFetch || isLoadingUpdate) && favorites.length === 0,
[isLoadingFetch, isLoadingUpdate, favorites.length],
() => (isLoadingFetch || isLoadingUpdate) && favorites === null,
[isLoadingFetch, isLoadingUpdate, favorites],
);

const [showDeleteToast, setShowDeleteToast] = useState(false);
Expand All @@ -65,7 +65,7 @@ const UserFavorites = (props: Props) => {
<>
{isLoading ? (
<FavoritesLoading t={t} />
) : favorites.length === 0 ? (
) : favorites === null || favorites.length === 0 ? (
<NoFavorites t={t} toggleTutoModal={toggleTutoModal} />
) : (
<>
Expand Down
4 changes: 2 additions & 2 deletions apps/client/src/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ const Layout = (props: Props) => {
const isUserFavoritesLoading = useSelector(isLoadingSelector(LoadingStatusKey.FETCH_USER_FAVORITES));
const hasUserFavoritesError = useSelector(hasErroredSelector(LoadingStatusKey.FETCH_USER_FAVORITES));
useEffect(() => {
if (user && userFavorites.length === 0 && !isUserFavoritesLoading && !hasUserFavoritesError) {
if (user && userFavorites === null && !isUserFavoritesLoading && !hasUserFavoritesError) {
dispatch(fetchUserFavoritesActionCreator(router.locale || "fr"));
}
}, [user, userFavorites.length, isUserFavoritesLoading, hasUserFavoritesError, dispatch, router.locale]);
}, [user, userFavorites, isUserFavoritesLoading, hasUserFavoritesError, dispatch, router.locale]);

const computeFullSentence = (nodeList: any) => {
let sentence = "";
Expand Down
5 changes: 4 additions & 1 deletion apps/client/src/hooks/useFavorites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ const useFavorites = (contentId: Id | null) => {
const { isAuth } = useAuth();

// Memoized computation of whether the current content is favorited
const isFavorite = useMemo(() => isContentFavorite(favorites, contentId), [favorites, contentId]);
const isFavorite = useMemo(
() => favorites !== null && isContentFavorite(favorites, contentId),
[favorites, contentId],
);

// Callback to refresh favorites after successful API operations
const successCallback = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { createReducer } from "typesafe-actions";
import { UserFavoritesActions } from "./UserFavoritesInLocale.actions";

export type UserFavoritesState = {
favorites: GetUserFavoritesResponse[];
favorites: GetUserFavoritesResponse[] | null;
};

const initialUserFavoritesState: UserFavoritesState = {
favorites: [],
favorites: null,
};

export const userFavoritesReducer = createReducer<UserFavoritesState, UserFavoritesActions>(initialUserFavoritesState, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GetUserFavoritesResponse } from "@refugies-info/api-types";
import { RootState } from "../rootReducer";

export const userFavoritesSelector = (state: RootState): GetUserFavoritesResponse[] => state.userFavorites.favorites;
export const userFavoritesSelector = (state: RootState): GetUserFavoritesResponse[] | null =>
state.userFavorites.favorites;

0 comments on commit 51e5255

Please sign in to comment.