|
| 1 | +import type { ActionFunctionArgs, LoaderFunctionArgs } from 'react-router'; |
| 2 | +import { |
| 3 | + data, |
| 4 | + RouterContextProvider, |
| 5 | + useFetcher, |
| 6 | + useLoaderData, |
| 7 | +} from 'react-router'; |
| 8 | +import { requireAuthCookie } from '@plone/react-router'; |
| 9 | +import { |
| 10 | + ploneClientContext, |
| 11 | + ploneContentContext, |
| 12 | +} from '@plone/aurora/app/middleware.server'; |
| 13 | +import { flattenToAppURL } from '@plone/helpers'; |
| 14 | +import { useTranslation } from 'react-i18next'; |
| 15 | +import type { GetHistoryResponse } from '@plone/types'; |
| 16 | + |
| 17 | +export async function loader({ |
| 18 | + request, |
| 19 | + context, |
| 20 | +}: LoaderFunctionArgs<RouterContextProvider>) { |
| 21 | + await requireAuthCookie(request); |
| 22 | + |
| 23 | + const cli = context.get(ploneClientContext); |
| 24 | + const content = context.get(ploneContentContext); |
| 25 | + const contentPath = content?.['@id'] ?? '/'; |
| 26 | + |
| 27 | + const { data: history } = await cli.getHistory({ path: contentPath }); |
| 28 | + |
| 29 | + return data(flattenToAppURL({ content, history })); |
| 30 | +} |
| 31 | + |
| 32 | +export async function action({ |
| 33 | + request, |
| 34 | + context, |
| 35 | +}: ActionFunctionArgs<RouterContextProvider>) { |
| 36 | + await requireAuthCookie(request); |
| 37 | + |
| 38 | + const cli = context.get(ploneClientContext); |
| 39 | + const content = context.get(ploneContentContext); |
| 40 | + const contentPath = content?.['@id'] ?? '/'; |
| 41 | + |
| 42 | + const formData = await request.formData(); |
| 43 | + const version = Number(formData.get('version')); |
| 44 | + |
| 45 | + await cli.revertHistory({ path: contentPath, data: { version } }); |
| 46 | + |
| 47 | + return data({ ok: true }); |
| 48 | +} |
| 49 | + |
| 50 | +export default function History() { |
| 51 | + const { content, history } = useLoaderData<typeof loader>(); |
| 52 | + const { t } = useTranslation(); |
| 53 | + const fetcher = useFetcher(); |
| 54 | + |
| 55 | + // TODO: replace this list with the quanta Table component and format the |
| 56 | + // timestamp with @internationalized/date. See Volto's History.jsx for the |
| 57 | + // full UX (version comparison/diff, state transitions). |
| 58 | + return ( |
| 59 | + <main> |
| 60 | + <h1>{t('cmsui.history.label')}</h1> |
| 61 | + <p>{content['@id']}</p> |
| 62 | + <ul> |
| 63 | + {(history as GetHistoryResponse).map((entry, index) => ( |
| 64 | + <li key={index}> |
| 65 | + <b>{entry.actor?.fullname}</b> {entry.transition_title} {entry.time} |
| 66 | + {entry.comments ? <em> ({entry.comments})</em> : null} |
| 67 | + {'version' in entry && entry.may_revert ? ( |
| 68 | + <fetcher.Form method="post" style={{ display: 'inline' }}> |
| 69 | + <input type="hidden" name="version" value={entry.version} /> |
| 70 | + <button type="submit">{t('cmsui.history.revert')}</button> |
| 71 | + </fetcher.Form> |
| 72 | + ) : null} |
| 73 | + </li> |
| 74 | + ))} |
| 75 | + </ul> |
| 76 | + </main> |
| 77 | + ); |
| 78 | +} |
0 commit comments