Skip to content

Commit

Permalink
Merge pull request #252 from Tampere/fix/too-early-rpc-requests
Browse files Browse the repository at this point in the history
Added polling when getting map layers from RPC
  • Loading branch information
jlaamanen authored Dec 2, 2024
2 parents 25f6b81 + 6301119 commit 150309a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
5 changes: 4 additions & 1 deletion client/src/components/SurveyStepper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ import ImageSection from './ImageSection';
import PageConnector from './PageConnector';
import StepperControls from './StepperControls';
import SubmissionInfoDialog from './SubmissionInfoDialog';
import { SurveyFollowUpSections } from './SurveyFollowUpSections';
import SurveyLanguageMenu from './SurveyLanguageMenu';
import SurveyMap from './SurveyMap';
import SurveyQuestion from './SurveyQuestion';
import TextSection from './TextSection';
import { SurveyFollowUpSections } from './SurveyFollowUpSections';

const useStyles = makeStyles((theme: Theme) => ({
root: {
Expand Down Expand Up @@ -193,6 +193,9 @@ export default function SurveyStepper({
* Update map's visible layers when page changes
*/
useEffect(() => {
if (currentPage.sidebar.type !== 'map') {
return;
}
setVisibleLayers(currentPage.sidebar.mapLayers);
// If modifying, stop it when changing page
if (isMapReady) {
Expand Down
27 changes: 19 additions & 8 deletions client/src/stores/SurveyMapContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -830,15 +830,26 @@ export default function SurveyMapProvider({
return;
}

state.rpcChannel.getAllLayers((allLayers) => {
const layers = allLayers.map((layer) => layer.id);
dispatch({ type: 'SET_ALL_LAYERS', layers });
// Set all layers to be visible by default, unless there already are visible layers declared
dispatch({
type: 'SET_VISIBLE_LAYERS',
layers: state.visibleLayers ?? layers,
// getAllLayers does work here, but we need to wait until Oskari assigns the layers all the way to the internal map state, thus the polling...
// This issue should resolve the problem in some better way: https://github.com/oskariorg/oskari-documentation/issues/58
const interval = setInterval(() => {
state.rpcChannel.getCurrentState(({ mapfull }) => {
const layers = mapfull.state.selectedLayers.map((layer) => layer.id);
if (!layers.length) {
return;
}
clearInterval(interval);
dispatch({ type: 'SET_ALL_LAYERS', layers });
dispatch({
type: 'SET_VISIBLE_LAYERS',
layers: state.visibleLayers ?? layers,
});
});
});
}, 100);

return () => {
clearInterval(interval);
};
}, [state.rpcChannel]);

/**
Expand Down
17 changes: 17 additions & 0 deletions client/src/typings/oskari-rpc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,22 @@ declare module 'oskari-rpc' {
msg?: string;
}

export interface State {
mapfull: {
state: {
north: number;
east: number;
selectedLayers: {
id: number;
opacity: number;
style?: string;
}[];
srs: string;
zoom: number;
};
};
}

/**
* All Oskari events
*/
Expand Down Expand Up @@ -306,6 +322,7 @@ declare module 'oskari-rpc' {
getSupportedFunctions: (callback: (functions: unknown[]) => void) => void;
getInfo: (callback: (info: any) => void) => void;
getMapPosition: (callback: (position: any) => void) => void;
getCurrentState: (callback: (state: State) => void) => void;
/**
* Post an Oskari request
*/
Expand Down

0 comments on commit 150309a

Please sign in to comment.