-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathChartViewEditorPage.tsx
84 lines (70 loc) · 2.48 KB
/
ChartViewEditorPage.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
import React from "react"
import { observer } from "mobx-react"
import { computed, action, runInAction, observable } from "mobx"
import { GrapherInterface } from "@ourworldindata/types"
import { Admin } from "./Admin.js"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
import { ChartEditorView, ChartEditorViewManager } from "./ChartEditorView.js"
import { ChartViewEditor, ChartViewEditorManager } from "./ChartViewEditor.js"
import { References } from "./AbstractChartEditor.js"
@observer
export class ChartViewEditorPage
extends React.Component<{
chartViewId: number
}>
implements ChartViewEditorManager, ChartEditorViewManager<ChartViewEditor>
{
static contextType = AdminAppContext
context!: AdminAppContextType
idsAndName: { id: number; name: string; configId: string } | undefined =
undefined
patchConfig: GrapherInterface = {}
fullConfig: GrapherInterface = {}
parentChartId: number = 0
parentConfig: GrapherInterface = {}
isInheritanceEnabled: boolean | undefined = true
@observable references: References | undefined = undefined
async fetchChartViewData(): Promise<void> {
const data = await this.context.admin.getJSON(
`/api/chartViews/${this.chartViewId}.config.json`
)
this.idsAndName = {
id: data.id,
name: data.name,
configId: data.chartConfigId,
}
this.fullConfig = data.configFull
this.patchConfig = data.configPatch
this.parentChartId = data.parentChartId
this.parentConfig = data.parentConfigFull
}
@computed get admin(): Admin {
return this.context.admin
}
@computed get chartViewId(): number {
return this.props.chartViewId
}
@computed get editor(): ChartViewEditor {
return new ChartViewEditor({ manager: this })
}
async fetchRefs(): Promise<void> {
const { admin } = this.context
const json =
this.chartViewId === undefined
? {}
: await admin.getJSON(
`/api/chartViews/${this.chartViewId}.references.json`
)
runInAction(() => (this.references = json.references))
}
@action.bound refresh(): void {
void this.fetchChartViewData()
void this.fetchRefs()
}
componentDidMount(): void {
this.refresh()
}
render(): React.ReactElement {
return <ChartEditorView manager={this} />
}
}