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

fix(ui-map): reload map data on study revisit #1927

Merged
merged 1 commit into from
Feb 9, 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
3 changes: 3 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 @@ -297,6 +299,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
56 changes: 33 additions & 23 deletions webapp/src/redux/hooks/useStudyMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,57 @@ import usePromise, { PromiseStatus } from "../../hooks/usePromise";

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(),
]);
}
} 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 };
}
Loading