-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add admin for multidimensional data pages
- Loading branch information
Showing
16 changed files
with
731 additions
and
291 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,255 @@ | ||
import { | ||
useMutation, | ||
UseMutationResult, | ||
useQuery, | ||
useQueryClient, | ||
} from "@tanstack/react-query" | ||
import { createContext, useContext, useEffect, useMemo, useState } from "react" | ||
import { | ||
Flex, | ||
Input, | ||
Popconfirm, | ||
Switch, | ||
Table, | ||
TableColumnsType, | ||
notification, | ||
} from "antd" | ||
import { Admin } from "./Admin.js" | ||
import { AdminLayout } from "./AdminLayout.js" | ||
import { AdminAppContext } from "./AdminAppContext.js" | ||
import urljoin from "url-join" | ||
import { | ||
BAKED_BASE_URL, | ||
BAKED_GRAPHER_URL, | ||
GRAPHER_DYNAMIC_THUMBNAIL_URL, | ||
} from "../settings/clientSettings.js" | ||
import { dayjs, Json } from "@ourworldindata/utils" | ||
import { Link } from "./Link.js" | ||
|
||
type ApiMultiDim = { | ||
id: number | ||
title: string | ||
slug: string | ||
updatedAt: string | ||
published: boolean | ||
} | ||
|
||
type MultiDim = Omit<ApiMultiDim, "updatedAt"> & { | ||
updatedAt: Date | ||
} | ||
|
||
function createColumns( | ||
publishMutation: UseMutationResult< | ||
unknown, | ||
unknown, | ||
{ id: number; published: boolean }, | ||
unknown | ||
> | ||
): TableColumnsType<MultiDim> { | ||
return [ | ||
{ | ||
title: "Preview", | ||
dataIndex: "thumbnailUrl", | ||
width: 100, | ||
key: "thumbnail", | ||
render: (_, record) => { | ||
return ( | ||
<div style={{ width: 100, height: 100 }}> | ||
<a | ||
href={urljoin( | ||
BAKED_BASE_URL, | ||
`/admin/grapher/${record.slug}` | ||
)} | ||
target="_blank" | ||
rel="noopener" | ||
> | ||
<img | ||
src={urljoin( | ||
GRAPHER_DYNAMIC_THUMBNAIL_URL, | ||
`/${record.slug}.png?imType=square&imSquareSize=100` | ||
)} | ||
style={{ maxWidth: "100%", maxHeight: "100%" }} | ||
alt="Preview" | ||
/> | ||
</a> | ||
</div> | ||
) | ||
}, | ||
}, | ||
{ | ||
title: "Title", | ||
dataIndex: "title", | ||
key: "title", | ||
render: (text, record) => | ||
record.published ? ( | ||
<a | ||
href={urljoin(BAKED_GRAPHER_URL, record.slug)} | ||
target="_blank" | ||
rel="noopener" | ||
> | ||
{text} | ||
</a> | ||
) : ( | ||
text | ||
), | ||
sorter: (a, b) => a.title.localeCompare(b.title), | ||
}, | ||
{ | ||
title: "Slug", | ||
dataIndex: "slug", | ||
key: "slug", | ||
// render: (text, record) => { | ||
// const handleSave = async () => { | ||
// await api.patchMdim(record.id, { slug: value }) | ||
// setIsEditing(false) | ||
// notificationApi.success({ | ||
// message: "Slug updated successfully", | ||
// placement: "bottomRight", | ||
// }) | ||
// } | ||
|
||
// return isEditing ? ( | ||
// <Input | ||
// value={value} | ||
// onChange={(e) => setValue(e.target.value)} | ||
// onPressEnter={handleSave} | ||
// onBlur={handleSave} | ||
// autoFocus | ||
// /> | ||
// ) : ( | ||
// <div | ||
// onClick={() => setIsEditing(true)} | ||
// style={{ cursor: "pointer" }} | ||
// > | ||
// {value} | ||
// <span | ||
// className="ant-typography-secondary" | ||
// style={{ marginLeft: 8 }} | ||
// > | ||
// (click to edit) | ||
// </span> | ||
// </div> | ||
// ) | ||
// }, | ||
}, | ||
{ | ||
title: "Last updated", | ||
dataIndex: "updatedAt", | ||
key: "updatedAt", | ||
defaultSortOrder: "descend", | ||
render: (updatedAt) => { | ||
const date = dayjs(updatedAt) | ||
return <span title={date.format()}>{date.fromNow()}</span> | ||
}, | ||
sorter: (a, b) => a.updatedAt.getTime() - b.updatedAt.getTime(), | ||
}, | ||
{ | ||
title: "Published", | ||
dataIndex: "published", | ||
key: "published", | ||
align: "center", | ||
render: (published, record) => ( | ||
<Popconfirm | ||
title={`Are you sure you want to ${published ? "unpublish" : "publish"} this page?`} | ||
onConfirm={() => | ||
publishMutation.mutate({ | ||
id: record.id, | ||
published: !published, | ||
}) | ||
} | ||
okText="Yes" | ||
cancelText="No" | ||
> | ||
<Switch | ||
checked={published} | ||
disabled={publishMutation.isLoading} | ||
/> | ||
</Popconfirm> | ||
), | ||
}, | ||
] | ||
} | ||
|
||
const NotificationContext = createContext(null) | ||
|
||
async function fetchMultiDims(admin: Admin) { | ||
const result = await admin.getJSON<{ multiDims: MultiDim[] }>( | ||
"/api/multi-dims.json" | ||
) | ||
return result.multiDims.map((mdim) => ({ | ||
...mdim, | ||
updatedAt: new Date(mdim.updatedAt), // TODO: Is the date correct in staging/prod? | ||
})) | ||
} | ||
|
||
async function patchMultiDim(admin: Admin, id: number, data: Json) { | ||
return await admin.requestJSON(`/api/multi-dims/${id}`, data, "PATCH") | ||
} | ||
|
||
export function MultiDimIndexPage() { | ||
const { admin } = useContext(AdminAppContext) | ||
const [notificationApi, notificationContextHolder] = | ||
notification.useNotification() | ||
const [searchValue, setSearchValue] = useState("") | ||
const queryClient = useQueryClient() | ||
|
||
useEffect(() => { | ||
admin.loadingIndicatorSetting = "off" | ||
return () => { | ||
admin.loadingIndicatorSetting = "default" | ||
} | ||
}, [admin]) | ||
|
||
const { data } = useQuery({ | ||
queryKey: ["multiDims"], | ||
queryFn: () => fetchMultiDims(admin), | ||
}) | ||
|
||
const mutation = useMutation({ | ||
mutationFn: ({ id, published }: { id: number; published: boolean }) => | ||
patchMultiDim(admin, id, { published }), | ||
onSuccess: (_, { published }) => { | ||
notificationApi.info({ | ||
message: published ? "Published" : "Unpublished", | ||
description: <Link to="/deploys">Check deploy progress</Link>, | ||
placement: "bottomRight", | ||
}) | ||
return queryClient.invalidateQueries({ queryKey: ["multiDims"] }) | ||
}, | ||
}) | ||
|
||
const filteredMdims = useMemo( | ||
() => | ||
data?.filter((mdim) => | ||
[mdim.title, mdim.slug].some((field) => | ||
field.toLowerCase().includes(searchValue.toLowerCase()) | ||
) | ||
), | ||
[data, searchValue] | ||
) | ||
|
||
const columns = createColumns(mutation) | ||
|
||
return ( | ||
<AdminLayout title="Multidimensional Data Pages"> | ||
<NotificationContext.Provider value={null}> | ||
{notificationContextHolder} | ||
<main className="MultiDimIndexPage"> | ||
<Flex justify="space-between"> | ||
<Input | ||
placeholder="Search by title or slug" | ||
value={searchValue} | ||
onChange={(e) => setSearchValue(e.target.value)} | ||
style={{ width: 500, marginBottom: 20 }} | ||
/> | ||
</Flex> | ||
<Table | ||
columns={columns} | ||
dataSource={filteredMdims} | ||
rowKey={(x) => x.id} | ||
/> | ||
</main> | ||
</NotificationContext.Provider> | ||
</AdminLayout> | ||
) | ||
} |
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
Oops, something went wrong.