-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Page builder blocks v2 UI list page blocks (#2496)
* feat: add Page Blocks page to Page Builder UI app * fix: fix code style issues * feat: add empty state to Page Blocks list * fix: fix code style comments * fix: change label value * feat: delete/edit Page Block functinality * fix: fix i18n namespace names for Page Block feature pages
- Loading branch information
1 parent
cf75eb3
commit 01aefbf
Showing
11 changed files
with
778 additions
and
7 deletions.
There are no files selected for viewing
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
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
168 changes: 168 additions & 0 deletions
168
packages/app-page-builder/src/admin/views/PageBlocks/BlocksByCategoriesDataList.tsx
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,168 @@ | ||
import React, { useCallback, useMemo, useState } from "react"; | ||
import { i18n } from "@webiny/app/i18n"; | ||
import { useRouter } from "@webiny/react-router"; | ||
import { useQuery } from "@apollo/react-hooks"; | ||
import orderBy from "lodash/orderBy"; | ||
|
||
import { | ||
DataList, | ||
DataListModalOverlay, | ||
DataListModalOverlayAction, | ||
ScrollList, | ||
ListItem, | ||
ListItemText, | ||
ListItemTextSecondary | ||
} from "@webiny/ui/List"; | ||
import { Cell, Grid } from "@webiny/ui/Grid"; | ||
import { Select } from "@webiny/ui/Select"; | ||
import SearchUI from "@webiny/app-admin/components/SearchUI"; | ||
import { ReactComponent as FilterIcon } from "@webiny/app-admin/assets/icons/filter-24px.svg"; | ||
|
||
import { PbBlockCategory, PbPageBlock } from "~/types"; | ||
import { LIST_PAGE_BLOCKS_AND_CATEGORIES } from "./graphql"; | ||
|
||
const t = i18n.ns("app-page-builder/admin/page-blocks/by-categories-data-list"); | ||
|
||
interface Sorter { | ||
label: string; | ||
sort: string; | ||
} | ||
const SORTERS: Sorter[] = [ | ||
{ | ||
label: t`Newest to oldest`, | ||
sort: "createdOn_DESC" | ||
}, | ||
{ | ||
label: t`Oldest to newest`, | ||
sort: "createdOn_ASC" | ||
}, | ||
{ | ||
label: t`Name A-Z`, | ||
sort: "name_ASC" | ||
}, | ||
{ | ||
label: t`Name Z-A`, | ||
sort: "name_DESC" | ||
} | ||
]; | ||
|
||
const BlocksByCategoriesDataList = () => { | ||
const [filter, setFilter] = useState<string>(""); | ||
const [sort, setSort] = useState<string>(SORTERS[0].sort); | ||
const { history } = useRouter(); | ||
const listQuery = useQuery(LIST_PAGE_BLOCKS_AND_CATEGORIES); | ||
|
||
const blockCategoriesData: PbBlockCategory[] = | ||
listQuery?.data?.pageBuilder?.listBlockCategories?.data || []; | ||
const pageBlocksData: PbPageBlock[] = listQuery?.data?.pageBuilder?.listPageBlocks?.data || []; | ||
|
||
const filterData = useCallback( | ||
({ slug, name }) => { | ||
return slug.toLowerCase().includes(filter) || name.toLowerCase().includes(filter); | ||
}, | ||
[filter] | ||
); | ||
|
||
const sortData = useCallback( | ||
categories => { | ||
if (!sort) { | ||
return categories; | ||
} | ||
const [field, order] = sort.split("_"); | ||
return orderBy(categories, field, order.toLowerCase() as "asc" | "desc"); | ||
}, | ||
[sort] | ||
); | ||
|
||
const selectedBlocksCategory = new URLSearchParams(location.search).get("category"); | ||
const loading = [listQuery].find(item => item.loading); | ||
|
||
const blockCategoriesDataListModalOverlay = useMemo( | ||
() => ( | ||
<DataListModalOverlay> | ||
<Grid> | ||
<Cell span={12}> | ||
<Select | ||
value={sort} | ||
onChange={setSort} | ||
label={t`Sort by`} | ||
description={"Sort block categories by"} | ||
> | ||
{SORTERS.map(({ label, sort: value }) => { | ||
return ( | ||
<option key={label} value={value}> | ||
{label} | ||
</option> | ||
); | ||
})} | ||
</Select> | ||
</Cell> | ||
</Grid> | ||
</DataListModalOverlay> | ||
), | ||
[sort] | ||
); | ||
|
||
const filteredBlockCategoriesData: PbBlockCategory[] = | ||
filter === "" ? blockCategoriesData : blockCategoriesData.filter(filterData); | ||
const categoryList: PbBlockCategory[] = sortData(filteredBlockCategoriesData); | ||
|
||
return ( | ||
<DataList | ||
title={t`Blocks`} | ||
loading={Boolean(loading)} | ||
data={categoryList} | ||
search={ | ||
<SearchUI | ||
value={filter} | ||
onChange={setFilter} | ||
inputPlaceholder={t`Search categories`} | ||
/> | ||
} | ||
modalOverlay={blockCategoriesDataListModalOverlay} | ||
modalOverlayAction={ | ||
<DataListModalOverlayAction | ||
icon={<FilterIcon />} | ||
data-testid={"default-data-list.filter"} | ||
/> | ||
} | ||
refresh={() => { | ||
if (!listQuery.refetch) { | ||
return; | ||
} | ||
listQuery.refetch(); | ||
}} | ||
> | ||
{({ data }: { data: PbBlockCategory[] }) => ( | ||
<ScrollList data-testid="default-data-list"> | ||
{data.map(item => { | ||
const numberOfBlocks = pageBlocksData.filter( | ||
pageBlock => pageBlock.blockCategory === item.slug | ||
).length; | ||
return ( | ||
<ListItem | ||
key={item.slug} | ||
selected={item.slug === selectedBlocksCategory} | ||
> | ||
<ListItemText | ||
onClick={() => | ||
history.push( | ||
`/page-builder/page-blocks?category=${item.slug}` | ||
) | ||
} | ||
> | ||
{item.name} | ||
<ListItemTextSecondary>{`${numberOfBlocks} ${ | ||
numberOfBlocks === 1 ? "block" : "blocks" | ||
} in the category`}</ListItemTextSecondary> | ||
</ListItemText> | ||
</ListItem> | ||
); | ||
})} | ||
</ScrollList> | ||
)} | ||
</DataList> | ||
); | ||
}; | ||
|
||
export default BlocksByCategoriesDataList; |
63 changes: 63 additions & 0 deletions
63
packages/app-page-builder/src/admin/views/PageBlocks/PageBlocks.tsx
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,63 @@ | ||
import React, { useMemo, useCallback } from "react"; | ||
import { SplitView, LeftPanel, RightPanel } from "@webiny/app-admin/components/SplitView"; | ||
import { useSecurity } from "@webiny/app-security"; | ||
|
||
import { PageBuilderSecurityPermission } from "~/types"; | ||
import BlocksByCategoriesDataList from "./BlocksByCategoriesDataList"; | ||
import PageBlocksDataList from "./PageBlocksDataList"; | ||
|
||
export interface CreatableItem { | ||
createdBy?: { | ||
id?: string; | ||
}; | ||
} | ||
|
||
const PageBlocks: React.FC = () => { | ||
const { identity, getPermission } = useSecurity(); | ||
const pbPageBlockPermission = useMemo((): PageBuilderSecurityPermission | null => { | ||
return getPermission("pb.block"); | ||
}, [identity]); | ||
|
||
const canEdit = useCallback((item: CreatableItem): boolean => { | ||
if (!pbPageBlockPermission) { | ||
return false; | ||
} | ||
if (pbPageBlockPermission.own) { | ||
const identityId = identity ? identity.id || identity.login : null; | ||
return item.createdBy?.id === identityId; | ||
} | ||
if (typeof pbPageBlockPermission.rwd === "string") { | ||
return pbPageBlockPermission.rwd.includes("w"); | ||
} | ||
|
||
return true; | ||
}, []); | ||
|
||
const canDelete = useCallback((item: CreatableItem): boolean => { | ||
if (!pbPageBlockPermission) { | ||
return false; | ||
} | ||
if (pbPageBlockPermission.own) { | ||
const identityId = identity ? identity.id || identity.login : null; | ||
return item.createdBy?.id === identityId; | ||
} | ||
if (typeof pbPageBlockPermission.rwd === "string") { | ||
return pbPageBlockPermission.rwd.includes("d"); | ||
} | ||
|
||
return true; | ||
}, []); | ||
|
||
return ( | ||
<SplitView> | ||
<LeftPanel> | ||
<BlocksByCategoriesDataList /> | ||
</LeftPanel> | ||
<RightPanel> | ||
<PageBlocksDataList canEdit={canEdit} canDelete={canDelete} /> | ||
</RightPanel> | ||
</SplitView> | ||
); | ||
}; | ||
|
||
export default PageBlocks; |
Oops, something went wrong.