Skip to content

Commit

Permalink
Merge pull request #2933 from owid/gdocs-index-page-enhancements
Browse files Browse the repository at this point in the history
Small gdocs index page enhancements
  • Loading branch information
ikesau authored Dec 8, 2023
2 parents 0f6332a + c5800db commit 87b0098
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
4 changes: 4 additions & 0 deletions adminSiteClient/GdocsAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export const GdocsAdd = ({ onAdd }: { onAdd: (id: string) => void }) => {
placeholder="https://docs.google.com/document/d/****/edit"
pattern={gdocUrlRegex.toString().slice(1, -1)}
/>
<span className="validation-notice">
Invalid URL - it should look like this:{" "}
<pre>https://docs.google.com/document/d/****/edit</pre>
</span>
</div>
</div>
<div className="modal-footer">
Expand Down
1 change: 1 addition & 0 deletions adminSiteClient/GdocsIndexPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export class GdocsIndexPage extends React.Component<GdocsMatchProps> {
)
: undefined,
gdoc.slug,
gdoc.id,
gdoc.content.authors?.join(" "),
gdoc.tags?.map(({ name }) => name).join(" "),
]
Expand Down
16 changes: 16 additions & 0 deletions adminSiteClient/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,22 @@ main:not(.ChartEditorPage):not(.GdocsEditPage) {
margin-bottom: 1rem;
}
}
.validation-notice {
display: none;
}
input:invalid + .validation-notice {
display: block;
margin-top: 4px;
color: red;
padding-left: 5px;
pre {
color: red;
}
}
// don't show the validation notice when the input is blank (because that counts as "invalid")
input:placeholder-shown + .validation-notice {
display: none;
}
}

.GdocsEditPage,
Expand Down
16 changes: 15 additions & 1 deletion adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2494,7 +2494,21 @@ apiRouter.put("/gdocs/:id", async (req, res) => {
if (isEmpty(nextGdocJSON)) {
const newGdoc = new GdocPost(id)
// this will fail if the gdoc already exists, as opposed to a call to newGdoc.save()
await dataSource.getRepository(GdocPost).insert(newGdoc)
try {
await dataSource.getRepository(GdocPost).insert(newGdoc)
} catch (e) {
if (
typeof e === "object" &&
e !== null &&
"code" in e &&
e.code === "ER_DUP_ENTRY"
) {
const preexistingGdoc = dataSource
.getRepository(GdocPost)
.findBy({ id: id })
return preexistingGdoc
}
}

const publishedExplorersBySlug =
await explorerAdminServer.getAllPublishedExplorersBySlugCached()
Expand Down
2 changes: 1 addition & 1 deletion packages/@ourworldindata/utils/src/GdocsConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export const IMAGES_DIRECTORY = "/images/published/"
* https://docs.google.com/spreadsheets/d/abcd1234
*/
export const gdocUrlRegex =
/https:\/\/docs\.google\.com\/document(?:\/u\/\d)?\/d\/([-\w]+)\/?(edit)?#?/
/https:\/\/docs\.google\.com\/document(?:\/u\/\d)?\/d\/([\-\w]+)\/?(edit)?#?/

export const gdocIdRegex = /^[0-9A-Za-z\-_]{44}$/

0 comments on commit 87b0098

Please sign in to comment.