From 9d3f68262635e0a2ceaa66ee775c89a38ad1ece0 Mon Sep 17 00:00:00 2001 From: Stefanos Hadjipetrou Date: Thu, 20 Jul 2023 15:16:18 +0300 Subject: [PATCH] fix: pass valid only mapping fields --- src/app/hooks/useChartsRawData.tsx | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/app/hooks/useChartsRawData.tsx b/src/app/hooks/useChartsRawData.tsx index bc2b9eac5..050ba49ce 100644 --- a/src/app/hooks/useChartsRawData.tsx +++ b/src/app/hooks/useChartsRawData.tsx @@ -1,6 +1,7 @@ /* third-party */ import React from "react"; import get from "lodash/get"; +import filter from "lodash/filter"; import isEmpty from "lodash/isEmpty"; import { useParams } from "react-router-dom"; import { useMount, useUpdateEffect } from "react-use"; @@ -150,7 +151,7 @@ export function useChartsRawData(props: { setLoading(true); axios .post( - `${process.env.REACT_APP_API}/chart/${chartId || page}/render`, + `${process.env.REACT_APP_API}/chart/${chartId ?? page}/render`, body, { headers: { @@ -214,11 +215,27 @@ export function useChartsRawData(props: { if (extraLoader) { extraLoader.style.display = "block"; } + const dimensionKeys = + chartFromAPI?.dimensions?.map((item: any) => item.id) || []; + const validMappingKeys = filter( + Object.keys(mapping), + (key: string) => dimensionKeys.indexOf(key) > -1 + ); + let validMapping = {}; + if (validMappingKeys.length === 0) { + validMapping = mapping; + } + validMappingKeys.forEach((key: string) => { + validMapping = { + ...validMapping, + [key]: mapping[key], + }; + }); const body = { rows: [ [ { - mapping, + mapping: validMapping, vizType: selectedChartType, datasetId: dataset, vizOptions: visualOptions,