Skip to content

Instantly share code, notes, and snippets.

@saary
Last active February 21, 2022 22:05
Show Gist options
  • Select an option

  • Save saary/5311a130c812544ed9cc07305f791076 to your computer and use it in GitHub Desktop.

Select an option

Save saary/5311a130c812544ed9cc07305f791076 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const createUser = ({
name,
phone,
email,
password,
country,
}) =>
fetch(`url/to/user/`).then((response) => response.json());
const signupMachine = Machine({
id: 'signup',
initial: 'fill_form',
context: {
name: null
},
states: {
fill_form: {
initial: 'fill',
states: {
fill: {
on: {
SUBMIT: 'create_user',
actions: assign({
name: (context, event) => event.name,
name: (context, event) => event.phone,
name: (context, event) => event.email,
name: (context, event) => event.password,
name: (context, event) => event.country,
})
}
},
create_user: {
invoke: {
id: 'createUser',
src: (context, event) => createUser(context),
onDone: {
target: 'user_created',
actions: assign({ pincode: (context, event) => event.data.pincode })
},
onError: {
target: 'fill',
actions: assign({ error: (context, event) => event.data })
}
}
},
user_created: {
type: 'final'
}
},
onDone: 'validate_pincode'
},
validate_pincode: {
on: {
RESEND: {
actions: 'resendPincode'
},
CHECK: [
{
target: 'info_screen',
cond: (context, event) => context.pincode === event.pincode
},
{
target: 'validate_pincode',
cond: (context, event) => context.retries < 3,
actions: assign({
error: 'wrong pincode',
retries: (context, event) => context.retries + 1
})
},
{
target: 'failure'
}
]
}
},
info_screen: {
on: {
NEXT: 'success',
}
},
success: {
type: 'final'
},
failure: {
type: 'final'
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment