From a810f874611428baad834c1fccca0539101bb578 Mon Sep 17 00:00:00 2001 From: Thomas Fuhrmann Date: Fri, 29 Sep 2023 13:18:55 +0200 Subject: [PATCH] Move initi18n. Update search component --- app/[locale]/basemap-list.tsx | 10 +--- app/[locale]/i18n.js | 19 +++--- app/[locale]/layer-list.tsx | 22 +++---- app/[locale]/map.tsx | 56 ++++++++---------- app/[locale]/page.tsx | 2 + app/[locale]/print.tsx | 24 +++----- app/[locale]/search.tsx | 105 ++++++++++++++++++++++++---------- 7 files changed, 124 insertions(+), 114 deletions(-) diff --git a/app/[locale]/basemap-list.tsx b/app/[locale]/basemap-list.tsx index 6e9e5c3..5c9d321 100644 --- a/app/[locale]/basemap-list.tsx +++ b/app/[locale]/basemap-list.tsx @@ -1,7 +1,6 @@ -import { useRef, useEffect, RefObject } from 'react'; +import { useRef, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; -import initI18n from './i18n'; import MapView from '@arcgis/core/views/MapView'; import Basemap from '@arcgis/core/Basemap'; @@ -13,13 +12,6 @@ import TileLayer from '@arcgis/core/layers/TileLayer'; const BEVURL = 'https://maps.bev.gv.at/tiles/{z}/{x}/{y}.png'; -// load localized strings -const match = location.pathname.match(/\/(\w+)/); -if (match && match.length > 1) { - const locale = match[1]; - initI18n(locale); -} - export default function Basemaps({ view }: { view: MapView }) { const basemapGalleryContainer = useRef(null); const rendered = useRef(false); diff --git a/app/[locale]/i18n.js b/app/[locale]/i18n.js index 3d7c9d1..1453404 100644 --- a/app/[locale]/i18n.js +++ b/app/[locale]/i18n.js @@ -4,7 +4,7 @@ import { initReactI18next } from 'react-i18next'; import deJSON from './dictionaries/de.json'; import enJSON from './dictionaries/en.json'; -// the translations +// define translations const resources = { en: { translation: enJSON, @@ -14,15 +14,14 @@ const resources = { }, }; +// initialize i18n with given locale const initI18n = (locale) => - i18n - .use(initReactI18next) // passes i18n down to react-i18next - .init({ - resources, - lng: locale, - interpolation: { - escapeValue: false, // react already safes from xss - }, - }); + i18n.use(initReactI18next).init({ + resources, + lng: locale, + interpolation: { + escapeValue: false, + }, + }); export default initI18n; diff --git a/app/[locale]/layer-list.tsx b/app/[locale]/layer-list.tsx index 9d91a59..3a25a43 100644 --- a/app/[locale]/layer-list.tsx +++ b/app/[locale]/layer-list.tsx @@ -1,7 +1,6 @@ -import { useRef, useEffect, RefObject } from 'react'; +import { useRef, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; -import initI18n from './i18n'; import MapView from '@arcgis/core/views/MapView'; import LayerList from '@arcgis/core/widgets/LayerList'; @@ -11,14 +10,7 @@ import MapImageLayer from '@arcgis/core/layers/MapImageLayer'; import Sublayer from '@arcgis/core/layers/support/Sublayer'; import ButtonMenuItem from '@arcgis/core/widgets/FeatureTable/Grid/support/ButtonMenuItem'; -// load localized strings -const match = location.pathname.match(/\/(\w+)/); -if (match && match.length > 1) { - const locale = match[1]; - initI18n(locale); -} - -export default function Layers({ view, tableRoot }: { view: MapView; tableRoot: RefObject }) { +export default function Layers({ view, tableRoot }: { view: MapView; tableRoot: HTMLDivElement }) { const htmlDiv = useRef(null); const featureTable = useRef(null); const rendered = useRef(false); @@ -33,9 +25,9 @@ export default function Layers({ view, tableRoot }: { view: MapView; tableRoot: const tableContainer = document.createElement('div'); tableContainer.className = 'h-full w-full'; - if (tableRoot && tableRoot.current) { - tableRoot.current.classList.remove('hidden'); - tableRoot.current.append(tableContainer); + if (tableRoot) { + tableRoot.classList.remove('hidden'); + tableRoot.append(tableContainer); } const featureLayer = await layer.createFeatureLayer(); @@ -51,8 +43,8 @@ export default function Layers({ view, tableRoot }: { view: MapView; tableRoot: iconClass: 'esri-icon-close', clickFunction: function () { featureTable.current?.destroy(); - if (tableRoot && tableRoot.current) { - tableRoot.current.classList.add('hidden'); + if (tableRoot) { + tableRoot.classList.add('hidden'); } }, } as unknown as ButtonMenuItem, diff --git a/app/[locale]/map.tsx b/app/[locale]/map.tsx index 1da140e..5b80a29 100644 --- a/app/[locale]/map.tsx +++ b/app/[locale]/map.tsx @@ -1,15 +1,15 @@ -import { useRef, useState, useEffect, lazy } from 'react'; -import { Root, createRoot } from 'react-dom/client'; +import { useRef, useState, useEffect } from 'react'; +import { createRoot } from 'react-dom/client'; + +import dynamic from 'next/dynamic'; import { useTranslation } from 'react-i18next'; -import initI18n from './i18n'; import MapView from '@arcgis/core/views/MapView'; import WebMap from '@arcgis/core/WebMap'; import esriConfig from '@arcgis/core/config'; import ScaleBar from '@arcgis/core/widgets/ScaleBar'; import Legend from '@arcgis/core/widgets/Legend'; -import WMTSLayer from '@arcgis/core/layers/WMTSLayer'; import VectorTileLayer from '@arcgis/core/layers/VectorTileLayer'; import TileLayer from '@arcgis/core/layers/TileLayer'; import Map from '@arcgis/core/Map.js'; @@ -23,11 +23,11 @@ esriConfig.assetsPath = './assets'; const webMapDEID = '7d0768f73d3e4be2b32c22274c600cb3'; const webMapENID = 'dbf5532d06954c6a989d4f022de83f70'; -// lazy load print component -const Print = lazy(() => import('./print')); -const Layers = lazy(() => import('./layer-list')); -const Basemaps = lazy(() => import('./basemap-list')); -const Search = lazy(() => import('./search')); +// lazy load components +const Print = dynamic(() => import('./print')); +const Layers = dynamic(() => import('./layer-list')); +const Basemaps = dynamic(() => import('./basemap-list')); +const Search = dynamic(() => import('./search')); // import Calcite components import '@esri/calcite-components/dist/calcite/calcite.css'; @@ -49,15 +49,8 @@ import { CalcitePanel, } from '@esri/calcite-components-react'; -// load localized strings -const match = location.pathname.match(/\/(\w+)/); -if (match && match.length > 1) { - const locale = match[1]; - initI18n(locale); -} - export default function MapComponent({ locale }: { locale: string }) { - const printRoot = useRef(null); + const legendRoot = useRef(null); const maskRoot = useRef(null); const tableRoot = useRef(null); const mapView = useRef(null); @@ -81,14 +74,6 @@ export default function MapComponent({ locale }: { locale: string }) { }, }); - // const wmtsLayer = new WMTSLayer({ - // url: 'https://mapsneu.wien.gv.at/basemapneu', - // }); - - // const basemap = new Basemap({ - // baseLayers: [wmtsLayer], - // }); - const lightgrayBase = new VectorTileLayer({ url: 'https://gis.geosphere.at/portal/sharing/rest/content/items/291da5eab3a0412593b66d384379f89f/resources/styles/root.json', opacity: 0.5, @@ -124,6 +109,7 @@ export default function MapComponent({ locale }: { locale: string }) { width: 5000, }, }, + dockEnabled: true, }, extent: { ymax: 6424330, @@ -144,6 +130,7 @@ export default function MapComponent({ locale }: { locale: string }) { createRoot(document.createElement('div')).render(); }); + // add further map related UI components const scaleBar = new ScaleBar({ view: view, unit: 'metric', @@ -151,10 +138,12 @@ export default function MapComponent({ locale }: { locale: string }) { view.ui.add([scaleBar], 'bottom-left'); - new Legend({ - view: view, - container: 'legend-container', - }); + if (legendRoot.current) { + new Legend({ + view: view, + container: legendRoot.current, + }); + } } return () => {}; @@ -211,7 +200,6 @@ export default function MapComponent({ locale }: { locale: string }) { return (
-