Skip to content

Commit

Permalink
fix(ui-map): reload map data on study revisit
Browse files Browse the repository at this point in the history
  • Loading branch information
hdinia committed Feb 8, 2024
1 parent bac31cb commit c2006a5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
10 changes: 10 additions & 0 deletions webapp/src/redux/ducks/studySyntheses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
LaunchJobDTO,
Link,
LinkElement,
StudyMetadata,
WSMessage,
} from "../../common/types";
import * as api from "../../services/api/study";
Expand All @@ -33,12 +34,14 @@ export interface StudySynthesesState
currentArea: string;
currentLink: string;
currentBindingConst: string;
prevStudyId: StudyMetadata["id"] | null;
}

const initialState = studySynthesesAdapter.getInitialState({
currentArea: "",
currentLink: "",
currentBindingConst: "",
prevStudyId: null,
}) as StudySynthesesState;

const n = makeActionName("studySyntheses");
Expand Down Expand Up @@ -78,6 +81,10 @@ export const deleteStudyLink = createAction<{
area2: LinkElement["area2"];
}>(n("DELETE_STUDY_LINK"));

export const setPrevStudyId = createAction<StudyMetadata["id"] | null>(
n("SET_PREV_STUDY_ID"),
);

////////////////////////////////////////////////////////////////
// Thunks
////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -196,5 +203,8 @@ export default createReducer(initialState, (builder) => {
})
.addCase(setCurrentBindingConst, (draftState, action) => {
draftState.currentBindingConst = action.payload;
})
.addCase(setPrevStudyId, (draftState, action) => {
draftState.prevStudyId = action.payload;
});
});
62 changes: 39 additions & 23 deletions webapp/src/redux/hooks/useStudyMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,66 @@ import {
import useStudySynthesis from "./useStudySynthesis";
import { Response } from "../../components/common/utils/UsePromiseCond";
import usePromise, { PromiseStatus } from "../../hooks/usePromise";
import { setPrevStudyId } from "../ducks/studySyntheses";

interface Props<T> {
studyId: StudyMetadata["id"];
selector?: (state: AppState, studyId: StudyMetadata["id"]) => T;
selector: (state: AppState, studyId: StudyMetadata["id"]) => T;
}

export default function useStudyMaps<T>(props: Props<T>): Response<T> {
const { studyId, selector } = props;
const synthesisRes = useStudySynthesis({ studyId });
const isMapsExist = useAppSelector((state) => !!getStudyMap(state, studyId));
const data = useAppSelector((state) =>
isMapsExist && selector ? selector(state, studyId) : undefined,
);
const dispatch = useAppDispatch();
export default function useStudyMaps<T>({
studyId,
selector,
}: Props<T>): Response<T> {
const [status, setStatus] = useState(PromiseStatus.Idle);
const [error, setError] = useState<Response["error"]>();
const dispatch = useAppDispatch();
const synthesis = useStudySynthesis({ studyId });
const isMapsExists = useAppSelector((state) => !!getStudyMap(state, studyId));
const data = useAppSelector((state) => selector(state, studyId) || undefined);
const prevStudyId = useAppSelector(
(state) => state.studySyntheses.prevStudyId,
);

usePromise(async () => {
if (synthesisRes.status === PromiseStatus.Rejected) {
setError(synthesisRes.error);
if (synthesis.status === PromiseStatus.Rejected) {
setError(synthesis.error);
setStatus(PromiseStatus.Rejected);
return;
}

if (synthesisRes.status !== PromiseStatus.Resolved) {
if (synthesis.status !== PromiseStatus.Resolved) {
setStatus(PromiseStatus.Pending);
return;
}

if (!isMapsExist) {
setStatus(PromiseStatus.Pending);
setStatus(PromiseStatus.Pending);

try {
/*
* Conditionally fetch study map data to address two scenarios:
* 1. The map data does not exist for the current study (isMapsExist is false).
* 2. The user has navigated to a different study than previously viewed (prevStudyId !== studyId).
*
* Particularly critical for accuracy when users return to a previously viewed study.
*/
if (!isMapsExists || prevStudyId !== studyId) {
await Promise.all([
dispatch(fetchStudyMapLayers(studyId)).unwrap(),
dispatch(fetchStudyMapDistricts(studyId)).unwrap(),
dispatch(createStudyMap(studyId)).unwrap(),
]);

try {
await dispatch(fetchStudyMapLayers(studyId)).unwrap();
await dispatch(fetchStudyMapDistricts(studyId)).unwrap();
await dispatch(createStudyMap(studyId)).unwrap();
} catch (e) {
setError(e as Error);
setStatus(PromiseStatus.Rejected);
// Updates the previous study ID to track navigation between studies.
dispatch(setPrevStudyId(studyId));
}
} else {

setStatus(PromiseStatus.Resolved);
} catch (err) {
setError(err as Error);
setStatus(PromiseStatus.Rejected);
}
}, [isMapsExist, studyId, synthesisRes.status]);
}, [isMapsExists, studyId, synthesis.status]);

return { data, status, error };
}

0 comments on commit c2006a5

Please sign in to comment.