2022-12-08 08:24:22 -05:00
|
|
|
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
2021-02-22 01:59:24 -05:00
|
|
|
import { Redirect, Route, Switch } from 'react-router-dom';
|
2021-04-26 14:19:06 -04:00
|
|
|
import loadable from '@loadable/component';
|
2021-02-22 01:59:24 -05:00
|
|
|
|
2021-08-22 21:26:58 -04:00
|
|
|
import { useRevisionId } from './api-data/use-revision';
|
|
|
|
import { useBuildingData } from './api-data/use-building-data';
|
|
|
|
import { useUserVerifiedData } from './api-data/use-user-verified-data';
|
2021-02-22 01:59:24 -05:00
|
|
|
import { useUrlBuildingParam } from './nav/use-url-building-param';
|
|
|
|
import { useUrlCategoryParam } from './nav/use-url-category-param';
|
|
|
|
import { useUrlModeParam } from './nav/use-url-mode-param';
|
2019-11-07 02:39:26 -05:00
|
|
|
import BuildingView from './building/building-view';
|
2019-09-08 20:09:05 -04:00
|
|
|
import Categories from './building/categories';
|
2019-11-07 02:39:26 -05:00
|
|
|
import { EditHistory } from './building/edit-history/edit-history';
|
2019-09-08 20:09:05 -04:00
|
|
|
import MultiEdit from './building/multi-edit';
|
2019-11-07 02:39:26 -05:00
|
|
|
import Sidebar from './building/sidebar';
|
2021-02-22 01:59:24 -05:00
|
|
|
import { Building, UserVerified } from './models/building';
|
2019-11-07 02:39:26 -05:00
|
|
|
import Welcome from './pages/welcome';
|
2021-02-08 11:03:30 -05:00
|
|
|
import { PrivateRoute } from './route';
|
2021-02-22 01:59:24 -05:00
|
|
|
import { useLastNotEmpty } from './hooks/use-last-not-empty';
|
|
|
|
import { Category } from './config/categories-config';
|
2022-12-08 08:24:22 -05:00
|
|
|
import { BuildingMapTileset } from './config/tileserver-config';
|
|
|
|
import { defaultMapCategory, categoryMapsConfig } from './config/category-maps-config';
|
2021-02-22 01:59:24 -05:00
|
|
|
import { useMultiEditData } from './hooks/use-multi-edit-data';
|
2021-08-22 21:26:58 -04:00
|
|
|
import { useAuth } from './auth-context';
|
|
|
|
import { sendBuildingUpdate } from './api-data/building-update';
|
2019-09-08 20:09:05 -04:00
|
|
|
|
2021-04-26 14:19:06 -04:00
|
|
|
/**
|
|
|
|
* Load and render ColouringMap component on client-side only.
|
|
|
|
* This is because leaflet and react-leaflet currently don't work on the server
|
|
|
|
* (leaflet assumes the presence of browser-specific global `window` variable).
|
|
|
|
*
|
|
|
|
* The previous solution involved installing react-leaflet-universal,
|
|
|
|
* but that doesn't work with latest react-leaflet.
|
|
|
|
*
|
|
|
|
* The limitation is that ColouringMap needs to be the single entry point in the whole app
|
|
|
|
* to all modules that import leaflet or react-leaflet.
|
|
|
|
*/
|
|
|
|
const ColouringMap = loadable(
|
2021-10-01 08:30:03 -04:00
|
|
|
async () => (await import('./map/map')).ColouringMap,
|
|
|
|
{ ssr: false }
|
2021-04-26 14:19:06 -04:00
|
|
|
);
|
|
|
|
|
2021-02-22 01:59:24 -05:00
|
|
|
interface MapAppProps {
|
2019-11-05 15:13:10 -05:00
|
|
|
building?: Building;
|
2021-02-22 01:59:24 -05:00
|
|
|
revisionId?: string;
|
2020-08-04 10:54:49 -04:00
|
|
|
user_verified?: object;
|
2019-09-08 20:09:05 -04:00
|
|
|
}
|
|
|
|
|
2021-02-22 01:59:24 -05:00
|
|
|
/** Returns first argument, unless it's equal to the second argument - then returns undefined */
|
|
|
|
function unless<V extends string, U extends V>(value: V, unlessValue: U): Exclude<V, U> {
|
|
|
|
return value === unlessValue ? undefined : value as Exclude<V, U>;
|
2019-09-08 20:09:05 -04:00
|
|
|
}
|
|
|
|
|
2021-02-22 01:59:24 -05:00
|
|
|
/** Returns the new value, unless it is equal to the current value - then returns undefined */
|
|
|
|
function setOrToggle<T>(currentValue: T, newValue: T): T {
|
|
|
|
if(newValue == undefined || newValue === currentValue){
|
|
|
|
return undefined;
|
|
|
|
} else {
|
|
|
|
return newValue;
|
2019-09-08 20:09:05 -04:00
|
|
|
}
|
2021-02-22 01:59:24 -05:00
|
|
|
}
|
2019-09-08 20:09:05 -04:00
|
|
|
|
2022-12-08 08:24:22 -05:00
|
|
|
function useStateWithOptions<T>(defaultValue: T, options: T[]): [T, (x: T) => void] {
|
|
|
|
const [value, setValue] = useState(defaultValue);
|
|
|
|
|
|
|
|
const effectiveValue = options.includes(value) ? value : options[0];
|
|
|
|
const handleChange = useCallback((x) => setValue(x), []);
|
|
|
|
|
|
|
|
return [effectiveValue, handleChange];
|
|
|
|
}
|
|
|
|
|
2021-02-22 01:59:24 -05:00
|
|
|
export const MapApp: React.FC<MapAppProps> = props => {
|
2021-08-22 21:26:58 -04:00
|
|
|
const { user } = useAuth();
|
2021-02-22 01:59:24 -05:00
|
|
|
const [categoryUrlParam] = useUrlCategoryParam();
|
|
|
|
|
|
|
|
const [currentCategory, setCategory] = useState<Category>();
|
|
|
|
useEffect(() => setCategory(unless(categoryUrlParam, 'categories')), [categoryUrlParam]);
|
2021-02-24 02:48:09 -05:00
|
|
|
|
2021-02-22 01:59:24 -05:00
|
|
|
const displayCategory = useLastNotEmpty(currentCategory) ?? defaultMapCategory;
|
2021-02-24 02:48:09 -05:00
|
|
|
|
|
|
|
const [selectedBuildingId, setSelectedBuildingId] = useUrlBuildingParam('view', displayCategory);
|
|
|
|
|
2021-08-22 21:26:58 -04:00
|
|
|
const [building, updateBuilding, reloadBuilding] = useBuildingData(selectedBuildingId, props.building, user != undefined);
|
2021-02-22 01:59:24 -05:00
|
|
|
const [userVerified, updateUserVerified, reloadUserVerified] = useUserVerifiedData(selectedBuildingId, props.user_verified);
|
2021-02-24 02:48:09 -05:00
|
|
|
|
2021-02-22 01:59:24 -05:00
|
|
|
const [revisionId, updateRevisionId] = useRevisionId(props.revisionId);
|
|
|
|
useEffect(() => {
|
|
|
|
updateRevisionId(building?.revision_id)
|
|
|
|
}, [building]);
|
2021-02-24 02:48:09 -05:00
|
|
|
|
|
|
|
const [mode] = useUrlModeParam();
|
2021-02-22 01:59:24 -05:00
|
|
|
const viewEditMode = unless(mode, 'multi-edit');
|
|
|
|
|
|
|
|
const [multiEditData, multiEditError] = useMultiEditData();
|
|
|
|
|
|
|
|
const selectBuilding = useCallback((selectedBuilding: Building) => {
|
2021-02-24 02:48:09 -05:00
|
|
|
const currentId = selectedBuildingId;
|
|
|
|
updateBuilding(selectedBuilding);
|
|
|
|
setSelectedBuildingId(setOrToggle(currentId, selectedBuilding?.building_id));
|
2021-02-22 01:59:24 -05:00
|
|
|
}, [selectedBuildingId, setSelectedBuildingId, updateBuilding, building]);
|
|
|
|
|
|
|
|
const colourBuilding = useCallback(async (building: Building) => {
|
|
|
|
const buildingId = building?.building_id;
|
|
|
|
|
|
|
|
if(buildingId != undefined && multiEditError == undefined) {
|
|
|
|
try {
|
2021-08-22 21:26:58 -04:00
|
|
|
const updatedBuilding = await sendBuildingUpdate(buildingId, multiEditData);
|
|
|
|
updateRevisionId(updatedBuilding.revision_id);
|
2021-02-22 01:59:24 -05:00
|
|
|
} catch(error) {
|
|
|
|
console.error({ error });
|
|
|
|
}
|
2019-11-14 10:25:19 -05:00
|
|
|
}
|
2021-02-22 01:59:24 -05:00
|
|
|
}, [multiEditError, multiEditData, currentCategory]);
|
2019-11-26 07:09:27 -05:00
|
|
|
|
2021-02-22 01:59:24 -05:00
|
|
|
const handleBuildingUpdate = useCallback((buildingId: number, updatedData: Building) => {
|
|
|
|
// only update current building data if the IDs match
|
|
|
|
if(buildingId === selectedBuildingId) {
|
|
|
|
updateBuilding(Object.assign({}, building, updatedData));
|
|
|
|
} else {
|
|
|
|
// otherwise, still update the latest revision ID
|
|
|
|
updateRevisionId(updatedData.revision_id);
|
2019-09-08 20:09:05 -04:00
|
|
|
}
|
2021-02-22 01:59:24 -05:00
|
|
|
}, [selectedBuildingId, building, updateBuilding, updateRevisionId]);
|
2019-09-08 20:09:05 -04:00
|
|
|
|
2021-02-22 01:59:24 -05:00
|
|
|
const handleUserVerifiedUpdate = useCallback((buildingId: number, updatedData: UserVerified) => {
|
|
|
|
// only update current building data if the IDs match
|
|
|
|
if(buildingId === selectedBuildingId) {
|
|
|
|
updateUserVerified(Object.assign({}, userVerified, updatedData)); // quickly show added verifications
|
|
|
|
reloadBuilding();
|
|
|
|
reloadUserVerified(); // but still reload from server to reflect removed verifications
|
2019-09-08 20:09:05 -04:00
|
|
|
}
|
2021-02-22 01:59:24 -05:00
|
|
|
}, [selectedBuildingId, updateUserVerified, reloadBuilding, userVerified]);
|
2019-09-08 20:09:05 -04:00
|
|
|
|
2022-12-08 08:24:22 -05:00
|
|
|
|
|
|
|
const categoryMapDefinitions = useMemo(() => categoryMapsConfig[displayCategory], [displayCategory]);
|
|
|
|
const availableMapStyles = useMemo(() => categoryMapDefinitions.map(x => x.mapStyle), [categoryMapDefinitions]);
|
|
|
|
const [mapColourScale, setMapColourScale] = useStateWithOptions<BuildingMapTileset>(undefined, availableMapStyles);
|
|
|
|
|
2021-02-22 01:59:24 -05:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<PrivateRoute path="/:mode(edit|multi-edit)" /> {/* empty private route to ensure auth for editing */}
|
|
|
|
<Sidebar>
|
2019-09-08 20:09:05 -04:00
|
|
|
<Switch>
|
|
|
|
<Route exact path="/">
|
2021-02-22 01:59:24 -05:00
|
|
|
<Welcome />
|
2019-09-08 20:09:05 -04:00
|
|
|
</Route>
|
2021-02-22 01:59:24 -05:00
|
|
|
<Route exact path="/multi-edit/:cat">
|
|
|
|
<MultiEdit category={displayCategory} />
|
2019-09-08 20:09:05 -04:00
|
|
|
</Route>
|
2021-02-22 01:59:24 -05:00
|
|
|
<Route path="/:mode/:cat">
|
|
|
|
<Categories mode={mode || 'view'} building_id={selectedBuildingId} />
|
|
|
|
<Switch>
|
|
|
|
<Route exact path="/:mode/:cat/:building/history">
|
|
|
|
<EditHistory building={building} />
|
|
|
|
</Route>
|
|
|
|
<Route exact path="/:mode/:cat/:building?">
|
|
|
|
<BuildingView
|
|
|
|
mode={viewEditMode}
|
|
|
|
cat={displayCategory}
|
|
|
|
building={building}
|
|
|
|
user_verified={userVerified ?? {}}
|
|
|
|
onBuildingUpdate={handleBuildingUpdate}
|
|
|
|
onUserVerifiedUpdate={handleUserVerifiedUpdate}
|
|
|
|
/>
|
|
|
|
</Route>
|
|
|
|
</Switch>
|
2019-10-24 07:20:48 -04:00
|
|
|
</Route>
|
2019-10-15 09:53:01 -04:00
|
|
|
<Route exact path="/:mode(view|edit|multi-edit)"
|
2020-02-03 17:35:32 -05:00
|
|
|
render={props => (<Redirect to={`/${props.match.params.mode}/categories`} />)}
|
2019-10-15 09:53:01 -04:00
|
|
|
/>
|
2019-09-08 20:09:05 -04:00
|
|
|
</Switch>
|
2021-02-22 01:59:24 -05:00
|
|
|
</Sidebar>
|
|
|
|
<ColouringMap
|
|
|
|
selectedBuildingId={selectedBuildingId}
|
|
|
|
mode={mode || 'basic'}
|
|
|
|
revisionId={revisionId}
|
|
|
|
onBuildingAction={mode === 'multi-edit' ? colourBuilding : selectBuilding}
|
2022-12-08 08:24:22 -05:00
|
|
|
mapColourScale={mapColourScale}
|
|
|
|
onMapColourScale={setMapColourScale}
|
|
|
|
categoryMapDefinitions={categoryMapDefinitions}
|
2021-02-22 01:59:24 -05:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|