Skip to content

Instantly share code, notes, and snippets.

@tmbtech
Created January 30, 2020 01:08
Show Gist options
  • Select an option

  • Save tmbtech/e12bbc738a0ba16000564f9cca7ace90 to your computer and use it in GitHub Desktop.

Select an option

Save tmbtech/e12bbc738a0ba16000564f9cca7ace90 to your computer and use it in GitHub Desktop.

Revisions

  1. tmbtech revised this gist Jan 30, 2020. No changes.
  2. tmbtech created this gist Jan 30, 2020.
    158 changes: 158 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,158 @@
    // Available variables:
    // - Machine
    // - interpret
    // - assign
    // - send
    // - sendParent
    // - spawn
    // - raise
    // - actions
    // - XState (all XState exports)

    const fetchMachine = Machine({
    id: "register_user",
    initial: "checkUserStatus",
    context: {
    alleToken: null,
    sessionToken: null,
    oktaTokens: null,
    registrationFields: {
    firstName: "",
    lastName: "",
    emailAddress: "",
    password: ""
    }
    },
    states: {
    checkUserStatus: {
    initial: "checkUserLoggedIn",
    states: {
    checkUserLoggedIn: {
    invoke: {
    id: "checkIsLoggedIn",
    src: "checkIsLoggedIn",
    onDone: "isLoggedIn",
    onError: "isNotLoggedIn"
    }
    },
    isLoggedIn: {
    entry: "redirectToAccount"
    },
    isNotLoggedIn: {
    invoke: {
    id: "verifySMSCode",
    src: "verifySMSCode",
    onDone: {
    target: "#waitForInput",
    actions: "setAlleToken"
    },
    onError: {
    target: "#redirectToPhoneVerification",
    actions: "redirectToPhoneVerification"
    }
    }
    }
    }
    },

    redirectToPhoneVerification: {
    id: "redirectToPhoneVerification",
    type: "final"
    },
    waitForInput: {
    id: "waitForInput",
    on: {
    REGISTRATION_SUBMITTED: {
    target: "hipaRequirement",
    actions: "setRegistrationFields"
    }
    }
    },
    hipaRequirement: {
    initial: "showHipaaNotice",
    states: {
    showHipaaNotice: {
    on: {
    DECLINE: "confirmHipaaDecline",
    REGISTRATION_COMPLETE: {
    target: "#getOktaToken",
    actions: "setSessionToken"
    },
    REGISTRATION_ERROR: {
    target: "acceptedHipaaWaitForInput"
    }
    }
    },
    acceptedHipaaWaitForInput: {
    on: {
    REGISTRATION_COMPLETE: {
    target: "#getOktaToken",
    actions: "setSessionToken"
    },
    REGISTRATION_ERROR: {
    target: "acceptedHipaaWaitForInput"
    }
    }
    },
    confirmHipaaDecline: {
    on: {
    CLOSE: "showHipaaNotice",
    EXIT_REGISTRATION: "#redirectToHomepage",
    REGISTRATION_COMPLETE: {
    target: "#getOktaToken",
    actions: "setSessionToken"
    },
    REGISTRATION_ERROR: {
    target: "acceptedHipaaWaitForInput"
    }
    }
    }
    }
    },

    oktaAuth: {
    states: {
    getOktaToken: {
    id: "getOktaToken",
    invoke: {
    id: "getOktaToken",
    src: "getOktaToken",
    onDone: {
    target: "updateTokenManager",
    actions: "setOktaTokens"
    },
    onError: "#redirectToLogin"
    }
    },
    updateTokenManager: {
    invoke: {
    id: "updateTokenManger",
    src: "updateTokenManager",
    onDone: "#registrationComplete",
    onError: "#redirectToLogin"
    }
    }
    }
    },

    redirects: {
    states: {
    redirectToLogin: {
    id: "redirectToLogin",
    entry: "redirectToLogin",
    type: "final"
    },
    registrationComplete: {
    id: "registrationComplete",
    entry: "redirectToAccount",
    type: "final"
    },
    redirectToHomepage: {
    id: "redirectToHomepage",
    entry: "redirectToHomepage",
    type: "final"
    }
    }
    }
    }
    });