From 2e4d93a07b1132db6c4efa7d9b38828d18e78937 Mon Sep 17 00:00:00 2001 From: Damien Allen Date: Sun, 26 May 2024 10:40:21 +0200 Subject: [PATCH] Fixed randomize routing --- app/src/components/ProcessingStatus.tsx | 30 ++++++++++++++--- app/src/components/RouteChangeHandler.tsx | 6 +++- app/src/index.tsx | 12 ++----- app/src/router.tsx | 9 +++++ app/src/stores.tsx | 41 ++++++++++------------- 5 files changed, 60 insertions(+), 38 deletions(-) create mode 100644 app/src/router.tsx diff --git a/app/src/components/ProcessingStatus.tsx b/app/src/components/ProcessingStatus.tsx index a32b5f5..2e7bde9 100644 --- a/app/src/components/ProcessingStatus.tsx +++ b/app/src/components/ProcessingStatus.tsx @@ -1,6 +1,6 @@ import { Buildings } from '@phosphor-icons/react/Buildings' +import { DiceThree } from '@phosphor-icons/react/DiceThree' import { Gear } from '@phosphor-icons/react/Gear' - import { Tooltip } from '@mantine/core' import { createUseStyles } from 'react-jss' import { observer } from 'mobx-react' @@ -15,9 +15,26 @@ const useStyles = createUseStyles({ userSelect: 'none', padding: '0 8px', }, - spin: { + gear: { animation: 'spin 5s linear infinite', }, + icon: { + '& .dice': { + display: 'none', + }, + '& .city': { + display: 'inline-block', + }, + '&:hover': { + '& .dice': { + display: 'inline-block', + }, + '& .city': { + display: 'none', + }, + cursor: 'pointer', + }, + }, }) export const ProcessingStatus = observer(() => { @@ -28,10 +45,15 @@ export const ProcessingStatus = observer(() => {
{contours.areProcessing ? ( - + ) : ( - + + contours.randomizeFeature()}> + + + + )}
) diff --git a/app/src/components/RouteChangeHandler.tsx b/app/src/components/RouteChangeHandler.tsx index 676982b..101bbb0 100644 --- a/app/src/components/RouteChangeHandler.tsx +++ b/app/src/components/RouteChangeHandler.tsx @@ -10,7 +10,11 @@ export const RouteChangeHandler = observer(() => { useEffect(() => { if (app.features) { - contours.featureFromPath(location.pathname) + if (location.pathname === '/') { + contours.randomizeFeature(false) + } else { + contours.featureFromPath(location.pathname) + } } }, [app.features, location]) diff --git a/app/src/index.tsx b/app/src/index.tsx index e16c56e..4f5d8cc 100644 --- a/app/src/index.tsx +++ b/app/src/index.tsx @@ -1,15 +1,7 @@ import './index.css' -import { RouterProvider, createBrowserRouter } from 'react-router-dom' - -import { App } from './components/App.tsx' import ReactDOM from 'react-dom/client' - -const router = createBrowserRouter([ - { - path: '*', - element: , - }, -]) +import { RouterProvider } from 'react-router-dom' +import { router } from './router' ReactDOM.createRoot(document.getElementById('root')!).render() diff --git a/app/src/router.tsx b/app/src/router.tsx new file mode 100644 index 0000000..fce7a26 --- /dev/null +++ b/app/src/router.tsx @@ -0,0 +1,9 @@ +import { App } from './components/App.tsx' +import { createBrowserRouter } from 'react-router-dom' + +export const router = createBrowserRouter([ + { + path: '*', + element: , + }, +]) diff --git a/app/src/stores.tsx b/app/src/stores.tsx index 06b2acc..34d9c43 100644 --- a/app/src/stores.tsx +++ b/app/src/stores.tsx @@ -5,6 +5,7 @@ import { MapGeoJSONFeature } from 'maplibre-gl' import React from 'react' import { makeAutoObservable } from 'mobx' import packageJson from '../package.json' +import { router } from './router' const worker = new Worker(new URL('./contour.worker.ts', import.meta.url), { type: 'module', @@ -200,26 +201,26 @@ export class ContoursStore { return this.data?.url } - randomizeFeature = () => { - this.setSelected({ - URAU_CODE: 'NL037C', - URAU_CATG: 'C', - CNTR_CODE: 'NL', - URAU_NAME: 'Rotterdam ', - CITY_CPTL: null, - FUA_CODE: 'NL037F', - AREA_SQM: 562733118.219429, - NUTS3_2021: 'NL33C', - FID: 'NL037C', - }) + randomizeFeature = (setPath: boolean = true) => { + if (setPath) { + router.navigate(`/rotterdam`) + } else { + this.setSelected({ + URAU_CODE: 'NL037C', + URAU_CATG: 'C', + CNTR_CODE: 'NL', + URAU_NAME: 'Rotterdam ', + CITY_CPTL: null, + FUA_CODE: 'NL037F', + AREA_SQM: 562733118.219429, + NUTS3_2021: 'NL33C', + FID: 'NL037C', + }) + } } featureFromPath = (path: string) => { const cityName = path.replace('/', '') - if (cityName === '') { - this.randomizeFeature() - } - const feature = this.root.app.features.find( (feat: MapGeoJSONFeature) => slugify(feat.properties.URAU_NAME) === cityName ) @@ -227,7 +228,7 @@ export class ContoursStore { this.setSelected(feature.properties) } else { console.log(`Unable to find city '${cityName}', randomizing...`) - this.randomizeFeature() + router.navigate('/') } } @@ -346,9 +347,3 @@ export const StoreProvider = ({ children }: { children: any }) => ( ) export const useStores = () => React.useContext(StoreContext) - -let timer: number -const debounce = (func: any, timeout = 350) => { - clearTimeout(timer) - timer = setTimeout(func, timeout) -}