Keystone: [RFC] A StateMachine type

Created on 23 Aug 2019  路  6Comments  路  Source: keystonejs/keystone

Request For Comments on a new field type StateMachine:


StateMachine Field Type

The StateMachine field type allows you specify a Finite State Machine to control the possible states and transitions between those states in a declarative syntax, ideal for setting up publishing workflows or user onboarding walkthroughs.

The StateMachine field type is powered by the xstate.js state chart library.

Example

keystone.createList('Post', {
  fields: {
    name: { type: Text },
    status: {
      type: StateMachine,
      defaultValue: 'draft',
      states: {
        draft: {
          on: {
            ready: [
              { target: 'ready', cond: 'isAdmin' },
              { target: 'ready', cond: 'isAuthor' },
            ],
          },
        },
        ready: {
          on: {
            published: [{ target: 'published', cond: 'isAdmin' }],
            rejected: [{ target: 'rejected', cond: 'isAdmin' }],
          },
        },
        rejected: {
          on: {
            draft: [
              { target: 'draft', cond: 'isAdmin' },
              { target: 'draft', cond: 'isAuthor' },
            ],
          },
        },
        published: {
          on: {
            draft: [{ target: 'draft', cond: 'isAdmin' }],
          },
        },
      },
      guards: {
        isAdmin: ({ context }) => context.authedItem && context.authedItem.isAdmin,
        isAuthor: ({ context, existingItem }) => context.authedItem && context.authedItem.id === existingItem.author,
      },
    },
  },
});

The states and guards are passed straight to xstate.

See the equivalent xstate state machine here (try changing the values returned from isAdmin/isAuthor to see the different transitions which are enabled/disabled):

visualisation of Post.status state chart

The AdminUI Field would display as a drop-down of the current state and any possible states which can be reached from there. Something like:

Select Dropdown showing 4 options with 2 disabled, 1 enabled, and 1 selected.


Whatchya think?

medium 3 feature request fields verified

Most helpful comment

Is there any update?

All 6 comments

This will be really useful

It looks like there hasn't been any activity here in over 6 months. Sorry about that! We've flagged this issue for special attention. It wil be manually reviewed by maintainers, not automatically closed. If you have any additional information please leave us a comment. It really helps! Thank you for you contribution. :)

Like it :)

Is there any update?

Hey all, any updates on this? I think it sounds awesome and my project could really use this functionality as a replacement for the dependsOn feature that was in previous versions of Keystone.

Hi, everbody there is state machine library in javascript called xstate it might can be used for this use case

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thekevinbrown picture thekevinbrown  路  31Comments

bothwellw picture bothwellw  路  18Comments

arnaud-zg picture arnaud-zg  路  18Comments

amorote picture amorote  路  21Comments

molomby picture molomby  路  12Comments