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.

Revisions

  1. hosmelq revised this gist Apr 18, 2020. 1 changed file with 20 additions and 13 deletions.
    33 changes: 20 additions & 13 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -119,20 +119,21 @@ Machine(
    initial: `initializing`,
    context: {
    state: null,
    checks2: {},
    checks: {
    emergencyContact: {
    message: null,
    state: 'PENDING',
    },
    picture: {
    message: null,
    state: 'PENDING',
    },
    vehicleInformation: {
    message: null,
    state: 'PENDING',
    },
    },
    emergencyContact: {
    message: null,
    state: 'PENDING',
    },
    picture: {
    message: null,
    state: 'PENDING',
    },
    vehicleInformation: {
    message: null,
    state: 'PENDING',
    },
    },
    },
    states: {
    initializing: {
    @@ -170,6 +171,12 @@ Machine(
    ...ctx.checks[key],
    ref: spawn(checkMachine.withContext(ctx.checks[key])),
    }));
    },
    checks2: (ctx) => {
    return {
    ...ctx.checks2,
    emergencyContact: spawn(emergencyContactMachine, `emergencyContact`)
    }
    }
    })
    },
  2. hosmelq revised this gist Apr 18, 2020. 1 changed file with 62 additions and 0 deletions.
    62 changes: 62 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,68 @@ const CourierState = {
    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`,
  3. hosmelq revised this gist Apr 17, 2020. No changes.
  4. hosmelq revised this gist Apr 17, 2020. 1 changed file with 24 additions and 11 deletions.
    35 changes: 24 additions & 11 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -14,17 +14,17 @@ const checkMachine = Machine(
    {
    id: `check`,
    initial: `unknown`,
    context: {
    message: null,
    state: null,
    },
    states: {
    unknown: {
    on: {
    '': [
    {
    target: `completed`,
    cond: (ctx) => ctx.completed,
    },
    {
    target: `pending`,
    },
    {cond: `isInRevision`, target: `inRevision`},
    {cond: `isPending`, target: `pending`},
    {cond: `isVerified`, target: `verified`},
    ],
    },
    },
    @@ -56,8 +56,21 @@ Machine(
    id: `onboard`,
    initial: `initializing`,
    context: {
    checks: [],
    state: null,
    checks: {
    emergencyContact: {
    message: null,
    state: 'PENDING',
    },
    picture: {
    message: null,
    state: 'PENDING',
    },
    vehicleInformation: {
    message: null,
    state: 'PENDING',
    },
    },
    },
    states: {
    initializing: {
    @@ -91,9 +104,9 @@ Machine(
    actions: {
    initializeChecks: assign({
    checks: (ctx) => {
    return ctx.checks.map(check => ({
    ...check,
    ref: spawn(checkMachine.withContext(check)),
    return Object.keys(ctx.checks).map(key => ({
    ...ctx.checks[key],
    ref: spawn(checkMachine.withContext(ctx.checks[key])),
    }));
    }
    })
  5. hosmelq created this gist Apr 17, 2020.
    105 changes: 105 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,105 @@
    const CourierOnboardingVerificationsItemState = {
    IN_REVISION: `IN_REVISION`,
    PENDING: `PENDING`,
    VERIFIED: `VERIFIED`,
    }
    const CourierState = {
    ACTIVE: `ACTIVE`,
    BLOCKED: `BLOCKED`,
    PENDING: `PENDING`,
    REJECTED: `REJECTED`,
    }

    const checkMachine = Machine(
    {
    id: `check`,
    initial: `unknown`,
    states: {
    unknown: {
    on: {
    '': [
    {
    target: `completed`,
    cond: (ctx) => ctx.completed,
    },
    {
    target: `pending`,
    },
    ],
    },
    },

    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: {
    checks: [],
    state: null,
    },
    states: {
    initializing: {
    entry: `initializeChecks`,
    on: {
    '': `unknown`,
    },
    },

    initializeChecks: {},

    unknown: {
    on: {
    '': [
    {
    cond: `isActive`,
    target: `active`,
    },
    ],
    },
    },

    active: {
    meta: {
    title: `Activa`,
    },
    },
    },
    },
    {
    actions: {
    initializeChecks: assign({
    checks: (ctx) => {
    return ctx.checks.map(check => ({
    ...check,
    ref: spawn(checkMachine.withContext(check)),
    }));
    }
    })
    },
    guards: {
    isActive: (ctx) => ctx.state === CourierState.ACTIVE,
    },
    }
    )