Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | 27x 2601x 2601x 958x 958x 958x 958x 958x 958x 958x 958x 958x 41x 41x 958x 685x 685x 685x 685x 664x 664x 664x 685x 44x 44x 44x 958x 958x 958x 958x 958x 1152x 1152x 41x 1152x 958x 958x | import { useCallback, useEffect, useMemo, useState } from 'react';
import { useLocation, useHistory } from 'react-router-dom';
import queryString from 'query-string';
import {
DEFAULT_PAGINATION_SIZE,
DEFAULT_PAGE_KEY,
MCL_NEED_MORE_DATA_PREV_NEXT_ARG_INDEX,
NEXT,
PREV
} from '../constants/pagination';
import useLocalPageStore from './useLocalPageStore';
// Currently there are several places in which this hook is used twice within the same component
// Once in order to get the current page, and again in order to handle changes to paginations
// This hook should be refactored in order to resolve these issue - @EthanFreestone
const usePrevNextPagination = ({
count = 0, // Only needed for reading back MCL props
defaultToPageOne = true, // A prop to allow the implementor to turn off the defaulting to page=1
pageSize = DEFAULT_PAGINATION_SIZE, // Only needed for reading back MCL props
id = DEFAULT_PAGE_KEY, // This id is ONLY used for syncToLocation: false cases, as a key to the zustand store
syncToLocation = true, // Used to turn on/off location syncing, so can be used as standalone state if required,
hasNextPage = null // Override for canGoNext, used in the case in which resources are fetched with no stats
} = {}) => {
/* ------ ZUSTAND STORE ------ */
// For NON-SYNC-TO-LOCATION use cases, store the currentPage in a keyed store
const pageStore = useLocalPageStore(state => state.pageStore);
const setPage = useLocalPageStore(state => state.setPage);
/* ------ CURRENTPAGE STATE ------ */
// Set up initialValue
const getInitialCurrentPage = useCallback(() => {
let initialCurrentPage;
Iif (!syncToLocation) {
if (pageStore[id]) {
initialCurrentPage = pageStore[id];
} else {
// Initialise store state
setPage(id, 1);
initialCurrentPage = 1;
}
}
return initialCurrentPage;
}, [id, pageStore, setPage, syncToLocation]);
// State itself
const [currentPage, setCurrentPage] = useState(getInitialCurrentPage());
const location = useLocation();
const history = useHistory();
/* ------ HANDLEPAGECHANGE ------ */
// Takes in a direction "prev" or "next" and performs the requisite logic to move
// currentPage state and/or zustand store state
const handlePageChange = useCallback((direction) => {
const urlQuery = queryString.parse(location.search);
let newPage;
if (direction === NEXT) {
newPage = currentPage + 1;
} else if (direction === PREV) {
newPage = currentPage - 1;
}
if (!syncToLocation) {
// We're manipulating the state directly in this case
// We're dealing with the zustand store in this case,
// change the store and the currentPage will update below
setPage(id, newPage);
setCurrentPage(newPage);
} else if (newPage !== urlQuery?.page) {
const newQuery = {
...urlQuery,
page: newPage
};
history.push({
pathname: location.pathname,
search: `?${queryString.stringify(newQuery)}`
});
}
}, [
currentPage,
history,
id,
location.pathname,
location.search,
setPage,
syncToLocation
]);
const [resetPageState, setResetPageState] = useState(false);
const resetPage = useCallback(() => {
if (syncToLocation) {
setResetPageState(true);
} else E{
setPage(id, 1);
setCurrentPage(1);
}
}, [id, setPage, syncToLocation]);
useEffect(() => {
if (syncToLocation) {
const urlQuery = queryString.parse(location.search);
Iif (urlQuery?.page && currentPage !== urlQuery?.page) {
setCurrentPage(Number(urlQuery?.page));
} else if (!urlQuery?.page && defaultToPageOne) {
// If url query "page" is not yet set, set it to 1
setCurrentPage(1);
const newQuery = {
...urlQuery,
page: 1
};
// We use replace instead of push for the defaulting so we don't add unnecessary history entries
history.replace({
pathname: location.pathname,
search: `?${queryString.stringify(newQuery)}`
});
}
if (resetPageState) {
const newQuery = {
...urlQuery,
page: 1
};
history.push({
pathname: location.pathname,
search: `?${queryString.stringify(newQuery)}`
});
setResetPageState(false);
}
} else Eif (currentPage !== pageStore[id]) {
// Only do this when not syncing to location...
// If current page state is not what we have in the store, set current page state
setCurrentPage(pageStore[id]);
}
}, [
currentPage,
defaultToPageOne,
history,
id,
location,
pageStore,
resetPageState,
syncToLocation
]);
// Set up MCL specific props based on page
const pagingCanGoNext = hasNextPage ?? (currentPage && (currentPage < Number(count) / pageSize));
const pagingCanGoPrevious = currentPage && Number(currentPage) > 1;
const pagingOffset = currentPage ? (currentPage - 1) * pageSize : 0;
const onNeedMoreData = (...args) => {
if (args[MCL_NEED_MORE_DATA_PREV_NEXT_ARG_INDEX]) {
handlePageChange(args[MCL_NEED_MORE_DATA_PREV_NEXT_ARG_INDEX]);
}
};
// Set up SASQ callback handling
// If extras are needed, these can be set up
// manually using resetPage per SASQ
const queryStateReducer = useCallback((_currState, nextState) => {
const resetPageEvents = [
'clear.all',
'reset.all',
'filter.state',
'filter.clearGroup',
'sort.change',
'search.reset',
'search.submit'
];
if (resetPageEvents.includes(nextState.changeType)) {
resetPage();
}
return nextState;
}, [resetPage]);
const paginationSASQProps = useMemo(() => ({
queryStateReducer
}), [queryStateReducer]);
return ({
currentPage,
handlePageChange,
paginationMCLProps: {
onNeedMoreData,
pagingCanGoNext,
pagingCanGoPrevious,
pagingOffset,
pagingType: 'prev-next'
},
paginationSASQProps,
queryStateReducer,
resetPage,
});
};
export default usePrevNextPagination;
|