mirror of
https://github.com/cpvalente/ontime.git
synced 2026-09-04 22:09:10 +00:00
View Params Persist when Navigating Back (#581)
* add url search params to local storage * add clearing local storage * use `useLocation` instead of `location.pathname`
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { FormEvent, useEffect } from 'react';
|
import { FormEvent, useEffect } from 'react';
|
||||||
import { useSearchParams } from 'react-router-dom';
|
import { useLocation, useSearchParams } from 'react-router-dom';
|
||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
Drawer,
|
Drawer,
|
||||||
@@ -12,11 +12,27 @@ import {
|
|||||||
useDisclosure,
|
useDisclosure,
|
||||||
} from '@chakra-ui/react';
|
} from '@chakra-ui/react';
|
||||||
|
|
||||||
|
import { useLocalStorage } from '../../../common/hooks/useLocalStorage';
|
||||||
|
|
||||||
import ParamInput from './ParamInput';
|
import ParamInput from './ParamInput';
|
||||||
import { ParamField } from './types';
|
import { ParamField } from './types';
|
||||||
|
|
||||||
import style from './ViewParamsEditor.module.scss';
|
import style from './ViewParamsEditor.module.scss';
|
||||||
|
|
||||||
|
type ViewParamsObj = { [key: string]: string | FormDataEntryValue };
|
||||||
|
type SavedViewParams = Record<string, ViewParamsObj>;
|
||||||
|
|
||||||
|
const getURLSearchParamsFromObj = (paramsObj: ViewParamsObj) =>
|
||||||
|
Object.entries(paramsObj).reduce((newSearchParams, [id, value]) => {
|
||||||
|
if (typeof value === 'string' && value.length) {
|
||||||
|
newSearchParams.set(id, value);
|
||||||
|
|
||||||
|
return newSearchParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
return newSearchParams;
|
||||||
|
}, new URLSearchParams());
|
||||||
|
|
||||||
interface EditFormDrawerProps {
|
interface EditFormDrawerProps {
|
||||||
paramFields: ParamField[];
|
paramFields: ParamField[];
|
||||||
}
|
}
|
||||||
@@ -24,6 +40,8 @@ interface EditFormDrawerProps {
|
|||||||
export default function ViewParamsEditor({ paramFields }: EditFormDrawerProps) {
|
export default function ViewParamsEditor({ paramFields }: EditFormDrawerProps) {
|
||||||
const [searchParams, setSearchParams] = useSearchParams();
|
const [searchParams, setSearchParams] = useSearchParams();
|
||||||
const { isOpen, onClose, onOpen } = useDisclosure();
|
const { isOpen, onClose, onOpen } = useDisclosure();
|
||||||
|
const { pathname } = useLocation();
|
||||||
|
const [storedViewParams, setStoredViewParams] = useLocalStorage<SavedViewParams>('ontime-views', {});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const isEditing = searchParams.get('edit');
|
const isEditing = searchParams.get('edit');
|
||||||
@@ -33,6 +51,19 @@ export default function ViewParamsEditor({ paramFields }: EditFormDrawerProps) {
|
|||||||
}
|
}
|
||||||
}, [searchParams, onOpen]);
|
}, [searchParams, onOpen]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const viewParamsObjFromLocalStorage = storedViewParams[pathname];
|
||||||
|
|
||||||
|
if (viewParamsObjFromLocalStorage !== undefined) {
|
||||||
|
const defaultSearchParams = getURLSearchParamsFromObj(viewParamsObjFromLocalStorage);
|
||||||
|
setSearchParams(defaultSearchParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
// linter is asking for `setSearchParams` in the useEffect deps
|
||||||
|
// rule is disabled since adding `setSearchParams` results in unnecessary re-renders
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [storedViewParams, pathname]);
|
||||||
|
|
||||||
const onEditDrawerClose = () => {
|
const onEditDrawerClose = () => {
|
||||||
onClose();
|
onClose();
|
||||||
|
|
||||||
@@ -41,6 +72,7 @@ export default function ViewParamsEditor({ paramFields }: EditFormDrawerProps) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const clearParams = () => {
|
const clearParams = () => {
|
||||||
|
setStoredViewParams({ ...storedViewParams, [pathname]: {} });
|
||||||
setSearchParams();
|
setSearchParams();
|
||||||
onClose();
|
onClose();
|
||||||
};
|
};
|
||||||
@@ -49,15 +81,9 @@ export default function ViewParamsEditor({ paramFields }: EditFormDrawerProps) {
|
|||||||
formEvent.preventDefault();
|
formEvent.preventDefault();
|
||||||
|
|
||||||
const newParamsObject = Object.fromEntries(new FormData(formEvent.currentTarget));
|
const newParamsObject = Object.fromEntries(new FormData(formEvent.currentTarget));
|
||||||
const newSearchParams = Object.entries(newParamsObject).reduce((newSearchParams, [id, value]) => {
|
const newSearchParams = getURLSearchParamsFromObj(newParamsObject);
|
||||||
if (typeof value === 'string' && value.length) {
|
|
||||||
newSearchParams.set(id, value);
|
|
||||||
|
|
||||||
return newSearchParams;
|
setStoredViewParams({ ...storedViewParams, [pathname]: newParamsObject });
|
||||||
}
|
|
||||||
|
|
||||||
return newSearchParams;
|
|
||||||
}, new URLSearchParams());
|
|
||||||
setSearchParams(newSearchParams);
|
setSearchParams(newSearchParams);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user