Last active
March 21, 2022 15:12
-
-
Save marschhuynh/382512ad45bde7e684cd8cfe82939d2b 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.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 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: { | |
OPEN_CHAT: { | |
target: '#engagement.rating', | |
actions: assign({ | |
history: (context, event) => ([...context.history, { event: 'OPEN_CHAT', at: Date.now() }]) | |
}) | |
} | |
} | |
}, | |
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() }]) | |
}) | |
} | |
} | |
} | |
const machine = Machine(STATE_SPEC) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment