Skip to content

Commit

Permalink
Add inheritance column to charts list
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Sep 4, 2024
1 parent c37bcf8 commit d629147
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
10 changes: 10 additions & 0 deletions adminSiteClient/ChartList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export interface ChartListItem {
publishedAt: string
publishedBy: string

hasParentIndicator?: boolean
isInheritanceEnabled?: boolean

tags: DbChartTagJoin[]
}

Expand Down Expand Up @@ -101,6 +104,11 @@ export class ChartList extends React.Component<{
render() {
const { charts, searchHighlight } = this.props
const { availableTags } = this

// if the first chart has inheritance information, we assume all charts have it
const showInheritanceColumn =
charts[0]?.isInheritanceEnabled !== undefined

return (
<table className="table table-bordered">
<thead>
Expand All @@ -109,6 +117,7 @@ export class ChartList extends React.Component<{
<th>Chart</th>
<th>Id</th>
<th>Type</th>
{showInheritanceColumn && <th>Inheritance</th>}
<th>Tags</th>
<th>Published</th>
<th>Last Updated</th>
Expand All @@ -124,6 +133,7 @@ export class ChartList extends React.Component<{
availableTags={availableTags}
searchHighlight={searchHighlight}
onDelete={this.onDeleteChart}
showInheritanceColumn={showInheritanceColumn}
/>
))}
</tbody>
Expand Down
19 changes: 18 additions & 1 deletion adminSiteClient/ChartRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class ChartRow extends React.Component<{
searchHighlight?: (text: string) => string | React.ReactElement
availableTags: DbChartTagJoin[]
onDelete: (chart: ChartListItem) => void
showInheritanceColumn?: boolean
}> {
static contextType = AdminAppContext
context!: AdminAppContextType
Expand All @@ -40,7 +41,8 @@ export class ChartRow extends React.Component<{
}

render() {
const { chart, searchHighlight, availableTags } = this.props
const { chart, searchHighlight, availableTags, showInheritanceColumn } =
this.props

const highlight = searchHighlight || lodash.identity

Expand Down Expand Up @@ -80,6 +82,7 @@ export class ChartRow extends React.Component<{
</td>
<td style={{ minWidth: "100px" }}>{chart.id}</td>
<td style={{ minWidth: "100px" }}>{showChartType(chart)}</td>
{showInheritanceColumn && <InheritanceStatus chart={chart} />}
<td style={{ minWidth: "340px" }}>
<EditableTags
tags={chart.tags}
Expand Down Expand Up @@ -122,3 +125,17 @@ export class ChartRow extends React.Component<{
)
}
}

function InheritanceStatus({ chart }: { chart: ChartListItem }) {
// if no information is available, return an empty cell
if (
chart.hasParentIndicator === undefined ||
chart.isInheritanceEnabled === undefined
)
return <td></td>

// if the chart doesn't have a parent, inheritance doesn't apply
if (!chart.hasParentIndicator) return <td>n/a</td>

return chart.isInheritanceEnabled ? <td>enabled</td> : <td>disabled</td>
}
21 changes: 19 additions & 2 deletions adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import {
mergeGrapherConfigs,
diffGrapherConfigs,
omitUndefinedValues,
getParentVariableIdFromChartConfig,
omit,
} from "@ourworldindata/utils"
import { applyPatch } from "../adminShared/patchHelper.js"
import {
Expand Down Expand Up @@ -94,6 +96,7 @@ import {
DbInsertUser,
FlatTagGraph,
DbRawChartConfig,
parseChartConfig,
} from "@ourworldindata/types"
import { uuidv7 } from "uuidv7"
import {
Expand Down Expand Up @@ -1408,10 +1411,15 @@ getRouteWithROTransaction(
variable.catalogPath += `#${variable.shortName}`
}

const charts = await db.knexRaw<OldChartFieldList>(
const rawCharts = await db.knexRaw<
OldChartFieldList & {
isInheritanceEnabled: DbPlainChart["isInheritanceEnabled"]
config: DbRawChartConfig["full"]
}
>(
trx,
`-- sql
SELECT ${oldChartFieldList}
SELECT ${oldChartFieldList}, charts.isInheritanceEnabled, chart_configs.full AS config
FROM charts
JOIN chart_configs ON chart_configs.id = charts.configId
JOIN users lastEditedByUser ON lastEditedByUser.id = charts.lastEditedByUserId
Expand All @@ -1423,6 +1431,15 @@ getRouteWithROTransaction(
[variableId]
)

// check for parent indicators
const charts = rawCharts.map((chart) => {
const parentIndicatorId = getParentVariableIdFromChartConfig(
parseChartConfig(chart.config)
)
const hasParentIndicator = parentIndicatorId !== undefined
return omit({ ...chart, hasParentIndicator }, "config")
})

await assignTagsForCharts(trx, charts)

const variableWithConfigs = await getGrapherConfigsForVariable(
Expand Down

0 comments on commit d629147

Please sign in to comment.