-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic chart editor for chart views
- Loading branch information
1 parent
9e97c78
commit a35c831
Showing
4 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { computed, runInAction } from "mobx" | ||
import { | ||
AbstractChartEditor, | ||
AbstractChartEditorManager, | ||
type EditorTab, | ||
} from "./AbstractChartEditor.js" | ||
|
||
export interface Chart { | ||
id: number | ||
title?: string | ||
variantName?: string | ||
isChild: boolean | ||
} | ||
|
||
export interface ChartViewEditorManager extends AbstractChartEditorManager { | ||
chartViewId: number | ||
} | ||
|
||
export class ChartViewEditor extends AbstractChartEditor<ChartViewEditorManager> { | ||
constructor(props: { manager: ChartViewEditorManager }) { | ||
super(props) | ||
} | ||
|
||
@computed | ||
get availableTabs(): EditorTab[] { | ||
const tabs: EditorTab[] = ["basic", "data", "text", "customize"] | ||
if (this.grapher.hasMapTab) tabs.push("map") | ||
if (this.grapher.isScatter) tabs.push("scatter") | ||
if (this.grapher.isMarimekko) tabs.push("marimekko") | ||
tabs.push("inheritance") | ||
tabs.push("debug") | ||
return tabs | ||
} | ||
|
||
@computed get chartViewId(): number { | ||
return this.manager.chartViewId | ||
} | ||
|
||
@computed get isNewGrapher(): boolean { | ||
return false | ||
} | ||
|
||
async saveGrapher({ | ||
onError, | ||
}: { onError?: () => void } = {}): Promise<void> { | ||
const { patchConfig, chartViewId } = this | ||
|
||
const json = await this.manager.admin.requestJSON( | ||
`/api/chartViews/${chartViewId}`, | ||
{ config: patchConfig }, | ||
"PUT" | ||
) | ||
|
||
if (json.success) { | ||
runInAction(() => { | ||
this.savedPatchConfig = json.savedPatch | ||
}) | ||
} else { | ||
onError?.() | ||
} | ||
} | ||
} | ||
|
||
export function isChartViewEditorInstance( | ||
editor: AbstractChartEditor | ||
): editor is ChartViewEditor { | ||
return editor instanceof ChartViewEditor | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React from "react" | ||
import { observer } from "mobx-react" | ||
import { computed, action } 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" | ||
|
||
@observer | ||
export class ChartViewEditorPage | ||
extends React.Component<{ | ||
chartViewId: number | ||
}> | ||
implements ChartViewEditorManager, ChartEditorViewManager<ChartViewEditor> | ||
{ | ||
static contextType = AdminAppContext | ||
context!: AdminAppContextType | ||
|
||
idAndSlug: { id: number; slug: string } | undefined = undefined | ||
|
||
patchConfig: GrapherInterface = {} | ||
fullConfig: GrapherInterface = {} | ||
parentChartId: number = 0 | ||
parentChartConfigFull: GrapherInterface = {} | ||
|
||
isInheritanceEnabled: boolean | undefined = undefined | ||
|
||
async fetchChartViewData(): Promise<void> { | ||
const data = await this.context.admin.getJSON( | ||
`/api/chartViews/${this.chartViewId}` | ||
) | ||
|
||
this.idAndSlug = { id: data.id, slug: data.slug } | ||
this.fullConfig = data.configFull | ||
this.patchConfig = data.configPatch | ||
this.parentChartId = data.parentChartId | ||
this.parentChartConfigFull = 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 }) | ||
} | ||
|
||
@action.bound refresh(): void { | ||
void this.fetchChartViewData() | ||
} | ||
|
||
componentDidMount(): void { | ||
this.refresh() | ||
} | ||
|
||
render(): React.ReactElement { | ||
return <ChartEditorView manager={this} /> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters