Created
March 22, 2022 01:58
-
-
Save marschhuynh/c4b8a6bfcbeaa9169b1541bfa21dc59f 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_DISPUTE = { | |
| target: '#engagement.disputed', | |
| actions: [assign((context, event, metaData) => { | |
| return { | |
| disputed_from_state: metaData && metaData.state.value, | |
| disputed_by: event.disputed_by, | |
| disputed_reason: event.disputed_reason | |
| } | |
| }), | |
| assign({ | |
| history: (context, event) => ([...context.history, { event: 'TRANSFER_DISPUTE', 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 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 && 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 SELLER_IMMEDIATE_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, buyer_cancelled: true } | |
| return { | |
| refuse_from_state: metaData && 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 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.post_id, | |
| 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: 'BUYER_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() }]), | |
| triggerUser: (context, event) => event.triggerUser | |
| }) | |
| } | |
| } | |
| }, | |
| history: { | |
| type: 'history' | |
| } | |
| }, | |
| on: { | |
| TRANSFER_REFUSE: SELLER_IMMEDIATE_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: SELLER_IMMEDIATE_TRANSFER_REFUSE, | |
| ADMIN_TRANSFER_REFUSE, | |
| TRANSFER_DISPUTE | |
| } | |
| }, | |
| transferring: { | |
| initial: 'confirmation', | |
| states: { | |
| 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, | |
| TRANSFER_DISPUTE | |
| } | |
| }, | |
| 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() }]) | |
| }) | |
| }, | |
| disputed: { | |
| type: 'final', | |
| entry: assign({ | |
| history: (context, event) => ([...context.history, { event: 'DISPUTED', 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, | |
| TRANSFER_DISPUTE | |
| } | |
| } | |
| } | |
| } | |
| const fetchMachine = Machine(STATE_SPEC); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment