-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for literal arrays to block meta (#2622)
Block meta was missing support for literal arrays, e.g., `string[]`. As a workaround devs would use `string` and join the array elements with a separator (e.g., `,`). This was not ideal. We therefore add support for string, number, boolean, and JSON arrays. Arrays can be defined by setting `array: true` (inspired by MikroORM). An explicit type annotation (e.g., `type: "string"`) is also necessary since the design type only states that it's an array, but no which type (Nest requires this as well).
- Loading branch information
1 parent
e2eb826
commit 9e2b0fa
Showing
17 changed files
with
261 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
"@comet/blocks-api": minor | ||
"@comet/cli": minor | ||
--- | ||
|
||
Add support for literal arrays to block meta | ||
|
||
String, number, boolean, and JSON arrays can be defined by setting `array: true`. | ||
|
||
**Example** | ||
|
||
```ts | ||
class NewsListBlockData { | ||
@BlockField({ type: "string", array: true }) | ||
newsIds: string[]; | ||
} | ||
``` |
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,74 @@ | ||
import { gql, useQuery } from "@apollo/client"; | ||
import { GridColDef, useBufferedRowCount, useDataGridRemote, usePersistentColumnState } from "@comet/admin"; | ||
import { BlockInterface, createBlockSkeleton } from "@comet/blocks-admin"; | ||
import { Box } from "@mui/material"; | ||
import { DataGridPro } from "@mui/x-data-grid-pro"; | ||
import { NewsListBlockData, NewsListBlockInput } from "@src/blocks.generated"; | ||
import { useContentScope } from "@src/common/ContentScopeProvider"; | ||
import { FormattedMessage, useIntl } from "react-intl"; | ||
|
||
import { GQLNewsListBlockNewsFragment, GQLNewsListBlockQuery, GQLNewsListBlockQueryVariables } from "./NewsListBlock.generated"; | ||
|
||
type State = { | ||
ids: string[]; | ||
}; | ||
|
||
export const NewsListBlock: BlockInterface<NewsListBlockData, State, NewsListBlockInput> = { | ||
...createBlockSkeleton(), | ||
name: "NewsList", | ||
displayName: <FormattedMessage id="blocks.newsList.name" defaultMessage="News List" />, | ||
defaultValues: () => ({ ids: [] }), | ||
AdminComponent: ({ state, updateState }) => { | ||
const { scope } = useContentScope(); | ||
const dataGridProps = { ...useDataGridRemote(), ...usePersistentColumnState("NewsListBlock") }; | ||
const intl = useIntl(); | ||
|
||
const columns: GridColDef<GQLNewsListBlockNewsFragment>[] = [ | ||
{ field: "title", headerName: intl.formatMessage({ id: "news.title", defaultMessage: "Title" }), width: 150 }, | ||
]; | ||
|
||
const { data, loading, error } = useQuery<GQLNewsListBlockQuery, GQLNewsListBlockQueryVariables>( | ||
gql` | ||
query NewsListBlock($scope: NewsContentScopeInput!) { | ||
newsList(scope: $scope) { | ||
nodes { | ||
id | ||
...NewsListBlockNews | ||
} | ||
totalCount | ||
} | ||
} | ||
fragment NewsListBlockNews on News { | ||
title | ||
} | ||
`, | ||
{ variables: { scope } }, | ||
); | ||
const rowCount = useBufferedRowCount(data?.newsList.totalCount); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
const rows = data?.newsList.nodes ?? []; | ||
|
||
return ( | ||
<Box sx={{ height: 500 }}> | ||
<DataGridPro | ||
{...dataGridProps} | ||
rows={rows} | ||
rowCount={rowCount} | ||
columns={columns} | ||
loading={loading} | ||
checkboxSelection | ||
disableSelectionOnClick | ||
keepNonExistentRowsSelected | ||
selectionModel={state.ids} | ||
onSelectionModelChange={(newSelection) => { | ||
updateState({ ids: newSelection as string[] }); | ||
}} | ||
/> | ||
</Box> | ||
); | ||
}, | ||
}; |
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,19 @@ | ||
import { BlockData, BlockField, BlockInput, createBlock, inputToData } from "@comet/blocks-api"; | ||
import { IsUUID } from "class-validator"; | ||
|
||
export class NewsListBlockData extends BlockData { | ||
@BlockField({ type: "string", array: true }) | ||
ids: string[]; | ||
} | ||
|
||
class NewsListBlockInput extends BlockInput { | ||
@BlockField({ type: "string", array: true }) | ||
@IsUUID(undefined, { each: true }) | ||
ids: string[]; | ||
|
||
transformToBlockData(): NewsListBlockData { | ||
return inputToData(NewsListBlockData, this); | ||
} | ||
} | ||
|
||
export const NewsListBlock = createBlock(NewsListBlockData, NewsListBlockInput, "NewsList"); |
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,24 @@ | ||
import { AffectedEntity, RequiredPermission } from "@comet/cms-api"; | ||
import { InjectRepository } from "@mikro-orm/nestjs"; | ||
import { EntityRepository } from "@mikro-orm/postgresql"; | ||
import { Args, ID, Query, Resolver } from "@nestjs/graphql"; | ||
|
||
import { News } from "./entities/news.entity"; | ||
|
||
@Resolver(() => News) | ||
@RequiredPermission("news") | ||
export class ExtendedNewsResolver { | ||
constructor(@InjectRepository(News) private readonly repository: EntityRepository<News>) {} | ||
|
||
@Query(() => [News]) | ||
@AffectedEntity(News, { idArg: "ids" }) | ||
async newsListByIds(@Args("ids", { type: () => [ID] }) ids: string[]): Promise<News[]> { | ||
const newsList = await this.repository.find({ id: { $in: ids } }); | ||
|
||
if (newsList.length !== ids.length) { | ||
throw new Error("Failed to load all requested news"); | ||
} | ||
|
||
return newsList.sort((newsA, newsB) => ids.indexOf(newsA.id) - ids.indexOf(newsB.id)); | ||
} | ||
} |
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,35 @@ | ||
import { BlockLoader, gql } from "@comet/cms-site"; | ||
import { NewsListBlockData } from "@src/blocks.generated"; | ||
|
||
import { GQLNewsListBlockNewsFragment, GQLNewsListBlockQuery, GQLNewsListBlockQueryVariables } from "./NewsListBlock.loader.generated"; | ||
|
||
export type LoadedData = GQLNewsListBlockNewsFragment[]; | ||
|
||
export const loader: BlockLoader<NewsListBlockData> = async ({ blockData, graphQLFetch }): Promise<LoadedData> => { | ||
if (blockData.ids.length === 0) { | ||
return []; | ||
} | ||
|
||
const data = await graphQLFetch<GQLNewsListBlockQuery, GQLNewsListBlockQueryVariables>( | ||
gql` | ||
query NewsListBlock($ids: [ID!]!) { | ||
newsListByIds(ids: $ids) { | ||
...NewsListBlockNews | ||
} | ||
} | ||
fragment NewsListBlockNews on News { | ||
id | ||
title | ||
slug | ||
scope { | ||
domain | ||
language | ||
} | ||
} | ||
`, | ||
{ ids: blockData.ids }, | ||
); | ||
|
||
return data.newsListByIds; | ||
}; |
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,24 @@ | ||
import { PropsWithData, withPreview } from "@comet/cms-site"; | ||
import { NewsListBlockData } from "@src/blocks.generated"; | ||
import Link from "next/link"; | ||
|
||
import { LoadedData } from "./NewsListBlock.loader"; | ||
|
||
export const NewsListBlock = withPreview( | ||
({ data: { loaded: newsList } }: PropsWithData<NewsListBlockData & { loaded: LoadedData }>) => { | ||
if (newsList.length === 0) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<ol> | ||
{newsList.map((news) => ( | ||
<li key={news.id}> | ||
<Link href={`/${news.scope.language}/news/${news.slug}`}>{news.title}</Link> | ||
</li> | ||
))} | ||
</ol> | ||
); | ||
}, | ||
{ label: "News List" }, | ||
); |
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
Oops, something went wrong.