diff --git a/.changeset/wise-icons-shake.md b/.changeset/wise-icons-shake.md new file mode 100644 index 00000000000..0b2afa55b6d --- /dev/null +++ b/.changeset/wise-icons-shake.md @@ -0,0 +1,10 @@ +--- +'@keystonejs/app-admin-ui': minor +'@keystonejs/list-plugins': major +--- + +* Added `singleton` list plugin which prevents creating more items for a list or delete the only item in the list. + +* Updated `admin-ui` to + - Redirect to the only item if there is an item in singleton list + - Hide `Search` field, `Back` and `AddNew` buttons on item details page diff --git a/packages/list-plugins/README.md b/packages/list-plugins/README.md index 99d9a447319..7786a5fd34e 100644 --- a/packages/list-plugins/README.md +++ b/packages/list-plugins/README.md @@ -52,7 +52,7 @@ const { createdAt, updatedAt } = require('@keystonejs/list-plugins'); _Note_: The API is the same. -## byTracking +# byTracking Adds `createdBy` and `updatedBy` fields to a list. These fields are read-only by will be updated automatically when items are created or updated. @@ -103,3 +103,39 @@ const { createdBy, updatedBy } = require('@keystonejs/list-plugins'); ``` _Note_: The API is the same. + +# singleton + +This plugin makes a list singleton by allowing only one item in the list. Useful for list which must contain only one items. + +## Usage + +```js +const { singleton } = require('@keystonejs/list-plugins'); + +keystone.createList('ListWithPlugin', { + fields: {...}, + plugins: [ + singleton({...}), + ], +}); +``` + +## Config + +| Option | Type | Default | Description | +| --------------- | --------- | ------- | ------------------------------------------------- | +| `preventDelete` | `Boolean` | `true` | Prevents deletion of the (only) item in the list. | + +### `preventDelete` + +By default the plugin will prevent deletion of the only item in the list by setting `delete` access to false. If you want to control the access yourself, set this value to false. + +```javascript allowCopy=false showLanguage=false +keystone.createList('ListWithPlugin', { + fields: {...}, + plugins: [ + singleton({ preventDelete: false }), + ], +}); +``` diff --git a/packages/list-plugins/singleton.md b/packages/list-plugins/singleton.md new file mode 100644 index 00000000000..bec91b57ee0 --- /dev/null +++ b/packages/list-plugins/singleton.md @@ -0,0 +1,40 @@ + + +# singleton Plugin + +This plugin makes a list singleton by allowing only one item in the list. Useful for list which must contain only one items. + +## Usage + +```js +const { singleton } = require('@keystonejs/list-plugins'); + +keystone.createList('ListWithPlugin', { + fields: {...}, + plugins: [ + singleton({...}), + ], +}); +``` + +## Config + +| Option | Type | Default | Description | +| --------------- | --------- | ------- | ------------------------------------------------- | +| `preventDelete` | `Boolean` | `true` | Prevents deletion of the (only) item in the list. | + +### `preventDelete` + +By default the plugin will prevent deletion of the only item in the list by setting `delete` access to false. If you want to control the access yourself, set this value to false. + +```javascript allowCopy=false showLanguage=false +keystone.createList('ListWithPlugin', { + fields: {...}, + plugins: [ + singleton({ preventDelete: false }), + ], +}); +```