Skip to content
Snippets Groups Projects
Unverified Commit 256835b0 authored by Alexander Polyankin's avatar Alexander Polyankin Committed by GitHub
Browse files

Load card metadata recursively (#41963)

parent 5df74763
No related branches found
No related tags found
No related merge requests found
......@@ -11,22 +11,56 @@ export interface LoadMetadataOptions {
}
export const loadMetadataForCard =
(card: Card, options?: LoadMetadataOptions) =>
(card: Card, options?: LoadMetadataOptions) => async (dispatch: Dispatch) => {
await dispatch(loadDependentMetadata(card, [], options));
};
const loadDependentMetadata =
(
card: Card,
dependencies: Lib.DependentItem[],
options?: LoadMetadataOptions,
) =>
async (dispatch: Dispatch, getState: GetState) => {
const question = new Question(card, getMetadata(getState()));
const loadAdhocMetadata =
question.isSaved() && question.type() !== "question";
const dependencies = [...Lib.dependentMetadata(question.query())];
if (loadAdhocMetadata) {
const tableId = getQuestionVirtualTableId(question.id());
dependencies.push({ id: tableId, type: "table" });
}
await dispatch(loadMetadataForDependentItems(dependencies, options));
if (loadAdhocMetadata) {
const questionWithMetadata = new Question(card, getMetadata(getState()));
const adhocQuestion = questionWithMetadata.composeQuestionAdhoc();
const adhocDependencies = Lib.dependentMetadata(adhocQuestion.query());
await dispatch(loadMetadataForDependentItems(adhocDependencies, options));
const withAdhocMetadata = shouldLoadAdhocMetadata(question);
const nextDependencies = getDependencies(question, withAdhocMetadata);
const dependenciesDiff = getDependenciesDiff(
dependencies,
nextDependencies,
);
if (dependenciesDiff.length > 0) {
await dispatch(loadMetadataForDependentItems(dependenciesDiff, options));
const mergedDependencies = [...dependencies, ...dependenciesDiff];
await dispatch(loadDependentMetadata(card, mergedDependencies, options));
} else if (withAdhocMetadata) {
const adhocCard = question.composeQuestionAdhoc().card();
await dispatch(loadDependentMetadata(adhocCard, dependencies, options));
}
};
function shouldLoadAdhocMetadata(question: Question) {
return question.isSaved() && question.type() !== "question";
}
function getDependencies(question: Question, withAdhocMetadata: boolean) {
const dependencies = [...Lib.dependentMetadata(question.query())];
if (withAdhocMetadata) {
const tableId = getQuestionVirtualTableId(question.id());
dependencies.push({ id: tableId, type: "table" });
}
return dependencies;
}
function getDependencyKey(dependency: Lib.DependentItem) {
return `${dependency.type}/${dependency.id}`;
}
function getDependenciesDiff(
prevDependencies: Lib.DependentItem[],
nextDependencies: Lib.DependentItem[],
) {
const prevKeys = new Set(prevDependencies.map(getDependencyKey));
return nextDependencies.filter(dep => !prevKeys.has(getDependencyKey(dep)));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment