Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rI-548: fix(client): 🐛 Fix handling of initial user favorites fetch #2517

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading