Skip to content

Instantly share code, notes, and snippets.

@renerbaffa
Last active April 29, 2020 08:32
Show Gist options
  • Save renerbaffa/7320b693054e11f23158e63ab7c14505 to your computer and use it in GitHub Desktop.
Save renerbaffa/7320b693054e11f23158e63ab7c14505 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// 1
function fetchUsers() {}
const fetchMachine = Machine({
id: 'multi-admin',
initial: 'fetch-users',
context: {
allUsers: [],
selected: [],
},
states: {
'fetch-users' : {
invoke: {
src: () => fetchUsers,
onError: 'fetch-error',
onDone: 'idle',
}
},
'fetch-error': {
on: {
RETRY: 'fetch-users'
}
},
idle: {
id: 'select-machine',
initial: 'selecting',
context: {
selected: [],
},
states: {
selecting: {
on: {
CHANGE_FILTER: {
actions: [
assign({
search: (_, event) => event.value,
}),
],
},
SELECT: {
actions: [
assign({
selected:
(context, event) => {
return [...context.selected, event.value]
},
}),
],
},
UNSELECT: {
actions: [],
},
SAVE: {
target: 'saving',
// cond: context => context.selected.length > 0,
},
},
},
saving: {
invoke: {
src: () => saveChanges,
onError: 'error',
onDone: 'finished',
},
},
error: {
on: {
RETRY: 'saving',
}
},
finished: {
type: 'final',
data: context => context.selected
},
},
onDone: {
target: 'finished'
},
},
finished: {
type: 'final',
}
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment