Skip to content

Commit

Permalink
✨ [open-formulieren/open-forms#2173] Add tile layer configuration to …
Browse files Browse the repository at this point in the history
…map edit
  • Loading branch information
robinmolen committed Dec 11, 2024
1 parent e130091 commit 4a010cb
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/components/ComponentConfiguration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const ComponentConfiguration: React.FC<ComponentConfigurationProps> = ({
uniquifyKey,
supportedLanguageCodes = ['nl', 'en'],
richTextColors,
getMapTileLayers,
theme,
getFormComponents,
getValidatorPlugins,
Expand Down Expand Up @@ -60,6 +61,7 @@ const ComponentConfiguration: React.FC<ComponentConfigurationProps> = ({
uniquifyKey,
supportedLanguageCodes,
richTextColors,
getMapTileLayers,
theme,
getFormComponents,
getValidatorPlugins,
Expand Down
15 changes: 15 additions & 0 deletions src/components/builder/loader.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Meta, StoryObj} from '@storybook/react';

import Loader from '@/components/builder/loader';

type Story = StoryObj<typeof Loader>;

export default {
title: 'Formio/Builder/Loader',
component: Loader,
parameters: {
modal: {noModal: true},
},
} satisfies Meta<typeof Loader>;

export const Default: Story = {};
13 changes: 13 additions & 0 deletions src/components/builder/loader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {FormattedMessage} from 'react-intl';

const Loader: React.FC = () => {
return (
<div className="spinner-border text-primary" role="status">
<span className="sr-only">
<FormattedMessage description="Loading content text" defaultMessage="Loading..." />
</span>
</div>
);
};

export default Loader;
13 changes: 13 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ export interface DocumentTypeOption {
description: string;
}

/*
Map tile layers
This datastructure is created by the Open Forms backend.
*/
export interface MapTileLayer {
identifier: string;
url: string;
label: string;
}

/*
Builder
*/
Expand All @@ -48,6 +59,7 @@ export interface BuilderContextType {
getDocumentTypes: () => Promise<Array<DocumentTypeOption>>;
getConfidentialityLevels: () => Promise<SelectOption[]>;
getAuthPlugins: () => Promise<AuthPluginOption[]>;
getMapTileLayers: () => Promise<MapTileLayer[]>;
}

const BuilderContext = React.createContext<BuilderContextType>({
Expand All @@ -65,6 +77,7 @@ const BuilderContext = React.createContext<BuilderContextType>({
getDocumentTypes: async () => [],
getConfidentialityLevels: async () => [],
getAuthPlugins: async () => [],
getMapTileLayers: async () => [],
});

BuilderContext.displayName = 'BuilderContext';
Expand Down
45 changes: 42 additions & 3 deletions src/registry/map/edit.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {MapComponentSchema} from '@open-formulieren/types';
import {useFormikContext} from 'formik';
import {useEffect} from 'react';
import {useContext, useEffect} from 'react';
import {FormattedMessage, useIntl} from 'react-intl';
import useAsync from 'react-use/esm/useAsync';

import {
BuilderTabs,
Expand All @@ -20,7 +21,8 @@ import {
useDeriveComponentKey,
} from '@/components/builder';
import {LABELS} from '@/components/builder/messages';
import {Checkbox, TabList, TabPanel, Tabs} from '@/components/formio';
import {Checkbox, Select, TabList, TabPanel, Tabs} from '@/components/formio';
import {BuilderContext} from '@/context';
import {useErrorChecker} from '@/utils/errors';

import {EditFormDefinition} from '../types';
Expand Down Expand Up @@ -62,7 +64,8 @@ const EditForm: EditFormDefinition<MapComponentSchema> = () => {
'isSensitiveData',
'useConfigDefaultMapSettings',
'defaultZoom',
'initialCenter'
'initialCenter',
'tileLayerIdentifier'
)}
/>
<BuilderTabs.Advanced hasErrors={hasAnyError('conditional')} />
Expand All @@ -83,6 +86,7 @@ const EditForm: EditFormDefinition<MapComponentSchema> = () => {
<IsSensitiveData />
<UseConfigDefaultMapSettings />
{!values.useConfigDefaultMapSettings && <MapConfiguration />}
<TileLayer />
</TabPanel>

{/* Advanced tab */}
Expand Down Expand Up @@ -134,6 +138,7 @@ EditForm.defaultValues = {
lat: undefined,
lng: undefined,
},
tileLayerIdentifier: undefined,
defaultValue: null,
// Advanced tab
conditional: {
Expand Down Expand Up @@ -174,4 +179,38 @@ const UseConfigDefaultMapSettings: React.FC<{}> = () => {
);
};

const TileLayer: React.FC = () => {
const {getMapTileLayers} = useContext(BuilderContext);
const intl = useIntl();
const {value: options, loading, error} = useAsync(async () => await getMapTileLayers(), []);
if (error) {
throw error;
}

const tooltip = intl.formatMessage({
description: "Tooltip for 'tileLayerIdentifier' builder field",
defaultMessage:
'The tile layer is responsible for showing the map background. ' +
'This effects the map style at particular coordinates and zoom levels.',
});
return (
<Select
name="tileLayerIdentifier"
label={
<FormattedMessage
description="Label for 'tileLayerIdentifier' builder field"
defaultMessage="Tile layer"
/>
}
isClearable
tooltip={tooltip}
isLoading={loading}
options={options?.map(tileLayer => ({
label: tileLayer.label,
value: tileLayer.identifier,
}))}
/>
);
};

export default EditForm;
26 changes: 24 additions & 2 deletions src/registry/map/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {CRS_RD, TILE_LAYER_RD} from '@open-formulieren/leaflet-tools';
import {MapComponentSchema} from '@open-formulieren/types';
import {useLayoutEffect} from 'react';
import {useContext, useLayoutEffect} from 'react';
import {MapContainer, TileLayer, useMap} from 'react-leaflet';
import useAsync from 'react-use/esm/useAsync';

import Loader from '@/components/builder/loader';
import {Component, Description} from '@/components/formio';
import {BuilderContext} from '@/context';

import {ComponentPreviewProps} from '../types';

Expand Down Expand Up @@ -37,10 +40,29 @@ const Preview: React.FC<ComponentPreviewProps<MapComponentSchema>> = ({component
validate = {},
defaultZoom,
initialCenter = {},
tileLayerIdentifier,
} = component;
const {getMapTileLayers} = useContext(BuilderContext);
const {value: tileLayers, loading, error} = useAsync(async () => await getMapTileLayers(), []);
if (error) {
throw error;
}
if (loading) {
return <Loader />;
}

const {required = false} = validate;
const {lat = 52.1326332, lng = 5.291266} = initialCenter;
const zoom = defaultZoom ?? 8;

const tileLayerUrl = (): string => {
// Try to find the url of the chosen tile layer. If it is found, return the url,
// else return the default tile layer url.
return (
tileLayers?.find(tileLayer => tileLayer.identifier === tileLayerIdentifier)?.url ??
TILE_LAYER_RD.url
);
};
return (
<Component
type={component.type}
Expand All @@ -61,7 +83,7 @@ const Preview: React.FC<ComponentPreviewProps<MapComponentSchema>> = ({component
minBlockSize: '400px',
}}
>
<TileLayer {...TILE_LAYER_RD} />
<TileLayer {...TILE_LAYER_RD} url={tileLayerUrl()} />
<MapView lat={lat} lng={lng} zoom={zoom} />
</MapContainer>
{description && <Description text={description} />}
Expand Down

0 comments on commit 4a010cb

Please sign in to comment.