Skip to content

Commit

Permalink
chore: put page in redux store (#541)
Browse files Browse the repository at this point in the history
* chore: put page in redux store

* chore: put page in redux store
  • Loading branch information
jy95 authored Aug 25, 2024
1 parent 66ebb9a commit f2c41bf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
14 changes: 6 additions & 8 deletions src/app/[locale]/games/_client/GamesGalleryGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Hooks
import { useState } from 'react';
import { useGetGamesQuery } from "@/redux/services/gamesAPI";
import { useAppSelector } from "@/redux/hooks";
import { useAppSelector, useAppDispatch } from "@/redux/hooks";
import { nextPage } from "@/redux/features/gamesSlice";
import { useTranslations } from 'next-intl';

// Style
Expand All @@ -28,9 +28,10 @@ function GamesGalleryGridInner() {

// Active filters
const activeFilters = useAppSelector((state) => state.games.activeFilters);

const [page, setPage] = useState(1);
// Current page
const page = useAppSelector((state) => state.games.page);
const t = useTranslations('common');
const dispatch = useAppDispatch();

const LIMIT_PAGE = 16;

Expand All @@ -40,9 +41,6 @@ function GamesGalleryGridInner() {
filters: activeFilters,
pageSize : LIMIT_PAGE,
page: page
},
{
refetchOnMountOrArgChange: true
}
);
const allGames = data?.items ?? [];
Expand Down Expand Up @@ -76,7 +74,7 @@ function GamesGalleryGridInner() {
<LoadingButton
loading={isFetching}
disabled={ page >= (data?.total_pages || 1) }
onClick={() => setPage((prev) => prev + 1)}
onClick={() => dispatch(nextPage())}
>
<span>{t('loadMore')}</span>
</LoadingButton>
Expand Down
15 changes: 12 additions & 3 deletions src/redux/features/gamesSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ export type gamesFilters = ({

export interface GamesState {
/** @description current filters applied */
activeFilters: gamesFilters
activeFilters: gamesFilters,
/** @description current page loaded */
page: number
}

const initialState: GamesState = {
activeFilters: []
activeFilters: [],
page: 1
};

const gamesSlice = createSlice({
Expand All @@ -42,6 +45,7 @@ const gamesSlice = createSlice({
});
}
state.activeFilters = newFilters;
state.page = 1;
},
filterByTitle(state : GamesState, action: PayloadAction<string>) {
// If empty, remove filter - if not, add it
Expand All @@ -53,6 +57,7 @@ const gamesSlice = createSlice({
});
}
state.activeFilters = newFilters;
state.page = 1;
},
filterByPlatform(state : GamesState, action: PayloadAction<number | undefined>) {
// If empty, remove filter - if not, add it
Expand All @@ -64,6 +69,10 @@ const gamesSlice = createSlice({
});
}
state.activeFilters = newFilters;
state.page = 1;
},
nextPage(state: GamesState) {
state.page = state.page + 1;
}
}
});
Expand Down Expand Up @@ -117,5 +126,5 @@ export const selectSelectedTitle = createSelector(
)

// Action creators are generated for each case reducer function
export const { filteringByGenre, filterByTitle, filterByPlatform } = gamesSlice.actions;
export const { filteringByGenre, filterByTitle, filterByPlatform, nextPage } = gamesSlice.actions;
export default gamesSlice.reducer;
8 changes: 4 additions & 4 deletions src/redux/services/gamesAPI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const gamesAPI = createApi({
return `/games?${stringifyObject(parameters)}`;
},
forceRefetch: ({ currentArg, previousArg }) => {
return currentArg !== previousArg;
return JSON.stringify(currentArg) !== JSON.stringify(previousArg);
},
// Custom key for cache
serializeQueryArgs: ({ endpointName }) => endpointName,
Expand All @@ -72,17 +72,17 @@ export const gamesAPI = createApi({
let currentParams = { filters: currentCache.filters, pageSize: currentCache.pageSize };
let newParams = { filters: newItems.filters, pageSize: newItems.pageSize };
let notSameParams = JSON.stringify(currentParams) !== JSON.stringify(newParams);
let samePageAsked = currentCache.page === newItems.page;

// If not same parameters (or same page), it means we have to replace by newest answer
if (notSameParams || samePageAsked) {
// If not same parameters, it means we have to replace by newest answer
if (notSameParams) {
return newItems;
}

// If same parameters, it means we have to merge them
return {
...currentCache,
items: [...currentCache.items, ...newItems.items],
page: newItems.page
};
},
})
Expand Down

0 comments on commit f2c41bf

Please sign in to comment.