Last active
October 7, 2021 19:51
-
-
Save sfarthin/71c2303d0ccb6e76151622d3b555a9a9 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const setErrorFromTransaction = assign({ | |
error: () => 'Some Error', | |
}); | |
const setSynapseTransactionId = assign({ | |
synapseTransactionId: () => Date.now(), | |
}); | |
const updateSynapseTransaction = assign({ | |
synapseTransaction: () => 'adasd', | |
}); | |
const setTipAmount = assign({ | |
tipAmountCents: () => 2000, | |
}); | |
const isSettledPayoutTransaction = () => true; | |
const isFailedPayoutTransaction = () => true; | |
const setDelayedRepaymentReason = assign({ | |
delayReason: () => 'Some Delay Reason', | |
}); | |
const canRepay = (_context, _event, { cond }) => cond.method.includes('full'); | |
const setUncollectableReason = assign({ | |
uncollectableReason: () => 'Some Uncollectable Reason', | |
}); | |
const setRepaymentReceipt = assign({ | |
repaymentReceipt: () => ({ | |
id: 'some receipt', | |
}), | |
}); | |
const updateRepaymentTransaction = () => null; | |
const updateIncomeTransaction = assign({ incomeTransaction: () => ({ id: 'transaction' }) }); | |
const getRepaymentStatus = (c, e, j) => { | |
console.log(j.cond.repayStatus); | |
return j.cond.repayStatus === 'repaid'; | |
}; | |
const fetchMachine = Machine( | |
{ | |
id: 'payAdvance', | |
initial: 'payout', | |
context: { | |
amountCents: 50, | |
expectedRepaymentDate: '2020-10-11', | |
}, | |
states: { | |
payout: { | |
initial: 'new', | |
states: { | |
new: { | |
on: { | |
initiatePayout: { | |
target: 'in_progress', | |
actions: ['setSynapseTransactionId'], | |
}, | |
}, | |
}, | |
in_progress: {}, | |
failed: {}, | |
}, | |
on: { | |
payoutWebhook: [ | |
{ | |
target: 'repayment', | |
cond: { type: 'isSettledPayoutTransaction' }, | |
}, | |
], | |
foundStuck: [ | |
{ | |
target: 'repayment', | |
cond: { type: 'isSettledPayoutTransaction' }, | |
}, | |
{ | |
target: 'payout.failed', | |
actions: ['setErrorFromTransaction'], | |
cond: { type: 'isFailedPayoutTransaction' }, | |
}, | |
], | |
}, | |
}, | |
repayment: { | |
initial: 'ready', | |
states: { | |
ready: { | |
initial: 'paid_out', | |
states: { | |
paid_out: {}, | |
delayed: {}, | |
failed: {}, | |
}, | |
on: { | |
manualRepayment: [ | |
{ | |
target: 'in_progress', | |
actions: ['setRepaymentReceipt'], | |
}, | |
], | |
autoRepayment: [ | |
{ | |
target: 'in_progress', | |
cond: { type: 'canRepay', method: ['full', 'partial'] }, | |
actions: ['setRepaymentReceipt'], | |
}, | |
{ | |
target: 'ready.delayed', | |
cond: { type: 'canRepay', method: ['delayed'] }, | |
actions: ['setDelayedRepaymentReason'], | |
}, | |
{ | |
target: 'uncollectable', | |
cond: { type: 'canRepay', method: ['uncollectable'] }, | |
actions: ['setUncollectableReason'], | |
}, | |
], | |
}, | |
}, | |
in_progress: {}, | |
uncollectable: {}, | |
repaid: {}, | |
}, | |
on: { | |
stripeChargeHook: [ | |
{ | |
target: 'repayment.repaid', | |
cond: { type: 'getRepaymentStatus', repayStatus: 'repaid' }, | |
}, | |
{ | |
target: 'repayment.ready.delayed', | |
cond: { type: 'getRepaymentStatus', repayStatus: 'partial' }, | |
}, | |
{ | |
target: 'repayment.ready.failed', | |
cond: { type: 'getRepaymentStatus', repayStatus: 'failed' }, | |
}, | |
], | |
}, | |
}, | |
}, | |
on: { | |
setTipAmount: { actions: ['setTipAmount'] }, | |
payoutWebhook: [ | |
{ | |
target: 'payout.failed', | |
actions: ['setErrorFromTransaction'], | |
cond: { type: 'isFailedPayoutTransaction' }, | |
}, | |
{ | |
actions: ['updateSynapseTransaction'], | |
}, | |
], | |
// sawRepaymentTransaction: { | |
// actions: ['updateRepaymentTransaction'], | |
// }, | |
sawIncomeTransaction: { | |
actions: ['updateIncomeTransaction'], | |
}, | |
stripeChargeHook: { | |
actions: ['updateRepaymentTransaction'], | |
}, | |
}, | |
}, | |
{ | |
guards: { | |
isFailedPayoutTransaction, | |
isSettledPayoutTransaction, | |
canRepay, | |
getRepaymentStatus, | |
}, | |
actions: { | |
setSynapseTransactionId, | |
updateSynapseTransaction, | |
setTipAmount, | |
setErrorFromTransaction, | |
setDelayedRepaymentReason, | |
setRepaymentReceipt, | |
setUncollectableReason, | |
updateRepaymentTransaction, | |
updateIncomeTransaction, | |
}, | |
}, | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment