Skip to content

Commit

Permalink
feat(admin): enable "Save as narrative view"
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Nov 27, 2024
1 parent 0758f9f commit 9e97c78
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 25 deletions.
37 changes: 37 additions & 0 deletions adminSiteClient/ChartEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getParentVariableIdFromChartConfig,
mergeGrapherConfigs,
isEmpty,
slugify,
} from "@ourworldindata/utils"
import { action, computed, observable, runInAction } from "mobx"
import { BAKED_GRAPHER_URL } from "../settings/clientSettings.js"
Expand Down Expand Up @@ -201,6 +202,42 @@ export class ChartEditor extends AbstractChartEditor<ChartEditorManager> {
)
}

async saveAsNarrativeView(): Promise<void> {
const { patchConfig, grapher } = this

const chartJson = { ...patchConfig }
delete chartJson.id
delete chartJson.isPublished
delete chartJson.slug

const suggestedSlug = grapher.title ? slugify(grapher.title) : undefined

const slug = prompt(
"Please enter a slug for the narrative view. Note that the slug cannot be changed later.",
suggestedSlug
)

// Need to open intermediary tab before AJAX to avoid popup blockers
const w = window.open("/", "_blank") as Window

const body = {
slug,
parentChartId: grapher.id,
config: chartJson,
}

const json = await this.manager.admin.requestJSON(
"/api/chartViews",
body,
"POST"
)

if (json.success)
w.location.assign(
this.manager.admin.url(`chartViews/${json.chartViewId}/edit`)
)
}

publishGrapher(): void {
const url = `${BAKED_GRAPHER_URL}/${this.grapher.displaySlug}`

Expand Down
64 changes: 39 additions & 25 deletions adminSiteClient/SaveButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class SaveButtonsForChart extends React.Component<{
void this.props.editor.saveAsNewGrapher()
}

@action.bound onSaveAsNarrativeView() {
void this.props.editor.saveAsNarrativeView()
}

@action.bound onPublishToggle() {
if (this.props.editor.grapher.isPublished)
this.props.editor.unpublishGrapher()
Expand All @@ -77,31 +81,41 @@ class SaveButtonsForChart extends React.Component<{

return (
<div className="SaveButtons">
<button
className="btn btn-success"
onClick={this.onSaveChart}
disabled={isSavingDisabled}
>
{grapher.isPublished
? "Update chart"
: grapher.id
? "Save draft"
: "Create draft"}
</button>{" "}
<button
className="btn btn-secondary"
onClick={this.onSaveAsNew}
disabled={isSavingDisabled}
>
Save as new
</button>{" "}
<button
className="btn btn-danger"
onClick={this.onPublishToggle}
disabled={isSavingDisabled}
>
{grapher.isPublished ? "Unpublish" : "Publish"}
</button>
<div>
<button
className="btn btn-success"
onClick={this.onSaveChart}
disabled={isSavingDisabled}
>
{grapher.isPublished
? "Update chart"
: grapher.id
? "Save draft"
: "Create draft"}
</button>{" "}
<button
className="btn btn-secondary"
onClick={this.onSaveAsNew}
disabled={isSavingDisabled}
>
Save as new
</button>{" "}
<button
className="btn btn-danger"
onClick={this.onPublishToggle}
disabled={isSavingDisabled}
>
{grapher.isPublished ? "Unpublish" : "Publish"}
</button>
</div>
<div className="mt-2">
<button
className="btn btn-primary"
onClick={this.onSaveAsNarrativeView}
>
Save as narrative view
</button>
</div>
</div>
)
}
Expand Down

0 comments on commit 9e97c78

Please sign in to comment.