Skip to content

Commit

Permalink
implement setting narrowing based on choices
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed May 30, 2024
1 parent 26f1b2f commit f47861a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
44 changes: 44 additions & 0 deletions site/multi-dim/MultiDimDataPageConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,50 @@ export class MultiDimDataPageConfig {
return matchingViews[0]
}

filterToAvailableChoices(selectedChoices: Record<string, string>) {
const updatedSelectedChoices: Record<string, string> = {}
const dimensionsWithAvailableChoices = this.dimensions
for (const [dimSlug, dim] of Object.entries(
dimensionsWithAvailableChoices
)) {
const availableViewsBeforeSelection = this.filterViewsByDimensions(
updatedSelectedChoices
)
if (
selectedChoices[dimSlug] &&
dim.choices[selectedChoices[dimSlug]]
) {
updatedSelectedChoices[dimSlug] = selectedChoices[dimSlug]
} else {
throw new Error(
`Missing or invalid choice for dimension ${dimSlug}`
)
}

const availableViewsAfterSelection = this.filterViewsByDimensions(
updatedSelectedChoices
)
if (availableViewsAfterSelection.length === 0) {
updatedSelectedChoices[dimSlug] = Object.values(
dim.choices
)[0].slug
}

dim.choices = Object.fromEntries(
Object.entries(dim.choices).filter(([slug, _]) =>
availableViewsBeforeSelection.some(
(view) => view.dimensions[dimSlug] === slug
)
)
)
}

return {
selectedChoices: updatedSelectedChoices,
dimensionsWithAvailableChoices,
}
}

static transformIndicatorPathObj(
indicatorPathObj: Record<string, DimensionProperty>
): Record<DimensionProperty, string[]> {
Expand Down
17 changes: 16 additions & 1 deletion site/multi-dim/MultiDimDataPageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
memoize,
Url,
getWindowQueryParams,
isEqual,
} from "@ourworldindata/utils"
import { AttachmentsContext, DocumentContext } from "../gdocs/OwidGdoc.js"
import StickyNav from "../blocks/StickyNav.js"
Expand Down Expand Up @@ -236,12 +237,26 @@ const MultiDimSettingsPanel = (props: {

const [currentSettings, setCurrentSettings] = useState(initialSettings)

const [availableSettings, setAvailableSettings] = useState<
Record<string, DimensionWithChoicesKeyedBySlug>
>({})

useEffect(() => {
if (!config) return
const { dimensionsWithAvailableChoices, selectedChoices } =
config.filterToAvailableChoices(currentSettings)

setAvailableSettings(dimensionsWithAvailableChoices)
if (!isEqual(selectedChoices, currentSettings))
setCurrentSettings(selectedChoices)
}, [currentSettings, config])

useEffect(
() => props.updateSettings?.(currentSettings),
[props, currentSettings]
)

const settings = Object.values(dimensions).map((dim) => (
const settings = Object.values(availableSettings).map((dim) => (
<DimensionDropdown
key={dim.slug}
dimension={dim}
Expand Down

0 comments on commit f47861a

Please sign in to comment.