Skip to content

Instantly share code, notes, and snippets.

@barbados-clemens
Last active February 9, 2020 00:27
Show Gist options
  • Save barbados-clemens/dbff7ab7affae922b6bc0ca461906a01 to your computer and use it in GitHub Desktop.
Save barbados-clemens/dbff7ab7affae922b6bc0ca461906a01 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// https://xstate.js.org/viz/?gist=dbff7ab7affae922b6bc0ca461906a01
// {
// "type": "USER_ADD",
// "user": {
// "name": "something",
// "isPending": true,
// "drafted": []
// }
// }
const {log} = actions;
const hasUsers = ({users}) => !!users
const hasPendingUsers = ({users}) => !users.filter(u => u.isPending).length > 0;
const addUser = assign({
users: (ctx, event) => {
return [...ctx.users, event.user]
}
});
const fetchMachine = Machine({
id: 'league',
initial: 'created',
context: {
name: '',
users: [
{name: '', isPending: false, drafted: [] }
]
},
states: {
created: {
on: {
'': {
target: 'pendingInvites',
cond: hasUsers
},
}
},
pendingInvites: {
on: {
'': {
target: 'ready',
cond: hasPendingUsers
}
}
},
ready: {
on: {
'START': {
target: 'drafting'
}
}
},
drafting: {
on: {
// should start drafting machine
'DONE': {
target: 'settled'
}
}
},
settled: {
on: {
'FINISHED': {
target: 'closed'
}
}
},
closed: {
},
},
on: {
'USER_ADD': {
target: 'pendingInvites',
actions: addUser
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment