Skip to content

Instantly share code, notes, and snippets.

@mwangaben
Last active August 8, 2021 19:58
Show Gist options
  • Save mwangaben/b8ee09f54f2e2a53df7c74be23f2cc26 to your computer and use it in GitHub Desktop.
Save mwangaben/b8ee09f54f2e2a53df7c74be23f2cc26 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
function makeBooking(context, event) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Your Ticket has been booked Successful ')
}, 1000)
})
}
const seatMachine = Machine({
id: 'seat-ticket',
initial: 'init',
context: {
msg: '',
error: ''
},
states: {
init: {
on: {
BOOKED: {target: 'booked'},
BOOKING: {target: 'inBooking'},
AVAILABLE: {target: 'available'},
}
},
inBooking: {
on:{
AVAILABLE: {target: 'available'},
BOOKED: {target: 'booked'},
}
},
available: {
on: {
BOOK: [
{
cond: (ctx, event) => event.seat !== '',
target: 'booking'
},
{
target: 'error',
actions: assign({error: (ctx, event) => 'Please fill the form fields'})
}
],
CANCEL: {target: 'available'}
}
},
booked: {
target: 'final',
},
booking: {
invoke: {
id: 'booking-a-ticket',
src: (context, event) => makeBooking(context, event),
onDone: {
actions: assign({msg: (context, event) => event.data}),
target: 'booked'
},
onError: {
target: 'error',
actions: assign({error: (context, event) => event.data})
}
}
},
error: {
after: {
2000: {
target: 'available',
actions: assign({error: (ctx, event) => ''})
}
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment