Skip to content

Commit

Permalink
refactor: Deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Jan 21, 2022
1 parent 4d774f8 commit c180f0b
Showing 1 changed file with 45 additions and 55 deletions.
100 changes: 45 additions & 55 deletions app/configurator/components/chart-configurator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,42 @@ import {
import { Loading } from "../../components/hint";
import { Box } from "theme-ui";

const DataFilterSelectGeneric = ({
dimension,
index,
}: {
dimension: DataCubeMetadata["dimensions"][number];
isOptional?: boolean;
index: number;
}) => {
return (
<Box sx={{ px: 2, mb: 2 }}>
{dimension.__typename === "TemporalDimension" ? (
<DataFilterSelectTime
dimensionIri={dimension.iri}
label={`${index + 1}. ${dimension.label}`}
from={dimension.values[0].value}
to={dimension.values[1].value}
timeUnit={dimension.timeUnit}
timeFormat={dimension.timeFormat}
disabled={false}
id={`select-single-filter-${index}`}
isOptional={!dimension.isKeyDimension}
/>
) : (
<DataFilterSelect
dimensionIri={dimension.iri}
label={`${index + 1}. ${dimension.label}`}
options={dimension.values}
disabled={false}
id={`select-single-filter-${index}`}
isOptional={!dimension.isKeyDimension}
/>
)}
</Box>
);
};

export const ChartConfigurator = ({
state,
}: {
Expand All @@ -31,12 +67,9 @@ export const ChartConfigurator = ({

if (data?.dataCubeByIri) {
const mappedIris = getFieldComponentIris(state.chartConfig.fields);
const requiredFilterDimensions = data?.dataCubeByIri.dimensions.filter(
(dim) => !mappedIris.has(dim.iri) && dim.isKeyDimension
);
const optionalFilterDimensions = data?.dataCubeByIri.dimensions.filter(
(dim) => !mappedIris.has(dim.iri) && !dim.isKeyDimension
);
const filterDimensions = data?.dataCubeByIri.dimensions
.filter((dim) => !mappedIris.has(dim.iri))
.sort((a, b) => (a.isKeyDimension ? 0 : 1));

return (
<>
Expand All @@ -61,55 +94,12 @@ export const ChartConfigurator = ({
<Trans id="controls.section.data.filters">Filters</Trans>
</SectionTitle>
<ControlSectionContent side="left" aria-labelledby="controls-data">
{requiredFilterDimensions.map((dimension, i) => (
<Box sx={{ px: 2, mb: 2 }} key={dimension.iri}>
{dimension.__typename === "TemporalDimension" ? (
<DataFilterSelectTime
dimensionIri={dimension.iri}
label={dimension.label}
from={dimension.values[0].value}
to={dimension.values[1].value}
timeUnit={dimension.timeUnit}
timeFormat={dimension.timeFormat}
disabled={false}
id={`select-single-filter-${i}`}
/>
) : (
<DataFilterSelect
dimensionIri={dimension.iri}
label={dimension.label}
options={dimension.values}
disabled={false}
id={`select-single-filter-${i}`}
/>
)}
</Box>
))}
{optionalFilterDimensions.map((dimension, i) => (
<Box sx={{ px: 2, mb: 2 }} key={dimension.iri}>
{dimension.__typename === "TemporalDimension" ? (
<DataFilterSelectTime
dimensionIri={dimension.iri}
label={dimension.label}
from={dimension.values[0].value}
to={dimension.values[1].value}
timeUnit={dimension.timeUnit}
timeFormat={dimension.timeFormat}
disabled={false}
isOptional
id={`select-single-filter-${i}`}
/>
) : (
<DataFilterSelect
dimensionIri={dimension.iri}
label={dimension.label}
options={dimension.values}
disabled={false}
isOptional
id={`select-single-filter-${i}`}
/>
)}
</Box>
{filterDimensions.map((dimension, i) => (
<DataFilterSelectGeneric
key={dimension.iri}
dimension={dimension}
index={i}
/>
))}
</ControlSectionContent>
</ControlSection>
Expand Down

0 comments on commit c180f0b

Please sign in to comment.