-
Notifications
You must be signed in to change notification settings - Fork 988
feat(admin): bulk publish / set to draft / move to trash in the content list #1524
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@emdash-cms/admin": minor | ||
| --- | ||
|
|
||
| Adds bulk actions to the content list. Select multiple entries with checkboxes (or the header "select all" box) and publish, set to draft, or move them to trash in one step. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -445,6 +445,80 @@ function ContentListPage() { | |
| }, | ||
| }); | ||
|
|
||
| // Bulk actions run the existing per-entry endpoints in parallel. allSettled | ||
| // keeps a single failure from aborting the rest; the count of failures is | ||
| // surfaced via the error toast, and the list is refetched either way. | ||
| const bulkPublishMutation = useMutation({ | ||
| mutationFn: async (ids: string[]) => { | ||
| const results = await Promise.allSettled( | ||
|
Contributor
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. [suggestion] Each bulk mutation fans out one request per selected id with
Collaborator
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. I think the bot is right here: it would be good if it could use a queue |
||
| ids.map((id) => publishContent(collection, id, { locale: activeLocale })), | ||
| ); | ||
| const failed = results.filter((r) => r.status === "rejected").length; | ||
| if (failed > 0) throw new Error(t`${failed} of ${ids.length} could not be published`); | ||
| return ids.length; | ||
| }, | ||
| onSuccess: (count) => { | ||
| toastManager.add({ title: t`Published ${count} items`, type: "success" }); | ||
| }, | ||
| onError: (mutationError) => { | ||
| toastManager.add({ | ||
| title: t`Failed to publish`, | ||
| description: mutationError instanceof Error ? mutationError.message : t`An error occurred`, | ||
| type: "error", | ||
| }); | ||
| }, | ||
| onSettled: () => { | ||
| void queryClient.invalidateQueries({ queryKey: ["content", collection] }); | ||
| }, | ||
| }); | ||
|
|
||
| const bulkUnpublishMutation = useMutation({ | ||
| mutationFn: async (ids: string[]) => { | ||
| const results = await Promise.allSettled( | ||
| ids.map((id) => unpublishContent(collection, id, { locale: activeLocale })), | ||
| ); | ||
| const failed = results.filter((r) => r.status === "rejected").length; | ||
| if (failed > 0) throw new Error(t`${failed} of ${ids.length} could not be updated`); | ||
| return ids.length; | ||
| }, | ||
| onSuccess: (count) => { | ||
| toastManager.add({ title: t`Moved ${count} items to draft`, type: "success" }); | ||
| }, | ||
| onError: (mutationError) => { | ||
| toastManager.add({ | ||
| title: t`Failed to update`, | ||
| description: mutationError instanceof Error ? mutationError.message : t`An error occurred`, | ||
| type: "error", | ||
| }); | ||
| }, | ||
| onSettled: () => { | ||
| void queryClient.invalidateQueries({ queryKey: ["content", collection] }); | ||
| }, | ||
| }); | ||
|
|
||
| const bulkDeleteMutation = useMutation({ | ||
| mutationFn: async (ids: string[]) => { | ||
| const results = await Promise.allSettled(ids.map((id) => deleteContent(collection, id))); | ||
| const failed = results.filter((r) => r.status === "rejected").length; | ||
| if (failed > 0) throw new Error(t`${failed} of ${ids.length} could not be deleted`); | ||
| return ids.length; | ||
| }, | ||
| onSuccess: (count) => { | ||
| toastManager.add({ title: t`Moved ${count} items to trash`, type: "success" }); | ||
| }, | ||
| onError: (mutationError) => { | ||
| toastManager.add({ | ||
| title: t`Failed to delete`, | ||
| description: mutationError instanceof Error ? mutationError.message : t`An error occurred`, | ||
| type: "error", | ||
| }); | ||
| }, | ||
| onSettled: () => { | ||
| void queryClient.invalidateQueries({ queryKey: ["content", collection] }); | ||
| void queryClient.invalidateQueries({ queryKey: ["content", collection, "trash"] }); | ||
| }, | ||
| }); | ||
|
|
||
| const items = React.useMemo(() => { | ||
| return data?.pages.flatMap((page) => page.items) || []; | ||
| }, [data]); | ||
|
|
@@ -507,6 +581,9 @@ function ContentListPage() { | |
| onAuthorFilterChange={setAuthorFilter} | ||
| dateFilter={dateFilter} | ||
| onDateFilterChange={setDateFilter} | ||
| onBulkPublish={(ids) => bulkPublishMutation.mutate(ids)} | ||
| onBulkUnpublish={(ids) => bulkUnpublishMutation.mutate(ids)} | ||
| onBulkDelete={(ids) => bulkDeleteMutation.mutate(ids)} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
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.
[suggestion]
runBulkcallsclearSelection()synchronously, before the fanned-out mutations resolve (fn([...selectedIds])is fire-and-forget —mutateisn't awaited). On partial failure the user sees"3 of 10 could not be published", but the selection is already wiped, so they can't retry just the failed items — and the toast reports only the count, not which ids failed. Consider clearing only after the batch settles (or reporting the failed ids) so a partial failure is recoverable.