From 4df948590090e4dc87ba7b7ffcc0728cdf98c61f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ra=C4=8D=C3=A1k?= Date: Fri, 21 Jun 2024 17:35:34 +0200 Subject: [PATCH] Add time to publishedAt date picker (#3729) --- adminSiteClient/GdocsDateline.tsx | 4 ++- adminSiteClient/GdocsPreviewPage.tsx | 38 +++++++++++++++++++++++----- baker/GrapherBaker.tsx | 4 +-- db/db.ts | 12 ++++----- db/model/Gdoc/GdocFactory.ts | 7 ++--- settings/clientSettings.ts | 2 ++ 6 files changed, 48 insertions(+), 19 deletions(-) diff --git a/adminSiteClient/GdocsDateline.tsx b/adminSiteClient/GdocsDateline.tsx index 947bb75cd93..fcd61eebe45 100644 --- a/adminSiteClient/GdocsDateline.tsx +++ b/adminSiteClient/GdocsDateline.tsx @@ -2,6 +2,7 @@ import { Col, DatePicker } from "antd" import { Dayjs } from "dayjs" import { dayjs, OwidGdocErrorMessage, OwidGdoc } from "@ourworldindata/utils" import React from "react" +import { PUBLISHED_AT_FORMAT } from "../settings/clientSettings" import { getPropertyMostCriticalError } from "./gdocsValidation.js" import { GdocsErrorHelp } from "./GdocsErrorHelp.js" @@ -31,9 +32,10 @@ export const GdocsPublishedAt = ({ { const { id } = match.params @@ -229,11 +238,26 @@ export const GdocsPreviewPage = ({ match, history }: GdocsMatchProps) => { {currentGdoc.published && ( <> [ - - View live - + {currentGdoc.publishedAt && + currentGdoc.publishedAt <= new Date() ? ( + <> + + View live + + + + + + ) : ( + `Scheduled for ${dayjs(currentGdoc.publishedAt).format(PUBLISHED_AT_FORMAT)}` + )} ] )} diff --git a/baker/GrapherBaker.tsx b/baker/GrapherBaker.tsx index 41cb8716e13..dd8708e8a52 100644 --- a/baker/GrapherBaker.tsx +++ b/baker/GrapherBaker.tsx @@ -60,7 +60,7 @@ import { getSlugForTopicTag, getTagToSlugMap } from "./GrapherBakingUtils.js" import { knexRaw } from "../db/db.js" import { getRelatedChartsForVariable } from "../db/model/Chart.js" import pMap from "p-map" -import { getGdocBaseObjectBySlug } from "../db/model/Gdoc/GdocFactory.js" +import { getPublishedGdocBaseObjectBySlug } from "../db/model/Gdoc/GdocFactory.js" const renderDatapageIfApplicable = async ( grapher: GrapherInterface, @@ -247,7 +247,7 @@ export async function renderDataPageV2( } let gdoc: OwidGdocBaseInterface | undefined = undefined if (slug) { - gdoc = await getGdocBaseObjectBySlug(knex, slug, true) + gdoc = await getPublishedGdocBaseObjectBySlug(knex, slug, true) } if (gdoc) { const citation = getShortPageCitation( diff --git a/db/db.ts b/db/db.ts index 2a20fd40fa3..0d5a620e2b6 100644 --- a/db/db.ts +++ b/db/db.ts @@ -256,7 +256,7 @@ export const getPublishedDataInsights = ( FROM posts_gdocs WHERE type = '${OwidGdocType.DataInsight}' AND published = TRUE - AND publishedAt < NOW() + AND publishedAt <= NOW() ORDER BY publishedAt DESC LIMIT ?`, [limit] @@ -279,7 +279,7 @@ export const getPublishedDataInsightCount = ( FROM posts_gdocs WHERE type = '${OwidGdocType.DataInsight}' AND published = TRUE - AND publishedAt < NOW()` + AND publishedAt <= NOW()` ).then((res) => res?.count ?? 0) } @@ -416,7 +416,7 @@ export const getPublishedGdocPostsWithTags = async ( FROM posts_gdocs g LEFT JOIN posts_gdocs_x_tags gxt ON - g.id = gxt.gdocId + g.id = gxt.gdocId LEFT JOIN tags t ON gxt.tagId = t.id WHERE @@ -595,9 +595,9 @@ export function getMinimalTagsWithIsTopic( return knexRaw( knex, `-- sql - SELECT t.id, - t.name, - t.slug, + SELECT t.id, + t.name, + t.slug, t.slug IS NOT NULL AND MAX(IF(pg.type IN (:types), TRUE, FALSE)) AS isTopic FROM tags t LEFT JOIN posts_gdocs_x_tags gt ON t.id = gt.tagId diff --git a/db/model/Gdoc/GdocFactory.ts b/db/model/Gdoc/GdocFactory.ts index 34e93715670..4e843027c57 100644 --- a/db/model/Gdoc/GdocFactory.ts +++ b/db/model/Gdoc/GdocFactory.ts @@ -226,7 +226,7 @@ export async function getAllMinimalGdocBaseObjects( }) } -export async function getGdocBaseObjectBySlug( +export async function getPublishedGdocBaseObjectBySlug( knex: KnexReadonlyTransaction, slug: string, fetchLinkedTags: boolean @@ -237,7 +237,8 @@ export async function getGdocBaseObjectBySlug( SELECT * FROM posts_gdocs WHERE slug = ? - AND published = 1`, + AND published = 1 + AND publishedAt <= NOW()`, [slug] ) if (!row) return undefined @@ -266,7 +267,7 @@ export async function getAndLoadGdocBySlug( knex: KnexReadWriteTransaction, slug: string ): Promise { - const base = await getGdocBaseObjectBySlug(knex, slug, true) + const base = await getPublishedGdocBaseObjectBySlug(knex, slug, true) if (!base) { throw new Error( `No published Google Doc with slug "${slug}" found in the database` diff --git a/settings/clientSettings.ts b/settings/clientSettings.ts index cdf3fc59f21..7e54564fdc2 100644 --- a/settings/clientSettings.ts +++ b/settings/clientSettings.ts @@ -88,3 +88,5 @@ export const ETL_API_URL: string = export const GDOCS_DETAILS_ON_DEMAND_ID: string = process.env.GDOCS_DETAILS_ON_DEMAND_ID ?? "" + +export const PUBLISHED_AT_FORMAT = "ddd, MMM D, YYYY HH:mm"