Created
April 15, 2021 08:56
-
-
Save riccardo-forina/ce7db081ed350655c1107d9ba39a4d36 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const kafkaMachine = Machine({ | |
id: 'kafkas', | |
initial: 'loading', | |
context: {}, | |
states: { | |
loading: { | |
invoke: { | |
id: 'fetchKafkaInstances', | |
src: 'something', | |
onDone: { | |
target: 'verify', | |
actions: assign({ | |
instances: (_context, event) => event.data.data, | |
}), | |
}, | |
onError: { | |
target: 'failure', | |
actions: assign({ | |
error: (_context, event) => event.data, | |
}), | |
}, | |
}, | |
}, | |
failure: { | |
}, | |
verify: { | |
on: { | |
'': [ | |
{ target: 'selecting', cond: 'noInstanceSelected' }, | |
{ target: 'valid', cond: 'instanceSelected' }, | |
] | |
} | |
}, | |
selecting: { | |
entry: sendParent('isInvalid'), | |
on: { | |
selectInstance: { | |
target: 'valid', | |
actions: 'selectInstance', | |
}, | |
}, | |
}, | |
valid: { | |
entry: sendParent('isValid'), | |
on: { | |
selectInstance: { | |
target: 'verify', | |
actions: 'selectInstance', | |
}, | |
deselectInstance: { | |
target: 'verify', | |
actions: 'reset' | |
}, | |
confirm: { | |
target: 'done' | |
} | |
} | |
}, | |
done: { | |
type: "final", | |
data: { | |
selectedInstance: (context) => context.selectedInstance | |
} | |
} | |
}, | |
}, { | |
services: { | |
'something': Promise.resolve({ | |
data: { | |
data: { | |
instances: [] | |
} | |
} | |
}) | |
}, | |
actions: { | |
selectInstance: assign({ | |
selectedInstance: (context, event) => { | |
if (event.type === "selectInstance") { | |
return context.instances?.items.find(i => i.id == event.selectedInstance); | |
} | |
return context.selectedInstance; | |
} | |
}), | |
reset: assign({ | |
selectedInstance: (context, event) => event.type === "deselectInstance" ? undefined : context.selectedInstance | |
}) | |
}, | |
guards: { | |
instanceSelected: (context) => context.selectedInstance !== undefined, | |
noInstanceSelected: (context) => context.selectedInstance === undefined, | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment