Skip to content

Instantly share code, notes, and snippets.

@BrianCortes
Last active June 17, 2021 22:08
Show Gist options
  • Save BrianCortes/af905ada3d2a94ea5948ef5d35fc24c6 to your computer and use it in GitHub Desktop.
Save BrianCortes/af905ada3d2a94ea5948ef5d35fc24c6 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 fetchPayments = new Promise((resolve) => {
setTimeout(() => {
const paymentList = [
{ id: '1', cardNumber: 1 },
{ id: '2', cardNumber: 2 }
];
resolve(paymentList);
}, 3000);
});
const fetchMachine = Machine(
{
id: 'payment',
type: 'parallel',
context: {
payments: []
},
states: {
paymentActions: {
initial: 'idle',
states: {
idle: {
on: {
ADD_CARD: {
target: '',
actions: ['goToAddCard']
},
DELETE_CARD: { target: 'confirmation' }
}
},
confirmation: {
on: {
YES: {
target: 'idle',
actions: ['deleteCard']
},
NOP: { target: 'idle' }
}
}
}
},
fetchPayments: {
initial: 'fetching',
states: {
fetching: {
invoke: {
id: 'fetchPayments',
src: fetchPayments,
onDone: {
target: 'success',
actions: ['savePayments']
},
onError: {
target: 'failure',
actions: ['showError']
}
}
},
success: {
on: {
REFRESH_PAYMENTS: { target: 'fetching' }
}
},
failure: {
on: {
REFRESH_PAYMENTS: { target: 'fetching' }
}
}
}
}
}
},
{
actions: {
goToAddCard: () => {},
savePayments: assign({
payments: (context, event) => {
console.log(event);
return event.data;
}
}),
showError: () => {},
deleteCard: assign({
payments: (context, event) => {
console.log('deleteCard', context.payments.splice(1, 1))
return context.payments.splice(1, 1);
}
})
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment