Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added polling when getting map layers from RPC #252

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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