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 5d7aab1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
5 changes: 5 additions & 0 deletions webapp/src/redux/ducks/studies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface StudiesSortConf {

export interface StudiesState extends AsyncEntityState<StudyMetadata> {
current: string;
prevStudyId: string;
scrollPosition: number;
versionList: string[];
favorites: Array<StudyMetadata["id"]>;
Expand All @@ -73,6 +74,7 @@ const initialState = studiesAdapter.getInitialState({
status: FetchStatus.Idle,
error: null as string | null,
current: "",
prevStudyId: "",
scrollPosition: 0,
versionList: [] as string[],
favorites: [],
Expand Down Expand Up @@ -100,6 +102,8 @@ const n = makeActionName("study");
// Action Creators
////////////////////////////////////////////////////////////////

export const setPrevStudyId = createAction<string>(n("SET_PREV_STUDY_ID"));

export const setCurrentStudy = createThunk<
NonNullable<StudiesState["current"]>,
NonNullable<StudiesState["current"]>
Expand Down Expand Up @@ -297,6 +301,7 @@ export default createReducer(initialState, (builder) => {
draftState.versionList = action.payload;
})
.addCase(setCurrentStudy, (draftState, action) => {
draftState.prevStudyId = draftState.current;
draftState.current = action.payload;
})
.addCase(setStudyScrollPosition, (draftState, action) => {
Expand Down
60 changes: 37 additions & 23 deletions webapp/src/redux/hooks/useStudyMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,64 @@ import {
import useStudySynthesis from "./useStudySynthesis";
import { Response } from "../../components/common/utils/UsePromiseCond";
import usePromise, { PromiseStatus } from "../../hooks/usePromise";
import { setPrevStudyId } from "../ducks/studies";

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.studies.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 {
await dispatch(fetchStudyMapLayers(studyId)).unwrap();
await dispatch(fetchStudyMapDistricts(studyId)).unwrap();
await dispatch(createStudyMap(studyId)).unwrap();
} catch (e) {
setError(e as Error);
setStatus(PromiseStatus.Rejected);
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(),
]);

// 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 5d7aab1

Please sign in to comment.