Skip to content

Instantly share code, notes, and snippets.

@hosmelq
Last active April 18, 2020 00:40
Show Gist options
  • Save hosmelq/e94329abf9741d1101334a73cd41787c to your computer and use it in GitHub Desktop.
Save hosmelq/e94329abf9741d1101334a73cd41787c to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const CourierOnboardingVerificationsItemState = {
IN_REVISION: `IN_REVISION`,
PENDING: `PENDING`,
VERIFIED: `VERIFIED`,
}
const CourierState = {
ACTIVE: `ACTIVE`,
BLOCKED: `BLOCKED`,
PENDING: `PENDING`,
REJECTED: `REJECTED`,
}
//
const emergencyContactMachine = Machine(
{
id: `emergencyContact`,
initial: `unknown`,
context: {
message: null,
state: `PENDING`,
},
states: {
unknown: {
on: {
'': [
{cond: `isPending`, target: `pending`},
{cond: `isVerified`, target: `verified`},
],
},
},
pending: {
on: {
SUBMIT: `submitting`,
},
},
submitting: {
invoke: {
id: `update-information`,
src: `updateInformation`,
onDone: `verified`,
onError: {
target: `pending`,
actions: `handleGraphQLErrors`,
},
},
},
verified: {
entry: `updateInformationSuccess`,
type: `final`,
},
},
},
{
guards: {
isPending: (ctx) => {
return ctx.state === CourierOnboardingVerificationsItemState.PENDING
},
isVerified: (ctx) => {
return ctx.state === CourierOnboardingVerificationsItemState.VERIFIED
},
},
}
)
//
const checkMachine = Machine(
{
id: `check`,
initial: `unknown`,
context: {
message: null,
state: null,
},
states: {
unknown: {
on: {
'': [
{cond: `isInRevision`, target: `inRevision`},
{cond: `isPending`, target: `pending`},
{cond: `isVerified`, target: `verified`},
],
},
},
inRevision: {},
pending: {},
verified: {},
},
},
{
guards: {
isInRevision: (ctx) => {
return ctx.state === CourierOnboardingVerificationsItemState.IN_REVISION
},
isPending: (ctx) => {
return ctx.state === CourierOnboardingVerificationsItemState.PENDING
},
isVerified: (ctx) => {
return ctx.state === CourierOnboardingVerificationsItemState.VERIFIED
},
},
}
)
Machine(
{
id: `onboard`,
initial: `initializing`,
context: {
state: null,
checks2: {},
checks: {
emergencyContact: {
message: null,
state: 'PENDING',
},
picture: {
message: null,
state: 'PENDING',
},
vehicleInformation: {
message: null,
state: 'PENDING',
},
},
},
states: {
initializing: {
entry: `initializeChecks`,
on: {
'': `unknown`,
},
},
initializeChecks: {},
unknown: {
on: {
'': [
{
cond: `isActive`,
target: `active`,
},
],
},
},
active: {
meta: {
title: `Activa`,
},
},
},
},
{
actions: {
initializeChecks: assign({
checks: (ctx) => {
return Object.keys(ctx.checks).map(key => ({
...ctx.checks[key],
ref: spawn(checkMachine.withContext(ctx.checks[key])),
}));
},
checks2: (ctx) => {
return {
...ctx.checks2,
emergencyContact: spawn(emergencyContactMachine, `emergencyContact`)
}
}
})
},
guards: {
isActive: (ctx) => ctx.state === CourierState.ACTIVE,
},
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment