-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Singleton List refactor #3013
Merged
MadeByMike
merged 11 commits into
keystonejs:master
from
gautamsi:singleton-list-refactor
May 26, 2020
Merged
Singleton List refactor #3013
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c3ac2f8
create singleton list plugin
gautamsi 660866c
prevent delete by setting access control.
gautamsi e9c0dc0
update ui fr singleton list
gautamsi c5dafe2
add readme and changeset
gautamsi 097814a
review comment - use fullpath on list, move it to useEffect
gautamsi 85448f0
update readme
gautamsi 643b02a
lint error
gautamsi b347383
revert UI changes to make it as part of different PR.
gautamsi dbc1d2c
Merge branch 'master' into singleton-list-refactor
gautamsi 7adac73
Merge branch 'master' into singleton-list-refactor
gautamsi 9d688e2
Merge branch 'master' into singleton-list-refactor
MadeByMike File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,5 @@ | ||
--- | ||
'@keystonejs/list-plugins': major | ||
--- | ||
|
||
* Added `singleton` list plugin which prevents creating more items for a list or delete the only item in the 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const { composeHook, composeAccess } = require('../utils'); | ||
|
||
exports.singleton = () => ( | ||
{ hooks = {}, access = {}, adminConfig = {}, ...rest }, | ||
{ listKey, keystone } | ||
) => { | ||
const newResolveInput = async ({ resolvedData, operation }) => { | ||
if (operation === 'create') { | ||
const list = keystone.getListByKey(listKey); | ||
const query = `{${list.gqlNames.listQueryMetaName} { count }}`; | ||
const { | ||
data: { [list.gqlNames.listQueryMetaName]: listQuery } = {}, | ||
errors, | ||
} = await keystone.executeQuery(query); | ||
if (errors) { | ||
throw errors; | ||
} | ||
if (listQuery && listQuery.count && listQuery.count > 0) { | ||
throw new Error(`ItemLimit reached, This Singleton list can not add more item`); | ||
} | ||
} | ||
return resolvedData; | ||
}; | ||
|
||
const listAccess = composeAccess(access, { delete: false }, keystone.defaultAccess.list); | ||
const originalResolveInput = hooks.resolveInput; | ||
hooks.resolveInput = composeHook(originalResolveInput, newResolveInput); | ||
return { | ||
access: listAccess, | ||
hooks, | ||
adminConfig: { ...adminConfig, singleton: true }, | ||
...rest, | ||
}; | ||
}; |
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,21 @@ | ||
<!--[meta] | ||
section: list-plugins | ||
title: singletoon | ||
[meta]--> | ||
|
||
# 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(), | ||
], | ||
}); | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This plugin does not need argument. We have this pattern to call the plugin when adding to list, if there is no argument this nesting of function call can be avoided. should this be a pattern or dependent on plugin whether to add as
[plugin]
or as[plugin()]
.