-
-
Notifications
You must be signed in to change notification settings - Fork 229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🎉 UI updates for standardized explorer names, explore tagging, and an explorer-tiles
component
#3158
Merged
Merged
🎉 UI updates for standardized explorer names, explore tagging, and an explorer-tiles
component
#3158
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
95627d8
🎉 update explorer title and subtitle display
ikesau adba983
🎉 tags for explorers
ikesau 727653b
🎉 explorer tiles component
ikesau a588142
✨ drop FK constraint for explorer tags, remove "x" from table name
ikesau 34a8d2e
✨ improve ExplorerTagsPage
ikesau cc618dc
🎉 use explorers from the DB to power gdocs
ikesau 22c4d14
🎉 explorer tile text wrap
ikesau 37e7c98
✅ fix lint
ikesau 7cd638b
✨ explorer code review improvements
ikesau 0a4f98d
✨ use explorer subtitles in prominent link
ikesau 7ad8265
✨ use better key for ExplorerTile
ikesau 4b54376
✅ fix explorer-tiles roundtrip test
ikesau f048003
Merge remote-tracking branch 'origin/master' into explorer-name-stand…
ikesau 38875ca
✨ enhance explorer tiles CSS, add tag icon support
ikesau 08806e7
✨ explorer-tiles code review fixes
ikesau 35d4d7e
Merge remote-tracking branch 'origin/master' into explorer-name-stand…
ikesau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,238 @@ | ||
import React from "react" | ||
import { observer } from "mobx-react" | ||
import { action, computed, observable, runInAction } from "mobx" | ||
|
||
import { AdminLayout } from "./AdminLayout.js" | ||
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js" | ||
import { DbChartTagJoin } from "@ourworldindata/utils" | ||
import { | ||
GetAllExplorersRoute, | ||
GetAllExplorersTagsRoute, | ||
} from "../explorer/ExplorerConstants.js" | ||
import { EditableTags } from "./EditableTags.js" | ||
import { ExplorerProgram } from "../explorer/ExplorerProgram.js" | ||
import cx from "classnames" | ||
|
||
type ExplorerWithTags = { | ||
slug: string | ||
tags: DbChartTagJoin[] | ||
} | ||
|
||
@observer | ||
export class ExplorerTagsPage extends React.Component { | ||
static contextType = AdminAppContext | ||
context!: AdminAppContextType | ||
@observable explorersWithTags: ExplorerWithTags[] = [] | ||
@observable explorers: ExplorerProgram[] = [] | ||
@observable tags: DbChartTagJoin[] = [] | ||
@observable newExplorerSlug = "" | ||
@observable newExplorerTags: DbChartTagJoin[] = [] | ||
|
||
componentDidMount() { | ||
this.getData() | ||
} | ||
ikesau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Don't show explorers that already have tags | ||
@computed get filteredExplorers() { | ||
return this.explorers.filter((explorer) => { | ||
return !this.explorersWithTags.find((e) => e.slug === explorer.slug) | ||
}) | ||
} | ||
|
||
render() { | ||
return ( | ||
<AdminLayout title="Explorer Tags"> | ||
<main className="ExplorerTags"> | ||
<h3>Explorer tags</h3> | ||
<p> | ||
This page is for managing the tags for each explorer. | ||
Explorer data is currently stored in{" "} | ||
<a href="https://github.com/owid/owid-content">git</a>, | ||
but tags are stored in the database, which means it's | ||
hard to strictly enforce{" "} | ||
<a href="https://en.wikipedia.org/wiki/Referential_integrity"> | ||
referential integrity. | ||
</a> | ||
</p> | ||
<p> | ||
If you update an explorer's slug, you'll need to delete | ||
the old row in this table and create a new one for the | ||
new slug. | ||
</p> | ||
|
||
<table className="table table-bordered"> | ||
<thead> | ||
<tr> | ||
<th style={{ width: "45%" }}>Explorer</th> | ||
<th style={{ width: "45%" }}>Tags</th> | ||
<th style={{ width: "10%" }}>Controls</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{/* Existing Explorers Rows */} | ||
{this.explorersWithTags.map((explorer) => { | ||
const isSlugValid = this.explorers.find( | ||
(e) => e.slug === explorer.slug | ||
) | ||
|
||
return ( | ||
<tr | ||
key={explorer.slug} | ||
className={cx({ | ||
"table-danger": !isSlugValid, | ||
})} | ||
> | ||
<td> | ||
{explorer.slug} | ||
{isSlugValid ? null : ( | ||
<p> | ||
<strong> | ||
❗️ this slug doesn't | ||
match any explorer in | ||
the database - is it for | ||
an explorer that has | ||
been deleted or renamed? | ||
</strong> | ||
</p> | ||
)} | ||
</td> | ||
<td> | ||
<EditableTags | ||
tags={explorer.tags} | ||
suggestions={this.tags} | ||
onSave={(tags) => { | ||
this.saveTags( | ||
explorer.slug, | ||
tags | ||
) | ||
}} | ||
/> | ||
</td> | ||
<td> | ||
<button | ||
className="btn btn-danger" | ||
onClick={() => | ||
this.deleteExplorerTags( | ||
explorer.slug | ||
) | ||
} | ||
> | ||
Delete | ||
</button> | ||
</td> | ||
</tr> | ||
) | ||
})} | ||
{/* New Explorer Row */} | ||
<tr> | ||
<td> | ||
<select | ||
value={this.newExplorerSlug} | ||
onChange={(e) => { | ||
this.newExplorerSlug = | ||
e.target.value | ||
}} | ||
> | ||
<option value=""> | ||
Select an explorer | ||
</option> | ||
{this.filteredExplorers.map( | ||
(explorer) => ( | ||
<option | ||
key={explorer.slug} | ||
value={explorer.slug} | ||
> | ||
{explorer.slug} | ||
</option> | ||
) | ||
)} | ||
</select> | ||
</td> | ||
<td> | ||
<EditableTags | ||
// This component clones the `tags` prop and doesn't rerender when that prop changes, | ||
// meaning the tags don't clear when you save a new explorer. | ||
// So we force a rerender by setting a key which updates when an explorer is created | ||
// Unfortunately it also resets if if you delete a row before saving the new explorer | ||
// But that seems like a rare edge case, and I don't want to rewrite the EditableTags component | ||
key={this.explorersWithTags.length} | ||
tags={[]} | ||
suggestions={this.tags} | ||
onSave={(tags) => { | ||
this.newExplorerTags = tags | ||
}} | ||
/> | ||
</td> | ||
<td> | ||
<button | ||
className="btn btn-primary" | ||
disabled={ | ||
!this.newExplorerSlug || | ||
!this.newExplorerTags.length | ||
} | ||
title="Enter a slug and at least one tag to save" | ||
onClick={() => this.saveNewExplorer()} | ||
> | ||
Save | ||
</button> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</main> | ||
</AdminLayout> | ||
) | ||
} | ||
|
||
async getData() { | ||
const [{ tags }, explorersWithTags, explorers] = await Promise.all([ | ||
this.context.admin.getJSON("/api/tags.json") as Promise<{ | ||
tags: DbChartTagJoin[] | ||
}>, | ||
this.context.admin.getJSON(GetAllExplorersTagsRoute) as Promise<{ | ||
explorers: ExplorerWithTags[] | ||
}>, | ||
this.context.admin.getJSON(GetAllExplorersRoute) as Promise<{ | ||
explorers: ExplorerProgram[] | ||
}>, | ||
]) | ||
runInAction(() => { | ||
this.tags = tags | ||
this.explorersWithTags = explorersWithTags.explorers | ||
this.explorers = explorers.explorers | ||
}) | ||
} | ||
|
||
async saveTags(slug: string, tags: DbChartTagJoin[]) { | ||
const tagIds = tags.map((tag) => tag.id) | ||
await this.context.admin.requestJSON( | ||
`/api/explorer/${slug}/tags`, | ||
{ | ||
tagIds, | ||
}, | ||
"POST" | ||
) | ||
} | ||
|
||
async deleteExplorerTags(slug: string) { | ||
if ( | ||
window.confirm( | ||
`Are you sure you want to delete the tags for "${slug}"?` | ||
) | ||
) { | ||
await this.context.admin.requestJSON( | ||
`/api/explorer/${slug}/tags`, | ||
{}, | ||
"DELETE" | ||
) | ||
await this.getData() | ||
} | ||
} | ||
|
||
@action.bound async saveNewExplorer() { | ||
await this.saveTags(this.newExplorerSlug, this.newExplorerTags) | ||
await this.getData() | ||
this.newExplorerTags = [] | ||
this.newExplorerSlug = "" | ||
} | ||
ikesau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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 |
---|---|---|
|
@@ -30,6 +30,7 @@ import { | |
DefaultNewExplorerSlug, | ||
EXPLORERS_PREVIEW_ROUTE, | ||
GetAllExplorersRoute, | ||
GetAllExplorersTagsRoute, | ||
} from "../explorer/ExplorerConstants.js" | ||
import { | ||
ExplorerProgram, | ||
|
@@ -253,6 +254,12 @@ adminRouter.get(`/${GetAllExplorersRoute}`, async (req, res) => { | |
res.send(await explorerAdminServer.getAllExplorersCommand()) | ||
}) | ||
|
||
adminRouter.get(`/${GetAllExplorersTagsRoute}`, async (_, res) => { | ||
return res.send({ | ||
explorers: await db.getExplorerTags(db.knexInstance()), | ||
}) | ||
Comment on lines
+258
to
+260
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not absolutely necessary now, but maybe wrap in read-only transaction? |
||
}) | ||
|
||
adminRouter.get(`/${EXPLORERS_PREVIEW_ROUTE}/:slug`, async (req, res) => { | ||
const slug = slugify(req.params.slug) | ||
const filename = slug + EXPLORER_FILE_SUFFIX | ||
|
@@ -275,16 +282,13 @@ adminRouter.get("/datapage-preview/:id", async (req, res) => { | |
const variableId = expectInt(req.params.id) | ||
const variableMetadata = await getVariableMetadata(variableId) | ||
if (!variableMetadata) throw new JsonError("No such variable", 404) | ||
const publishedExplorersBySlug = | ||
await explorerAdminServer.getAllPublishedExplorersBySlugCached() | ||
|
||
res.send( | ||
await renderDataPageV2({ | ||
variableId, | ||
variableMetadata, | ||
isPreviewing: true, | ||
useIndicatorGrapherConfigs: true, | ||
publishedExplorersBySlug, | ||
}) | ||
) | ||
}) | ||
|
@@ -293,16 +297,7 @@ adminRouter.get("/grapher/:slug", async (req, res) => { | |
const entity = await Chart.getBySlug(req.params.slug) | ||
if (!entity) throw new JsonError("No such chart", 404) | ||
|
||
const explorerAdminServer = new ExplorerAdminServer(GIT_CMS_DIR) | ||
const publishedExplorersBySlug = | ||
await explorerAdminServer.getAllPublishedExplorersBySlug() | ||
|
||
res.send( | ||
await renderPreviewDataPageOrGrapherPage( | ||
entity.config, | ||
publishedExplorersBySlug | ||
) | ||
) | ||
res.send(await renderPreviewDataPageOrGrapherPage(entity.config)) | ||
}) | ||
|
||
const gitCmsServer = new GitCmsServer({ | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably nothing to change now
this is a bit unexpected - but maybe unavoidable given the git/db limbo. When first encountering this, it gave me the impression that "explorer tags" were different from the regular ones.
Also given how seldom this is going to be used beyond the first tagging effort, it seems like there is a mismatch with the prime real estate this menu item will be permanently using.
Maybe worth considering complementing this with a more contextual link to /explorer-tags on every explorer row?