forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChartEditorPage.tsx
141 lines (126 loc) · 4.43 KB
/
ChartEditorPage.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React from "react"
import { observer } from "mobx-react"
import { observable, computed, runInAction, action } from "mobx"
import {
getParentVariableIdFromChartConfig,
RawPageview,
} from "@ourworldindata/utils"
import { GrapherInterface, ChartRedirect } from "@ourworldindata/types"
import { Admin } from "./Admin.js"
import {
ChartEditor,
Log,
References,
ChartEditorManager,
fetchMergedGrapherConfigByVariableId,
} from "./ChartEditor.js"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
import { ChartEditorView, ChartEditorViewManager } from "./ChartEditorView.js"
@observer
export class ChartEditorPage
extends React.Component<{
grapherId?: number
grapherConfig?: GrapherInterface
}>
implements ChartEditorManager, ChartEditorViewManager<ChartEditor>
{
static contextType = AdminAppContext
context!: AdminAppContextType
@observable logs: Log[] = []
@observable references: References | undefined = undefined
@observable redirects: ChartRedirect[] = []
@observable pageviews?: RawPageview = undefined
patchConfig: GrapherInterface = {}
parentConfig: GrapherInterface | undefined = undefined
isInheritanceEnabled: boolean | undefined = undefined
async fetchGrapherConfig(): Promise<void> {
const { grapherId, grapherConfig } = this.props
if (grapherId !== undefined) {
this.patchConfig = await this.context.admin.getJSON(
`/api/charts/${grapherId}.patchConfig.json`
)
} else if (grapherConfig) {
this.patchConfig = grapherConfig
}
}
async fetchParentConfig(): Promise<void> {
const { grapherId, grapherConfig } = this.props
if (grapherId !== undefined) {
const parent = await this.context.admin.getJSON(
`/api/charts/${grapherId}.parent.json`
)
this.parentConfig = parent?.config
this.isInheritanceEnabled = parent?.isActive ?? true
} else if (grapherConfig) {
const parentIndicatorId =
getParentVariableIdFromChartConfig(grapherConfig)
if (parentIndicatorId) {
this.parentConfig = await fetchMergedGrapherConfigByVariableId(
this.context.admin,
parentIndicatorId
)
}
this.isInheritanceEnabled = true
} else {
this.isInheritanceEnabled = true
}
}
async fetchLogs(): Promise<void> {
const { grapherId } = this.props
const { admin } = this.context
const json =
grapherId === undefined
? {}
: await admin.getJSON(`/api/charts/${grapherId}.logs.json`)
runInAction(() => (this.logs = json.logs))
}
async fetchRefs(): Promise<void> {
const { grapherId } = this.props
const { admin } = this.context
const json =
grapherId === undefined
? {}
: await admin.getJSON(
`/api/charts/${grapherId}.references.json`
)
runInAction(() => (this.references = json.references))
}
async fetchRedirects(): Promise<void> {
const { grapherId } = this.props
const { admin } = this.context
const json =
grapherId === undefined
? {}
: await admin.getJSON(`/api/charts/${grapherId}.redirects.json`)
runInAction(() => (this.redirects = json.redirects))
}
async fetchPageviews(): Promise<void> {
const { grapherId } = this.props
const { admin } = this.context
const json =
grapherId === undefined
? {}
: await admin.getJSON(`/api/charts/${grapherId}.pageviews.json`)
runInAction(() => (this.pageviews = json.pageviews))
}
@computed get admin(): Admin {
return this.context.admin
}
@computed get editor(): ChartEditor {
return new ChartEditor({ manager: this })
}
@action.bound refresh(): void {
void this.fetchGrapherConfig()
void this.fetchParentConfig()
void this.fetchLogs()
void this.fetchRefs()
void this.fetchRedirects()
void this.fetchPageviews()
}
componentDidMount(): void {
this.refresh()
}
render(): React.ReactElement {
return <ChartEditorView manager={this} />
}
}