From 08e1467bb14be30bbc93d3ae404f3b7c22d84b98 Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Sat, 14 Nov 2020 22:38:39 +0100 Subject: [PATCH] Bulk deletion of pages --- src/helpers.ts | 5 +++-- src/management/api/page.ts | 7 +++++-- src/management/page/list.tsx | 28 ++++++++++++++++++++++------ translations/management.pl.yaml | 2 ++ 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index a37de8c..1d66767 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -2,6 +2,7 @@ export type Nullable = { [P in keyof T]: T[P] | null } export type Subset = { [K in keyof T]?: Subset } export type Dictionary = { [key: string]: T }; +export type OneOrMany = T | T[]; export type Index = string | symbol | number; @@ -27,7 +28,7 @@ export function throttle(decorated: (...args: TArgs) => voi } } -export function encapsulate(value: T|T[]): T[] { +export function encapsulate(value: OneOrMany): T[] { if (value instanceof Array) { return value; } @@ -35,7 +36,7 @@ export function encapsulate(value: T|T[]): T[] { return [ value ]; } -export function one(value: T|T[]): T { +export function one(value: OneOrMany): T { if (value instanceof Array) { return value[0]; } diff --git a/src/management/api/page.ts b/src/management/api/page.ts index c615845..aa0ce1e 100644 --- a/src/management/api/page.ts +++ b/src/management/api/page.ts @@ -3,6 +3,7 @@ import pageDtoTransformer, { PageDTO } from "@/api/dto/page"; import { axios } from "@/api"; import { STATIC_PAGE_ENDPOINT } from "@/api/page"; import { prepare } from "@/routing"; +import { encapsulate, OneOrMany } from "@/helpers"; const STATIC_PAGE_INDEX_ENDPOINT = "/staticPage"; @@ -13,8 +14,10 @@ export async function all(): Promise { return response.data.map(dto => pageDtoTransformer.transform(dto)); } -export async function remove(page: Pick): Promise { - await axios.delete(prepare(STATIC_PAGE_ENDPOINT, { slug: page.slug })); +export async function remove(page: OneOrMany>): Promise { + const pages = encapsulate(page); + + await Promise.all(pages.map(page => axios.delete(prepare(STATIC_PAGE_ENDPOINT, { slug: page.slug })))); } export async function save(page: Page): Promise { diff --git a/src/management/page/list.tsx b/src/management/page/list.tsx index 4eaf524..2a00a83 100644 --- a/src/management/page/list.tsx +++ b/src/management/page/list.tsx @@ -9,7 +9,7 @@ import { Async } from "@/components/async"; import MaterialTable, { Action, Column } from "material-table"; import { default as StaticPage } from "@/data/page"; import { Delete, FileFind, Pencil, Refresh } from "mdi-material-ui"; -import { encapsulate, one } from "@/helpers"; +import { encapsulate, one, OneOrMany } from "@/helpers"; import { Actions } from "@/components"; import { useSpacing } from "@/styles"; import { useHistory } from "react-router-dom"; @@ -21,6 +21,7 @@ import { Confirm } from "@/components/confirm"; export const StaticPageManagement = () => { const { t } = useTranslation("management"); const [ result, setPagesPromise ] = useAsyncState(); + const [ selected, setSelected ] = useState([]); const spacing = useSpacing(2); const updatePageList = () => { @@ -67,20 +68,28 @@ export const StaticPageManagement = () => { } - const DeleteStaticPageAction = ({ page }: { page: StaticPage }) => { + const DeleteStaticPageAction = ({ page, children }: { page: OneOrMany, children?: (action: any) => React.ReactNode }) => { const handlePageDeletion = async () => { await api.page.remove(page); updatePageList(); } const confirmation = <> - - Czy na pewno chcesz usunąć stronę { page.title.pl }? - + { !Array.isArray(page) + ? + Czy na pewno chcesz usunąć stronę { page.title.pl }? + + : <> + { t("page.confirm.bulk-delete") } +
    + { page.map(page =>
  • { page.title.pl }
  • ) } +
+ + } ; return - { action => } + { action => children ? children(action) : } ; } @@ -143,12 +152,19 @@ export const StaticPageManagement = () => { + { selected.length > 0 && + + { action => } + + } { pages => { t("page.index.title") } { result.isLoading && } } columns={ columns } data={ pages } detailPanel={ page => } + onSelectionChange={ pages => setSelected(pages) } + options={{ selection: true }} /> } diff --git a/translations/management.pl.yaml b/translations/management.pl.yaml index 1b9e4ac..1ebd647 100644 --- a/translations/management.pl.yaml +++ b/translations/management.pl.yaml @@ -31,3 +31,5 @@ page: title: Utwórz stronę statyczną edit: title: Zmień stronę statyczną + confirm: + bulk-delete: Czy na pewno chcesz usunąć wszystkie wybrane strony?