forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndicatorChartEditorPage.tsx
94 lines (81 loc) · 2.79 KB
/
IndicatorChartEditorPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React from "react"
import { observer } from "mobx-react"
import { computed, action } from "mobx"
import { isEmpty } from "@ourworldindata/utils"
import { GrapherInterface, DimensionProperty } from "@ourworldindata/types"
import { Admin } from "./Admin.js"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
import { ChartEditorView, ChartEditorViewManager } from "./ChartEditorView.js"
import {
IndicatorChartEditor,
IndicatorChartEditorManager,
type Chart,
} from "./IndicatorChartEditor.js"
import { defaultGrapherConfig } from "@ourworldindata/grapher"
@observer
export class IndicatorChartEditorPage
extends React.Component<{
variableId: number
}>
implements
IndicatorChartEditorManager,
ChartEditorViewManager<IndicatorChartEditor>
{
static contextType = AdminAppContext
context!: AdminAppContextType
patchConfig: GrapherInterface = {}
parentConfig: GrapherInterface | undefined = undefined
charts: Chart[] = []
isNewGrapher: boolean | undefined = undefined
isInheritanceEnabled: boolean | undefined = undefined
async fetchGrapherConfig(): Promise<void> {
const { variableId } = this
const config = await this.context.admin.getJSON(
`/api/variables/grapherConfigAdmin/${variableId}.patchConfig.json`
)
if (isEmpty(config)) {
this.patchConfig = {
$schema: defaultGrapherConfig.$schema,
dimensions: [{ variableId, property: DimensionProperty.y }],
}
this.isNewGrapher = true
} else {
this.patchConfig = config
this.isNewGrapher = false
}
}
async fetchParentConfig(): Promise<void> {
const { variableId } = this
const etlConfig = await this.context.admin.getJSON(
`/api/variables/grapherConfigETL/${variableId}.patchConfig.json`
)
this.parentConfig = isEmpty(etlConfig) ? undefined : etlConfig
this.isInheritanceEnabled = true
}
async fetchCharts(): Promise<void> {
const { variableId } = this
this.charts = await this.context.admin.getJSON(
`/api/variables/${variableId}/charts.json`
)
}
@computed get admin(): Admin {
return this.context.admin
}
@computed get variableId(): number {
return this.props.variableId
}
@computed get editor(): IndicatorChartEditor {
return new IndicatorChartEditor({ manager: this })
}
@action.bound refresh(): void {
void this.fetchGrapherConfig()
void this.fetchParentConfig()
void this.fetchCharts()
}
componentDidMount(): void {
this.refresh()
}
render(): React.ReactElement {
return <ChartEditorView manager={this} />
}
}