Skip to content

Commit

Permalink
Reorder variable and hook definitions
Browse files Browse the repository at this point in the history
Allow for better reading flow where variables and hooks are defined at
the top.
  • Loading branch information
bjoernricks committed Jun 7, 2024
1 parent 3786b01 commit 5f0acf8
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions src/web/hooks/usePageFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,40 @@ const useDefaultFilter = pageName =>
*/
const usePageFilter = (
pageName,
{fallbackFilter, locationQueryFilterString} = {},
{
fallbackFilter,
locationQueryFilterString: initialLocationQueryFilterString,
} = {},
) => {
const gmp = useGmp();
const dispatch = useDispatch();
const location = useLocation();
const history = useHistory();
const [defaultSettingFilter, defaultSettingsFilterError] =
useDefaultFilter(pageName);
let [rowsPerPage, rowsPerPageError] = useShallowEqualSelector(state => {
const userSettingDefaultSel = getUserSettingsDefaults(state);
return [
userSettingDefaultSel.getValueByName('rowsperpage'),
userSettingDefaultSel.getError(),
];
});
const pageFilter = useShallowEqualSelector(state =>
getPage(state).getFilter(pageName),
);

// only use location directly if locationQueryFilterString is undefined
// use null as value for not set at all
if (!isDefined(locationQueryFilterString)) {
locationQueryFilterString = location?.query?.filter;
}

let returnedFilter;
// only use location directly if locationQueryFilterString is undefined
const locationQueryFilterString = initialLocationQueryFilterString
? initialLocationQueryFilterString
: location?.query?.filter;

const [defaultSettingFilter, defaultSettingsFilterError] =
useDefaultFilter(pageName);
const [locationQueryFilter, setLocationQueryFilter] = useState(
hasValue(locationQueryFilterString)
? Filter.fromString(locationQueryFilterString)
: undefined,
);

useEffect(() => {
if (
Expand All @@ -86,26 +103,12 @@ const usePageFilter = (
pageName,
]);

let [rowsPerPage, rowsPerPageError] = useShallowEqualSelector(state => {
const userSettingDefaultSel = getUserSettingsDefaults(state);
return [
userSettingDefaultSel.getValueByName('rowsperpage'),
userSettingDefaultSel.getError(),
];
});

useEffect(() => {
if (!isDefined(rowsPerPage)) {
dispatch(loadUserSettingDefault(gmp)(ROWS_PER_PAGE_SETTING_ID));
}
}, [returnedFilter, rowsPerPage, gmp, dispatch]);

const [locationQueryFilter, setLocationQueryFilter] = useState(
hasValue(locationQueryFilterString)
? Filter.fromString(locationQueryFilterString)
: undefined,
);

useEffect(() => {
if (hasValue(locationQueryFilterString)) {
dispatch(
Expand All @@ -115,10 +118,6 @@ const usePageFilter = (
setLocationQueryFilter(undefined);
}, []); // eslint-disable-line react-hooks/exhaustive-deps

const pageFilter = useShallowEqualSelector(state =>
getPage(state).getFilter(pageName),
);

if (hasValue(locationQueryFilter)) {
returnedFilter = locationQueryFilter;
} else if (isDefined(pageFilter)) {
Expand Down

0 comments on commit 5f0acf8

Please sign in to comment.