diff --git a/apps/admin-server/src/pages/projects/[project]/widgets/resourcesmap/[id]/datalayers.tsx b/apps/admin-server/src/pages/projects/[project]/widgets/resourcesmap/[id]/datalayers.tsx index d434b988d..ba78acc89 100644 --- a/apps/admin-server/src/pages/projects/[project]/widgets/resourcesmap/[id]/datalayers.tsx +++ b/apps/admin-server/src/pages/projects/[project]/widgets/resourcesmap/[id]/datalayers.tsx @@ -13,20 +13,23 @@ import { useRouter } from 'next/router'; import useDatalayers from "@/hooks/use-datalayers"; import {Checkbox} from "@/components/ui/checkbox"; import {YesNoSelect} from "@/lib/form-widget-helpers"; +import { useEffect } from 'react'; const formSchema = z.object({ enableOnOffSwitching: z.boolean().optional(), datalayer: z.array(z.object({ id: z.number(), - name: z.string() + name: z.string(), + activeOnInit: z.boolean().optional(), })).optional(), -}); +}).catchall(z.boolean().optional()); export default function WidgetResourcesMapDatalayers( props: ResourceOverviewMapWidgetTabProps & EditFieldProps & { datalayer?: any; enableOnOffSwitching?: boolean; + activeOnInit?: boolean; } ) { @@ -48,6 +51,17 @@ export default function WidgetResourcesMapDatalayers( const { data: datalayers } = useDatalayers(props.projectId === undefined ? projectId : props.projectId) as { data: { id: string, name: string }[] } ?? []; + useEffect(() => { + if (!form.getValues('enableOnOffSwitching')) { + const updatedLayers = (form.getValues('datalayer') || []).map((layer) => { + const { activeOnInit, ...rest } = layer; + return rest; + }); + form.setValue('datalayer', updatedLayers); + props.onFieldChanged('datalayer', updatedLayers); + } + }, [form.getValues('enableOnOffSwitching')]); + return (
@@ -58,49 +72,84 @@ export default function WidgetResourcesMapDatalayers( className="space-y-4 lg:w-1/2"> {datalayers?.map((item) => ( - { +
+ { const isChecked = Array.isArray(field.value) && field.value.some(obj => obj.id === Number(item.id)); return ( - -
- - { - let values = form.getValues('datalayer') || []; + + + { + let values = form.getValues('datalayer') || []; - if (checked) { - if (!values.some(obj => obj.id === Number(item.id))) { - const { name } = item; - form.setValue('datalayer', [...values, { name, id: Number(item.id) }]); - props.onFieldChanged(field.name, [...values, { name, id: Number(item.id) }]); - } - } else { - const filteredValues = values.filter(obj => obj.id !== Number(item.id)); - form.setValue('datalayer', filteredValues); - props.onFieldChanged('datalayer', filteredValues); - } - }} - /> - - - {item.name} - -
- + if (checked) { + if (!values.some(obj => obj.id === Number(item.id))) { + const {name} = item; + form.setValue('datalayer', [...values, {name, id: Number(item.id)}]); + props.onFieldChanged(field.name, [...values, {name, id: Number(item.id)}]); + } + } else { + const filteredValues = values.filter(obj => obj.id !== Number(item.id)); + form.setValue('datalayer', filteredValues); + props.onFieldChanged('datalayer', filteredValues); + } + }} + /> + + + {item.name} + +
); - }} - /> + }} + /> + + {( + form.getValues('enableOnOffSwitching') && + Array.isArray(form.getValues('datalayer')) && + form.getValues('datalayer')?.some(obj => obj.id === Number(item.id)) + ) && ( + { + const activeOnInit = form.getValues('datalayer') + ?.find(obj => obj.id === Number(item.id))?.activeOnInit ?? true; + + return ( + + + { + let values = form.getValues('datalayer') || []; + const updatedValues = values.map(obj => + obj.id === Number(item.id) ? { ...obj, activeOnInit: !!checked } : obj + ); + form.setValue('datalayer', updatedValues); + props.onFieldChanged('datalayer', updatedValues); + }} + /> + + + Deze laag standaard tonen bij het openen van de kaart + + + + ); + }} + /> + )} +
)) || null} +
{ if (mapDataLayers.length > 0 && Object.keys(activeLayers).length === 0) { const initialLayers = mapDataLayers.reduce((acc, layer) => { - acc[layer.id] = true; // Zet alle lagen standaard aan + acc[layer.id] = layer.activeOnInit; return acc; }, {} as { [key: string]: boolean }); @@ -250,16 +251,18 @@ const ResourceOverviewMap = ({ return ((polygon && center) || !areaId) ? (
- {showOnOffButtons && mapDataLayers.length > 0 && ( + { mapDataLayers.length > 0 && (
    {mapDataLayers.map(layer => (