Created
August 12, 2021 15:33
-
-
Save mwangaben/f34f11ec8136a6a39d567f0f61a60e5d 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
function makeBooking(context, event) { | |
console.log(event) | |
let url = `http://localhost:4000/seats/${event.id}`; | |
let data = { | |
id: event.id, | |
number: event.number, | |
state: 'booked' | |
} | |
axios.patch(url, data).then(({data}) => { | |
console.log(data) | |
context.seat = data | |
return data; | |
}).catch(response => response.error); | |
} | |
const seatMachine = Machine({ | |
id: 'seat-machine', | |
initial: 'init', | |
context: { | |
error: '', | |
seat: '', | |
}, | |
states: { | |
init: { | |
on: { | |
AVAILABLE: {target: 'available'}, | |
BOOKED: {target: 'booked'}, | |
BOOKING: {target: 'booking'}, | |
INBOOKING: {target: 'inBooking'}, | |
} | |
}, | |
available: { | |
on: { | |
BOOK: [ | |
{ | |
cond: (ctx, event) => event.number !== '', | |
target: 'booking' | |
}, | |
{ | |
target: 'error', | |
actions: assign({error: (ctx, event) => 'Please select a seat to proceed'}) | |
} | |
], | |
CANCEL: {target: 'available'} | |
} | |
}, | |
booked: {target: 'final'}, | |
booking: { | |
invoke: { | |
id: 'booking-a-ticket', | |
src: (context, event) => makeBooking(context, event), | |
onDone: { | |
actions: assign({seat: (context, event) => { | |
console.log(event) | |
return event.data | |
} | |
}), | |
target: 'success' | |
}, | |
onError: { | |
target: 'error', | |
actions: assign({error: (context, event) => event.data}) | |
} | |
} | |
}, | |
inBooking: { | |
on: { | |
AVAILABLE: {target: 'available'}, | |
BOOKED: {target: 'booked'}, | |
} | |
}, | |
success: { | |
after: { | |
2000: { | |
target: 'booked' | |
} | |
} | |
}, | |
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