Skip to content

Commit

Permalink
✨ add endpoints to enable/disable inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Aug 14, 2024
1 parent d1b74cb commit 9c6be0e
Show file tree
Hide file tree
Showing 4 changed files with 423 additions and 116 deletions.
76 changes: 61 additions & 15 deletions adminSiteClient/ChartEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
mergeGrapherConfigs,
isEmpty,
} from "@ourworldindata/utils"
import { action, computed, observable, runInAction } from "mobx"
import { action, computed, observable, runInAction, when } from "mobx"
import { BAKED_GRAPHER_URL } from "../settings/clientSettings.js"
import {
AbstractChartEditor,
Expand Down Expand Up @@ -59,6 +59,19 @@ export class ChartEditor extends AbstractChartEditor<ChartEditorManager> {
// so the page knows to update the url
@observable.ref newChartId?: number

@observable.ref savedInheritanceEnabled?: boolean | undefined

constructor(props: { manager: ChartEditorManager }) {
super(props)

when(
() => this.manager.isInheritanceEnabled !== undefined,
() =>
(this.savedInheritanceEnabled =
this.manager.isInheritanceEnabled)
)
}

@computed get logs() {
return this.manager.logs
}
Expand Down Expand Up @@ -140,12 +153,9 @@ export class ChartEditor extends AbstractChartEditor<ChartEditorManager> {
if (!patchConfig.title) patchConfig.title = grapher.displayTitle
if (!patchConfig.slug) patchConfig.slug = grapher.displaySlug

const query = new URLSearchParams({
inheritance: this.isInheritanceEnabled ? "1" : "0",
})
const targetUrl = isNewGrapher
? `/api/charts?${query}`
: `/api/charts/${grapher.id}?${query}`
? `/api/charts`
: `/api/charts/${grapher.id}`

const json = await this.manager.admin.requestJSON(
targetUrl,
Expand All @@ -154,15 +164,49 @@ export class ChartEditor extends AbstractChartEditor<ChartEditorManager> {
)

if (json.success) {
// enable/disable inheritance when necessary
let inheritanceJson: Json | undefined
const shouldEnableInheritanceForNewGrapher =
isNewGrapher && this.isInheritanceEnabled
const shouldToggleInheritanceForExistingGrapher =
!isNewGrapher &&
this.isInheritanceEnabled !== this.savedInheritanceEnabled
if (
shouldEnableInheritanceForNewGrapher ||
shouldToggleInheritanceForExistingGrapher
) {
const action = this.isInheritanceEnabled ? "enable" : "disable"
const url = `/api/charts/${grapher.id}/configInheritance/${action}`
inheritanceJson = await this.manager.admin.requestJSON(
url,
{},
"PUT"
)
}

const updateInheritance = () => {
if (!inheritanceJson) return
// this shouldn't happen since we only allow to toggle
// inheritance when it's appropriate
if (!inheritanceJson.success) {
this.isInheritanceEnabled = !this.isInheritanceEnabled
}
this.savedInheritanceEnabled = this.isInheritanceEnabled
}

const savedPatch = inheritanceJson?.savedPatch ?? json.savedPatch

if (isNewGrapher) {
this.newChartId = json.chartId
this.grapher.id = json.chartId
this.savedPatchConfig = json.savedPatch
this.savedPatchConfig = savedPatch
updateInheritance()
} else {
runInAction(() => {
grapher.version += 1
this.logs.unshift(json.newLog)
this.savedPatchConfig = json.savedPatch
this.savedPatchConfig = savedPatch
updateInheritance()
})
}
} else onError?.()
Expand All @@ -178,20 +222,22 @@ export class ChartEditor extends AbstractChartEditor<ChartEditorManager> {
// Need to open intermediary tab before AJAX to avoid popup blockers
const w = window.open("/", "_blank") as Window

const query = new URLSearchParams({
inheritance: this.isInheritanceEnabled ? "1" : "0",
})
const targetUrl = `/api/charts?${query}`

const json = await this.manager.admin.requestJSON(
targetUrl,
"/api/charts",
chartJson,
"POST"
)
if (json.success)

if (json.success) {
if (this.isInheritanceEnabled) {
const url = `/api/charts/${json.chartId}/configInheritance/enable`
await this.manager.admin.requestJSON(url, {}, "PUT")
}

w.location.assign(
this.manager.admin.url(`charts/${json.chartId}/edit`)
)
}
}

publishGrapher(): void {
Expand Down
Loading

0 comments on commit 9c6be0e

Please sign in to comment.