Created
November 17, 2021 02:24
-
-
Save mwangaben/9acb7bd7bde6270c69dff82f27eb3949 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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: 'inBooking' | |
}, | |
{ | |
target: 'error', | |
actions: assign({error: (ctx, event) => 'Please select a seat to proceed'}) | |
} | |
], | |
} | |
}, | |
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'}, | |
BOOKING: {target: 'booking'}, | |
} | |
}, | |
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
Last edit