Skip to content

Commit

Permalink
Removed a GeoChart from template for testing
Browse files Browse the repository at this point in the history
Store processor stuff first
Simplified back code
Bug fix with layer-state
Typos and cleanup
eslint
  • Loading branch information
Alex-NRCan committed Jan 25, 2024
1 parent 4358552 commit 9e24f7b
Show file tree
Hide file tree
Showing 21 changed files with 372 additions and 130 deletions.
2 changes: 1 addition & 1 deletion docs/programming/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The logger class can be found here: [https://github.com/Canadian-Geospatial-Pla
The `logger` provides functions for high-level logging abstraction following best-practices concepts and the following constants:
```ts
// The most detailed messages. Disabled by default. Only shows if actually running in dev environment, never shown otherwise.
export const LOG_TRACE_DETAILED 1;
export const LOG_TRACE_DETAILED = 1;
// For tracing useEffect unmounting. Disabled by default. Only shows if running in dev environment or GEOVIEW_LOG_ACTIVE key is set in local storage.
export const LOG_TRACE_USE_EFFECT_UNMOUNT = 2;
// For tracing rendering. Disabled by default. Only shows if running in dev environment or GEOVIEW_LOG_ACTIVE key is set in local storage.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
{
"layers": [
{
"layerId": "geojsonLYR5/polygons.json"
"layerId": "geojsonLYR5/geojsonLYR5/polygons.json"
}
],
"chart": "pie",
Expand Down Expand Up @@ -232,7 +232,7 @@
{
"layers": [
{
"layerId": "geojsonLYR5/point-feature-group/points.json"
"layerId": "geojsonLYR5/geojsonLYR5/point-feature-group/points.json"
}
],
"chart": "bar",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
{
"layers": [
{
"layerId": "geojsonLYR5/polygons.json"
"layerId": "geojsonLYR5/geojsonLYR5/polygons.json"
}
],
"chart": "pie",
Expand Down Expand Up @@ -232,7 +232,7 @@
{
"layers": [
{
"layerId": "geojsonLYR5/point-feature-group/points.json"
"layerId": "geojsonLYR5/geojsonLYR5/point-feature-group/points.json"
}
],
"chart": "bar",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ <h1><strong>Package Footer Panel With Geo-Chart</strong></h1>
<hr />
<div id="UIM10" class="geoview-map" data-lang="en" data-config-url="./configs/package-footer-panel-geochart-map1-config.json"></div>
<hr />
<div id="UIM11" class="geoview-map" data-lang="fr" data-config-url="./configs/package-footer-panel-geochart-map2-config.json"></div>
<!-- <div id="UIM11" class="geoview-map" data-lang="fr" data-config-url="./configs/package-footer-panel-geochart-map2-config.json"></div> -->
<hr />

<script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ <h4 id="HUC1">1. Default Time Slider</h4>
<h4 id="HUC2">2. Custom Time Slider</h4>
<a class="ref-link" href="#top">Top</a>
</div>
<div id="mapWM2" class="geoview-map" data-lang="en" data-config-url="./configs/package-time-slider2-config.json"></div>
<!-- <div id="mapWM2" class="geoview-map" data-lang="en" data-config-url="./configs/package-time-slider2-config.json"></div> -->
<hr />

<script src="codedoc.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { GeoviewStoreType } from '@/core/stores/geoview-store';
import { AbstractEventProcessor } from '../abstract-event-processor';
import { getGeoViewStore } from '@/core/stores/stores-managers';
import { TypeJsonObject } from '@/core/types/global-types';
import { GeoChartStoreByLayerPath } from '@/core/stores/store-interface-and-intial-values/geochart-state';

export class GeochartEventProcessor extends AbstractEventProcessor {
onInitialize(store: GeoviewStoreType) {
store.getState();

// add to arr of subscriptions so it can be destroyed later
this.subscriptionArr.push();
}

// **********************************************************
// Static functions for Typescript files to access store actions
// **********************************************************
//! Typescript MUST always use store action to modify store - NEVER use setState!
//! Some action does state modifications AND map actions.
//! ALWAYS use map event processor when an action modify store and IS NOT trap by map state event handler

// #region

/**
* Set the default layers from configuration.
* In the store, the GeoChart configurations are stored in an object with layerPath as its property name
* (to retrieve the configuration per layer faster).
*
* @param {string} mapId the map id
* @param {TypeJsonObject} charts The array of JSON configuration for geochart
*/
static setGeochartCharts(mapId: string, charts: TypeJsonObject[]): void {
// The store object representation
const chartData: GeoChartStoreByLayerPath = {};

// Loop on the charts
// eslint-disable-next-line @typescript-eslint/no-explicit-any
charts.forEach((chartInfo: any) => {
// For each layer path
// eslint-disable-next-line @typescript-eslint/no-explicit-any
chartInfo.layers.forEach((layer: any) => {
// Get the layer path
const layerPath = layer.layerId;
chartData[layerPath] = chartInfo;
});
});

// set store charts config
getGeoViewStore(mapId).getState().geochartState.actions.setGeochartCharts(chartData);
}

// #endregion

// **********************************************************
// Static functions for Store Map State to action on API
// **********************************************************
//! NEVER add a store action who does set state AND map action at a same time.
//! Review the action in store state to make sure
}
7 changes: 7 additions & 0 deletions packages/geoview-core/src/api/event-processors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import { GeoviewStoreType } from '@/core/stores/geoview-store';
import { LegendEventProcessor } from '@/api/event-processors/event-processor-children/legend-event-processor';
import { MapEventProcessor } from '@/api/event-processors/event-processor-children/map-event-processor';
import { TimeSliderEventProcessor } from '@/api/event-processors/event-processor-children/time-slider-event-processor';
import { GeochartEventProcessor } from '@/api/event-processors/event-processor-children/geochart-event-processor';

// core
const appEventProcessor = new AppEventProcessor();
const featureInfoEventProcessor = new FeatureInfoEventProcessor();
const legendEventProcessor = new LegendEventProcessor();
const mapEventProcessor = new MapEventProcessor();

// packages
const timeSliderEventProcessor = new TimeSliderEventProcessor();
const geochartEventProcessor = new GeochartEventProcessor();

export function initializeEventProcessors(store: GeoviewStoreType) {
// core stores
Expand All @@ -21,6 +26,7 @@ export function initializeEventProcessors(store: GeoviewStoreType) {
// package stores, only create if needed
// TODO: Change this check for something more generic that checks in appBarTabs too
if (store.getState().mapConfig!.footerTabs?.tabs.core.includes('time-slider')) timeSliderEventProcessor.onInitialize(store);
if (store.getState().mapConfig!.footerTabs?.tabs.core.includes('geochart')) geochartEventProcessor.onInitialize(store);
}

export function destroyEventProcessors(store: GeoviewStoreType) {
Expand All @@ -33,4 +39,5 @@ export function destroyEventProcessors(store: GeoviewStoreType) {
// package stores, only destroy if created
// TODO: Change this check for something more generic that checks in appBarTabs too
if (store.getState().mapConfig!.footerTabs?.tabs.core.includes('time-slider')) timeSliderEventProcessor.onDestroy(store);
if (store.getState().mapConfig!.footerTabs?.tabs.core.includes('geochart')) geochartEventProcessor.onDestroy(store);
}
6 changes: 4 additions & 2 deletions packages/geoview-core/src/core/app-start.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Suspense, useMemo } from 'react';
import { createContext, Suspense, useMemo } from 'react';

import './translation/i18n';
import i18n from 'i18next';
Expand All @@ -16,7 +16,7 @@ import { logger } from './utils/logger';

// create a state that will hold map config information
// TODO: use store, only keep map id on context for store manager to gather right store on hooks
export const MapContext = React.createContext<TypeMapContext>({
export const MapContext = createContext<TypeMapContext>({
mapId: '',
mapFeaturesConfig: undefined,
});
Expand Down Expand Up @@ -80,7 +80,9 @@ function AppStart(props: AppStartProps): JSX.Element {
<I18nextProvider i18n={i18nInstance}>
<MapContext.Provider value={mapContextValue}>
<ThemeProvider theme={getTheme(theme)}>
{/* <StrictMode> */}
<Shell shellId={mapId as string} />
{/* </StrictMode> */}
</ThemeProvider>
</MapContext.Provider>
</I18nextProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { IconStack } from '@/app';
export interface LayerListEntry {
layerName: string;
layerPath: string;
layerFeatures?: ReactNode | undefined;
mapFilteredIcon?: ReactNode | undefined;
tooltip?: ReactNode | undefined;
layerFeatures?: ReactNode;
mapFilteredIcon?: ReactNode;
tooltip?: ReactNode;
numOffeatures?: number;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function Layout({ children, layerList, handleLayerList, selectedLayerPath
/>
);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedLayerPath, isEnlargeDataTable]);
}, [selectedLayerPath, isEnlargeDataTable, layerList]);

return (
<Box sx={sxClasses.detailsContainer}>
Expand Down
9 changes: 7 additions & 2 deletions packages/geoview-core/src/core/stores/geoview-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ILayerState, initializeLayerState } from './store-interface-and-intial-
import { IMapState, initializeMapState } from './store-interface-and-intial-values/map-state';
import { IMapDataTableState, initialDataTableState } from './store-interface-and-intial-values/data-table-state';
import { ITimeSliderState, initializeTimeSliderState } from './store-interface-and-intial-values/time-slider-state';
import { IGeochartState, initializeGeochartState } from './store-interface-and-intial-values/geochart-state';
import { IUIState, initializeUIState } from './store-interface-and-intial-values/ui-state';

import { TypeMapFeaturesConfig } from '@/core/types/global-types';
Expand All @@ -26,14 +27,17 @@ export interface IGeoviewState {
mapId: string;
setMapConfig: (config: TypeMapFeaturesConfig) => void;

// state interfaces
// core state interfaces
appState: IAppState;
detailsState: IDetailsState;
dataTableState: IMapDataTableState;
layerState: ILayerState;
mapState: IMapState;
timeSliderState: ITimeSliderState;
uiState: IUIState;

// packages state interface
geochartState: IGeochartState;
timeSliderState: ITimeSliderState;
}

export const geoviewStoreDefinition = (set: TypeSetStore, get: TypeGetStore) => {
Expand All @@ -56,6 +60,7 @@ export const geoviewStoreDefinition = (set: TypeSetStore, get: TypeGetStore) =>
// packages states, only create if needed
// TODO: Change this check for something more generic that checks in appBarTabs too
if (config.footerTabs?.tabs.core.includes('time-slider')) set({ timeSliderState: initializeTimeSliderState(set, get) });
if (config.footerTabs?.tabs.core.includes('geochart')) set({ geochartState: initializeGeochartState(set, get) });
},

// core states
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { useStore } from 'zustand';
import { useGeoViewStore } from '../stores-managers';
import { TypeGetStore, TypeSetStore } from '../geoview-store';

export type GeoChartStoreByLayerPath = {
[layerPath: string]: ChartInfo;
};

export type ChartInfo = unknown; // unknown, because the definition is in the external package

// #region INTERFACES
export interface IGeochartState {
geochartChartsConfig: GeoChartStoreByLayerPath;
// geochartLayers: TypeJsonObject[];
// visibleGeochartLayers: string[];

actions: {
setGeochartCharts: (charts: GeoChartStoreByLayerPath) => void;
// setGeochartLayers: (layers: TypeJsonObject) => void;
};
}
// #endregion INTERFACES

export function initializeGeochartState(set: TypeSetStore, get: TypeGetStore): IGeochartState {
const init = {
geochartChartsConfig: {},
// geochartLayers: {},
// geochartChartsConfig: [],
// visibleGeochartLayers: [],

// #region ACTIONS
actions: {
setGeochartCharts(charts: GeoChartStoreByLayerPath): void {
set({
geochartState: {
...get().geochartState,
geochartChartsConfig: charts,
},
});
},
// #endregion ACTIONS
},
} as IGeochartState;

return init;
}

// **********************************************************
// Layer state selectors
// **********************************************************
export const useGeochartConfigs = () => useStore(useGeoViewStore(), (state) => state.geochartState.geochartChartsConfig);
// export const useGeochartLayers = () => useStore(useGeoViewStore(), (state) => state.geochartState.geochartLayers);
// export const useVisibleGeochartLayers = () => useStore(useGeoViewStore(), (state) => state.geochartState.visibleGeochartLayers);

export const useGeochartStoreActions = () => useStore(useGeoViewStore(), (state) => state.geochartState.actions);
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ function findLayerByPath(layers: TypeLegendLayer[], layerPath: string): TypeLege
if (layerPath === l.layerPath) {
return l;
}
if (layerPath.startsWith(l.layerPath) && l.children?.length > 0) {
if (layerPath?.startsWith(l.layerPath) && l.children?.length > 0) {
const result: TypeLegendLayer | undefined = findLayerByPath(l.children, layerPath);
if (result) {
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useGeoViewStore } from '../stores-managers';
import { TypeGetStore, TypeSetStore } from '../geoview-store';
import { TimeSliderEventProcessor } from '@/api/event-processors/event-processor-children/time-slider-event-processor';

// #region INTERFACES
export interface TypeTimeSliderValues {
title?: string;
description?: string;
Expand All @@ -23,6 +24,7 @@ export interface TypeTimeSliderValues {
export interface ITimeSliderState {
timeSliderLayers: { [index: string]: TypeTimeSliderValues };
visibleTimeSliderLayers: string[];

actions: {
addTimeSliderLayer: (newLayer: { [index: string]: TypeTimeSliderValues }) => void;
applyFilters: (layerPath: string, values: number[]) => void;
Expand All @@ -38,11 +40,14 @@ export interface ITimeSliderState {
setVisibleTimeSliderLayers: (visibleLayerPaths: string[]) => void;
};
}
// #endregion INTERFACES

export function initializeTimeSliderState(set: TypeSetStore, get: TypeGetStore): ITimeSliderState {
const init = {
timeSliderLayers: {},
visibleTimeSliderLayers: [],

// #region ACTIONS
actions: {
addTimeSliderLayer(newLayer: { [index: string]: TypeTimeSliderValues }): void {
set({
Expand Down Expand Up @@ -157,6 +162,7 @@ export function initializeTimeSliderState(set: TypeSetStore, get: TypeGetStore):
},
});
},
// #endregion ACTIONS
},
} as ITimeSliderState;

Expand All @@ -168,4 +174,5 @@ export function initializeTimeSliderState(set: TypeSetStore, get: TypeGetStore):
// **********************************************************
export const useTimeSliderLayers = () => useStore(useGeoViewStore(), (state) => state.timeSliderState.timeSliderLayers);
export const useVisibleTimeSliderLayers = () => useStore(useGeoViewStore(), (state) => state.timeSliderState.visibleTimeSliderLayers);

export const useTimeSliderStoreActions = () => useStore(useGeoViewStore(), (state) => state.timeSliderState.actions);
5 changes: 1 addition & 4 deletions packages/geoview-core/src/ui/autocomplete/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ export interface TypeAutocompleteProps<
DisableClearable extends boolean | undefined = undefined,
FreeSolo extends boolean | undefined = undefined
> extends AutocompleteProps<T, Multiple, DisableClearable, FreeSolo> {
// eslint-disable-next-line react/require-default-props
mapId?: string;
// eslint-disable-next-line react/require-default-props
fullWidth?: boolean;
fullWidth: boolean;
}

/**
Expand Down
Loading

0 comments on commit 9e24f7b

Please sign in to comment.