Last active
January 4, 2022 16:41
-
-
Save marschhuynh/f4fd79a1f72846a96c14d7ae66cbb047 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
const TRANSFER_REFUSE = { | |
target: '#engagement.canceling', | |
cond: (context) => !context.buyer_cancelled && !context.seller_cancelled, | |
actions: [assign((context, event, metaData) => { | |
const addition = context.buyer === event.cancelled_by | |
? { buyer_cancelled: true } | |
: { seller_cancelled: true } | |
return { | |
refuse_from_state: metaData.state.value, | |
cancelled_by: event.cancelled_by, | |
cancelled_reason: event.cancelled_reason, | |
...addition | |
} | |
}), | |
assign({ | |
history: (context, event) => ([...context.history, { event: 'TRANSFER_REFUSE', at: Date.now() }]) | |
})] | |
} | |
const ADMIN_TRANSFER_REFUSE = { | |
target: '#engagement.cancelled', | |
actions: [assign((context, event, metaData) => { | |
return { | |
cancelled_by: event.cancelled_by, | |
cancelled_reason: event.cancelled_reason, | |
is_admin_cancelled: true | |
} | |
})] | |
} | |
const STATE_SPEC = { | |
id: 'engagement', | |
initial: 'initial', | |
context: { | |
engagement_id: null, | |
buyer: null, | |
seller: null, | |
buyer_rated: false, | |
seller_rated: false, | |
buyer_confirmed: false, | |
seller_confirmed: false, | |
deposit: null, | |
refuse_from_state: {}, | |
history: [] | |
}, | |
states: { | |
initial: { | |
on: { | |
BUYER_ENGAGE: { | |
target: 'engaged', | |
actions: assign((context, event) => { | |
return { | |
engagement_id: event.engagement_id, | |
buyer: event.buyer, | |
seller: event.seller, | |
post_id: event.postId, | |
history: [ | |
...context.history, | |
{ event: 'BUYER_ENGAGED', at: Date.now() } | |
] | |
} | |
}) | |
} | |
} | |
}, | |
engaged: { | |
initial: 'reviewing', | |
states: { | |
reviewing: { | |
on: { | |
SELLER_DEPOSIT: { | |
target: 'depositing', | |
actions: assign({ | |
history: (context, event) => ([...context.history, { event: 'SELLER_DEPOSIT', at: Date.now() }]), | |
deposit: (context, event) => ({ | |
...context.deposit, | |
deposit_time: event.deposit_time, | |
aid: event.aid, | |
method: event.method, | |
amount: event.amount, | |
paymentId: event.paymentId, | |
depositRole: 'seller' | |
}) | |
}) | |
}, | |
BUYER_DEPOSIT: { | |
target: 'depositing', | |
actions: assign({ | |
history: (context, event) => ([...context.history, { event: 'SELLER_DEPOSIT', at: Date.now() }]), | |
deposit: (context, event) => ({ | |
...context.deposit, | |
deposit_time: event.deposit_time, | |
aid: event.aid, | |
method: event.method, | |
amount: event.amount, | |
paymentId: event.paymentId, | |
depositRole: 'buyer' | |
}) | |
}) | |
} | |
} | |
}, | |
depositing: { | |
on: { | |
DEPOSIT_SUCCESS: { | |
target: 'deposit_success', | |
actions: assign({ | |
history: (context, event) => ([...context.history, { event: 'DEPOSIT_SUCCESS', at: Date.now() }]), | |
deposit: (context, event) => ({ | |
...context.deposit, | |
deposit_success: Date.now(), | |
authToken: event.authToken, | |
payType: event.payType | |
}) | |
}) | |
}, | |
DEPOSIT_FAIL: { | |
target: 'deposit_failure', | |
actions: assign({ | |
history: (context, event) => ([...context.history, { event: 'DEPOSIT_FAIL', at: Date.now() }]), | |
deposit: (context, event) => ({ | |
...context.deposit, | |
deposit_fail: Date.now() | |
}) | |
}) | |
} | |
} | |
}, | |
deposit_failure: { | |
on: { | |
RETRY_DEPOSIT: { | |
target: 'reviewing', | |
actions: assign({ | |
history: (context, event) => ([...context.history, { event: 'RETRY_DEPOSIT', at: Date.now() }]) | |
}) | |
} | |
} | |
}, | |
deposit_success: { | |
on: { | |
OPEN_CHAT: { | |
target: '#engagement.preparing', | |
actions: assign({ | |
history: (context, event) => ([...context.history, { event: 'OPEN_CHAT', at: Date.now() }]) | |
}) | |
} | |
} | |
}, | |
history: { | |
type: 'history' | |
} | |
}, | |
on: { TRANSFER_REFUSE, ADMIN_TRANSFER_REFUSE } | |
}, | |
preparing: { | |
initial: 'doc_preparing', | |
states: { | |
doc_preparing: { | |
on: { | |
TRANSFER_CONFIRM: { | |
target: '#engagement.transferring', | |
actions: assign({ | |
history: (context, event) => ([...context.history, { event: 'TRANSFER_CONFIRM', at: Date.now() }]) | |
}) | |
} | |
} | |
}, | |
history: { | |
type: 'history' | |
} | |
}, | |
on: { TRANSFER_REFUSE, ADMIN_TRANSFER_REFUSE } | |
}, | |
transferring: { | |
initial: 'ship_preparing', | |
states: { | |
ship_preparing: { | |
on: { | |
CONFIRM_SHIP: { | |
target: 'shipping', | |
actions: assign({ | |
history: (context, event) => ([...context.history, { event: 'CONFIRM_SHIP', at: Date.now() }]) | |
}) | |
} | |
} | |
}, | |
shipping: { | |
on: { | |
BUYER_CONFIRM_RECEIVED: { | |
target: 'confirmation', | |
actions: assign({ | |
history: (context, event) => ([...context.history, { event: 'BUYER_CONFIRM_RECEIVED', at: Date.now() }]) | |
}) | |
} | |
} | |
}, | |
confirmation: { | |
on: { | |
BUYER_CONFIRM: { | |
actions: [ | |
assign({ buyer_confirmed: true }), | |
assign({ | |
history: (context, event) => ([...context.history, { event: 'BUYER_CONFIRM', at: Date.now() }]) | |
}) | |
] | |
}, | |
SELLER_CONFIRM: { | |
actions: [ | |
assign({ seller_confirmed: true }), | |
assign({ | |
history: (context, event) => ([...context.history, { event: 'SELLER_CONFIRM', at: Date.now() }]) | |
}) | |
] | |
}, | |
'': { | |
target: '#engagement.rating', | |
cond: (context) => context.buyer_confirmed && context.seller_confirmed | |
} | |
} | |
}, | |
history: { | |
type: 'history' | |
} | |
}, | |
on: { TRANSFER_REFUSE, ADMIN_TRANSFER_REFUSE } | |
}, | |
rating: { | |
entry: assign({ | |
history: (context, event) => ([...context.history, { event: 'COMPLETED', at: Date.now() }]) | |
}), | |
on: { | |
BUYER_RATE: { | |
actions: [ | |
assign({ buyer_rated: true }), | |
assign({ | |
history: (context, event) => ([...context.history, { event: 'BUYER_RATE', at: Date.now() }]) | |
}) | |
] | |
}, | |
SELLER_RATE: { | |
actions: [ | |
assign({ seller_rated: true }), | |
assign({ | |
history: (context, event) => ([...context.history, { event: 'SELLER_RATE', at: Date.now() }]) | |
}) | |
] | |
}, | |
'': { | |
target: '#engagement.completed', | |
cond: (context) => context.buyer_rated && context.seller_rated | |
} | |
} | |
}, | |
completed: { | |
type: 'final', | |
entry: assign({ | |
history: (context, event) => ([...context.history, { event: 'COMPLETED', at: Date.now() }]) | |
}) | |
}, | |
cancelled: { | |
type: 'final', | |
entry: assign({ | |
history: (context, event) => ([...context.history, { event: 'CANCELLED', at: Date.now() }]) | |
}) | |
}, | |
canceling: { | |
initial: 'waiting', | |
states: { | |
waiting: { | |
on: { | |
SELLER_CONFIRM_CANCEL: { | |
actions: [ | |
assign({ seller_cancelled: true }), | |
assign({ | |
history: (context, event) => ([...context.history, { event: 'SELLER_CONFIRM_CANCEL', at: Date.now() }]) | |
}) | |
] | |
}, | |
BUYER_CONFIRM_CANCEL: { | |
actions: [ | |
assign({ buyer_cancelled: true }), | |
assign({ | |
history: (context, event) => ([...context.history, { event: 'BUYER_CONFIRM_CANCEL', at: Date.now() }]) | |
}) | |
] | |
}, | |
BUYER_DECLINE_CANCEL: { | |
actions: [ | |
assign({ seller_cancelled: false, buyer_cancelled: false }), | |
assign({ | |
history: (context, event) => ([...context.history, { event: 'BUYER_DECLINE_CANCEL', at: Date.now() }]) | |
}) | |
] | |
}, | |
SELLER_DECLINE_CANCEL: { | |
actions: [ | |
assign({ seller_cancelled: false, buyer_cancelled: false }), | |
assign({ | |
history: (context, event) => ([...context.history, { event: 'SELLER_DECLINE_CANCEL', at: Date.now() }]) | |
}) | |
] | |
}, | |
'': [ | |
{ | |
target: '#engagement.engaged.history', | |
cond: (context) => !context.seller_cancelled && !context.buyer_cancelled && context.refuse_from_state.engaged | |
}, | |
{ | |
target: '#engagement.preparing', | |
cond: (context) => !context.seller_cancelled && !context.buyer_cancelled && context.refuse_from_state.preparing | |
}, | |
{ | |
target: '#engagement.transferring.history', | |
cond: (context) => !context.seller_cancelled && !context.buyer_cancelled && context.refuse_from_state.transferring | |
}, | |
{ | |
target: '#engagement.cancelled', | |
cond: (context) => context.seller_cancelled && context.buyer_cancelled | |
} | |
] | |
} | |
} | |
}, | |
on: { ADMIN_TRANSFER_REFUSE } | |
} | |
} | |
} | |
const machine = Machine(STATE_SPEC) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment