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

Clean up measurements Redux states/actions #1881

Merged
merged 12 commits into from
Nov 6, 2024
Merged
41 changes: 15 additions & 26 deletions src/actions/loadData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import { getServerAddress } from "../util/globals";
import { goTo404 } from "./navigation";
import { createStateFromQueryOrJSONs, createTreeTooState, getNarrativePageFromQuery } from "./recomputeReduxState";
import { loadFrequencies } from "./frequencies";
import { parseMeasurementsJSON, loadMeasurements } from "./measurements";
import { fetchJSON, fetchWithErrorHandling } from "../util/serverInteraction";
import { warningNotification, errorNotification } from "./notifications";
import { parseMarkdownNarrativeFile } from "../util/parseNarrative";
import { NoContentError, FetchError} from "../util/exceptions";
import { parseMarkdown } from "../util/parseMarkdown";
import { updateColorByWithRootSequenceData } from "../actions/colors";
import { explodeTree } from "./tree";
import { togglePanelDisplay } from "./panelDisplay";

export function getDatasetNamesFromUrl(url) {
let secondTreeUrl;
Expand Down Expand Up @@ -121,13 +119,15 @@ function narrativeFetchingErrorNotification(err) {
*/
async function dispatchCleanStart(dispatch, main, second, query, narrativeBlocks) {
const json = await main.main;
const measurementsData = main.measurements ? (await main.measurements) : undefined;
const secondTreeDataset = second ? (await second.main) : undefined;
const pathnameShouldBe = second ? `${main.pathname}:${second.pathname}` : main.pathname;
dispatch({
type: types.CLEAN_START,
pathnameShouldBe: narrativeBlocks ? undefined : pathnameShouldBe,
...createStateFromQueryOrJSONs({
json,
measurementsData,
secondTreeDataset,
query,
narrativeBlocks,
Expand Down Expand Up @@ -280,7 +280,19 @@ Dataset.prototype.fetchMain = function fetchMain() {
}
return res;
})
.then((res) => res.json());
.then((res) => res.json())
.then((json) => {
if (json.meta.panels && json.meta.panels.includes("measurements") && !this.measurements) {
/**
* Fetch measurements and store the resulting promise.
* Avoid the browser's default unhandled promise rejection logging and
* just resolve to an Error object that will be handled appropriately in loadMeasurements.
*/
this.measurements = fetchJSON(this.apiCalls.measurements)
.catch((reason) => Promise.resolve(reason));
}
return json;
});
};
Dataset.prototype.fetchSidecars = async function fetchSidecars() {
/**
Expand All @@ -304,12 +316,6 @@ Dataset.prototype.fetchSidecars = async function fetchSidecars() {
this.rootSequence = fetchJSON(this.apiCalls.rootSequence)
.catch((reason) => Promise.resolve(reason))
}

if (mainJson.meta.panels && mainJson.meta.panels.includes("measurements") && !this.measurements) {
this.measurements = fetchJSON(this.apiCalls.measurements)
.then((json) => parseMeasurementsJSON(json))
.catch((reason) => Promise.resolve(reason))
}
};
Dataset.prototype.loadSidecars = function loadSidecars(dispatch) {
/* Helper function to load (dispatch) the visualisation of sidecar files.
Expand Down Expand Up @@ -346,23 +352,6 @@ Dataset.prototype.loadSidecars = function loadSidecars(dispatch) {
dispatch(warningNotification({message: "Failed to parse root sequence JSON"}));
})
}
if (this.measurements) {
this.measurements
.then((data) => {
if (data instanceof Error) throw data;
return data
})
.then((data) => dispatch(loadMeasurements(data)))
.catch((err) => {
const errorMessage = `Failed to ${err instanceof FetchError ? 'fetch' : 'parse'} measurements collections`;
console.error(errorMessage, err.message);
dispatch(warningNotification({message: errorMessage}));
// Hide measurements panel
dispatch(togglePanelDisplay("measurements"));
// Save error message to display if user toggles panel again
dispatch({ type: types.UPDATE_MEASUREMENTS_ERROR, data: errorMessage });
});
}
};
Dataset.prototype.fetchAvailable = async function fetchAvailable() {
this.available = fetchJSON(this.apiCalls.getAvailable);
Expand Down
Loading