mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-31 20:09:11 +00:00
fix: maintain lock on param changes
This commit is contained in:
committed by
Carlos Valente
parent
aecf9575b1
commit
0f83ceaf01
@@ -11,7 +11,7 @@ import IconButton from '../buttons/IconButton';
|
|||||||
import Info from '../info/Info';
|
import Info from '../info/Info';
|
||||||
|
|
||||||
import { ViewOption } from './viewParams.types';
|
import { ViewOption } from './viewParams.types';
|
||||||
import { getURLSearchParamsFromObj } from './viewParams.utils';
|
import { getPreservedSearchParams, getURLSearchParamsFromObj } from './viewParams.utils';
|
||||||
import { useViewParamsEditorStore } from './viewParamsEditor.store';
|
import { useViewParamsEditorStore } from './viewParamsEditor.store';
|
||||||
import { ViewParamsPresets } from './ViewParamsPresets';
|
import { ViewParamsPresets } from './ViewParamsPresets';
|
||||||
import ViewParamsSection from './ViewParamsSection';
|
import ViewParamsSection from './ViewParamsSection';
|
||||||
@@ -25,17 +25,19 @@ interface EditFormDrawerProps {
|
|||||||
|
|
||||||
export default memo(ViewParamsEditor);
|
export default memo(ViewParamsEditor);
|
||||||
function ViewParamsEditor({ target, viewOptions }: EditFormDrawerProps) {
|
function ViewParamsEditor({ target, viewOptions }: EditFormDrawerProps) {
|
||||||
const [_, setSearchParams] = useSearchParams();
|
const [searchParams, setSearchParams] = useSearchParams();
|
||||||
const { data: viewSettings } = useViewSettings();
|
const { data: viewSettings } = useViewSettings();
|
||||||
const { isOpen, close } = useViewParamsEditorStore();
|
const { isOpen, close } = useViewParamsEditorStore();
|
||||||
const isSmallScreen = useIsSmallScreen();
|
const isSmallScreen = useIsSmallScreen();
|
||||||
|
|
||||||
|
const getPreservedParams = () => getPreservedSearchParams(searchParams, viewOptions);
|
||||||
|
|
||||||
const handleClose = () => {
|
const handleClose = () => {
|
||||||
close();
|
close();
|
||||||
};
|
};
|
||||||
|
|
||||||
const resetParams = () => {
|
const resetParams = () => {
|
||||||
setSearchParams();
|
setSearchParams(getPreservedParams());
|
||||||
};
|
};
|
||||||
|
|
||||||
const onParamsFormSubmit = (formEvent: FormEvent<HTMLFormElement>) => {
|
const onParamsFormSubmit = (formEvent: FormEvent<HTMLFormElement>) => {
|
||||||
@@ -43,6 +45,10 @@ function ViewParamsEditor({ target, viewOptions }: EditFormDrawerProps) {
|
|||||||
|
|
||||||
const newParamsObject = Object.fromEntries(new FormData(formEvent.currentTarget));
|
const newParamsObject = Object.fromEntries(new FormData(formEvent.currentTarget));
|
||||||
const newSearchParams = getURLSearchParamsFromObj(newParamsObject, viewOptions);
|
const newSearchParams = getURLSearchParamsFromObj(newParamsObject, viewOptions);
|
||||||
|
const preservedParams = getPreservedParams();
|
||||||
|
preservedParams.forEach((value, key) => {
|
||||||
|
newSearchParams.append(key, value);
|
||||||
|
});
|
||||||
setSearchParams(newSearchParams);
|
setSearchParams(newSearchParams);
|
||||||
|
|
||||||
if (isSmallScreen) {
|
if (isSmallScreen) {
|
||||||
|
|||||||
@@ -191,3 +191,26 @@ export function getURLSearchParamsFromObj(paramsObj: ViewParamsObj, paramFields:
|
|||||||
|
|
||||||
return newSearchParams;
|
return newSearchParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extracts search params that are not managed by the view params editor
|
||||||
|
* @param currentParams - The current URL search params
|
||||||
|
* @param paramFields - The view options that define the managed parameters
|
||||||
|
* @returns A new URLSearchParams object with preserved params
|
||||||
|
*/
|
||||||
|
export function getPreservedSearchParams(currentParams: URLSearchParams, paramFields: ViewOption[]) {
|
||||||
|
const managedParamIds = new Set<string>();
|
||||||
|
paramFields.forEach((section) => {
|
||||||
|
section.options.forEach((option) => {
|
||||||
|
managedParamIds.add(option.id);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const preserved = new URLSearchParams();
|
||||||
|
currentParams.forEach((value, key) => {
|
||||||
|
if (!managedParamIds.has(key)) {
|
||||||
|
preserved.append(key, value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return preserved;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user