-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathChartEditorPage.tsx
176 lines (158 loc) · 5.44 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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,
MinimalTagWithIsTopic,
DbChartTagJoin,
} from "@ourworldindata/types"
import { Admin } from "./Admin.js"
import {
ChartEditor,
Log,
ChartEditorManager,
fetchMergedGrapherConfigByVariableId,
} from "./ChartEditor.js"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
import { ChartEditorView, ChartEditorViewManager } from "./ChartEditorView.js"
import { References } from "./AbstractChartEditor.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
@observable tags?: DbChartTagJoin[] = undefined
@observable availableTags?: MinimalTagWithIsTopic[] = 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))
}
async fetchTags(): Promise<void> {
const { grapherId } = this.props
const { admin } = this.context
const json =
grapherId === undefined
? {}
: await admin.getJSON(`/api/charts/${grapherId}.tags.json`)
runInAction(() => (this.tags = json.tags))
}
async fetchAvailableTags() {
const json = (await this.admin.getJSON("/api/tags.json")) as any
this.availableTags = json.tags
}
@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()
void this.fetchTags()
void this.fetchAvailableTags()
}
componentDidMount(): void {
this.refresh()
}
componentDidUpdate(
prevProps: Readonly<{
grapherId?: number
grapherConfig?: GrapherInterface
}>
): void {
if (prevProps.grapherId !== this.props.grapherId) {
void this.fetchTags()
}
}
render(): React.ReactElement {
return <ChartEditorView manager={this} />
}
}