Skip to content

Instantly share code, notes, and snippets.

@larrybotha
Last active September 20, 2019 21:32
Show Gist options
  • Select an option

  • Save larrybotha/15c143d743a94880f8e30dbce3794a46 to your computer and use it in GitHub Desktop.

Select an option

Save larrybotha/15c143d743a94880f8e30dbce3794a46 to your computer and use it in GitHub Desktop.

Revisions

  1. Larry Botha revised this gist Sep 20, 2019. No changes.
  2. Larry Botha revised this gist Sep 20, 2019. No changes.
  3. Larry Botha revised this gist Sep 20, 2019. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -36,10 +36,6 @@ const xeroOrgMachine = Machine({
    },
    },

    orgFound: {
    type: "final",
    },

    selectOrg: {
    on: {
    ORG_SELECTED: {
    @@ -91,6 +87,10 @@ const xeroOrgMachine = Machine({
    },
    },
    },

    orgFound: {
    type: "final",
    },
    }
    }, {
    guards: {
  4. Larry Botha revised this gist Sep 20, 2019. 1 changed file with 31 additions and 31 deletions.
    62 changes: 31 additions & 31 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,37 @@ const xeroOrgMachine = Machine({
    },
    },

    gettingOrgs: {
    invoke: {
    src: "getOrgs",
    onDone: "selectOrg",
    onError: "error.getOrgs",
    },
    },

    orgFound: {
    type: "final",
    },

    selectOrg: {
    on: {
    ORG_SELECTED: {
    target: "exchangingToken"
    },
    CREATE_ORG: {
    target: "creatingOrg",
    }
    }
    },

    creatingOrg: {
    invoke: {
    src: "createOrg",
    onDone: "exchangingToken",
    onError: "error.createOrg"
    }
    },

    error: {
    states: {
    tokenExchange: {
    @@ -60,37 +91,6 @@ const xeroOrgMachine = Machine({
    },
    },
    },

    gettingOrgs: {
    invoke: {
    src: "getOrgs",
    onDone: "selectOrg",
    onError: "error.getOrgs",
    },
    },

    creatingOrg: {
    invoke: {
    src: "createOrg",
    onDone: "exchangingToken",
    onError: "error.createOrg"
    }
    },

    orgFound: {
    type: "final",
    },

    selectOrg: {
    on: {
    ORG_SELECTED: {
    target: "exchangingToken"
    },
    CREATE_ORG: {
    target: "creatingOrg",
    }
    }
    },
    }
    }, {
    guards: {
  5. Larry Botha revised this gist Sep 20, 2019. 1 changed file with 31 additions and 13 deletions.
    44 changes: 31 additions & 13 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -18,8 +18,8 @@ const xeroOrgMachine = Machine({
    onDone: "orgFound",
    onError: [
    {
    cond: "hasMultipleOrgs",
    target: "multipleOrgs",
    cond: "noAssociatedOrg",
    target: "gettingOrgs",
    },
    {
    target: "error.tokenExchange"
    @@ -28,14 +28,6 @@ const xeroOrgMachine = Machine({
    },
    },

    multipleOrgs: {
    invoke: {
    src: "getOrgs",
    onDone: "selectOrg",
    onError: "error.multipleOrgs",
    }
    },

    error: {
    states: {
    tokenExchange: {
    @@ -45,10 +37,17 @@ const xeroOrgMachine = Machine({
    },
    },
    },
    multipleOrgs: {
    createOrg: {
    on: {
    RETRY: {
    target: "#xeroOrg.multipleOrgs"
    target: "#xeroOrg.creatingOrg"
    },
    },
    },
    getOrgs: {
    on: {
    RETRY: {
    target: "#xeroOrg.gettingOrgs"
    },
    },
    },
    @@ -62,6 +61,22 @@ const xeroOrgMachine = Machine({
    },
    },

    gettingOrgs: {
    invoke: {
    src: "getOrgs",
    onDone: "selectOrg",
    onError: "error.getOrgs",
    },
    },

    creatingOrg: {
    invoke: {
    src: "createOrg",
    onDone: "exchangingToken",
    onError: "error.createOrg"
    }
    },

    orgFound: {
    type: "final",
    },
    @@ -70,12 +85,15 @@ const xeroOrgMachine = Machine({
    on: {
    ORG_SELECTED: {
    target: "exchangingToken"
    },
    CREATE_ORG: {
    target: "creatingOrg",
    }
    }
    },
    }
    }, {
    guards: {
    hasMultipleOrgs: () => true
    noAssociatedOrg: () => Math.random() > .5
    }
    });
  6. Larry Botha revised this gist Sep 20, 2019. No changes.
  7. Larry Botha revised this gist Sep 20, 2019. 1 changed file with 6 additions and 15 deletions.
    21 changes: 6 additions & 15 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -29,21 +29,10 @@ const xeroOrgMachine = Machine({
    },

    multipleOrgs: {
    initial: "fetching",

    invoke: {
    src: "getOrgs",
    onDone: "multipleOrgs.success",
    onError: "#xeroOrg.error.multipleOrgs",
    },

    states: {
    fetching: {},
    success: {
    on: {
    SELECT_ORG: "#xeroOrg.exchangingToken",
    },
    },
    onDone: "selectOrg",
    onError: "error.multipleOrgs",
    }
    },

    @@ -73,12 +62,14 @@ const xeroOrgMachine = Machine({
    },
    },

    orgFound: {},
    orgFound: {
    type: "final",
    },

    selectOrg: {
    on: {
    ORG_SELECTED: {
    target: "#xeroOrg.exchangingToken"
    target: "exchangingToken"
    }
    }
    },
  8. Larry Botha revised this gist Sep 20, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ const xeroOrgMachine = Machine({
    target: "multipleOrgs",
    },
    {
    target: "error.exchangeToken"
    target: "error.tokenExchange"
    },
    ],
    },
    @@ -49,7 +49,7 @@ const xeroOrgMachine = Machine({

    error: {
    states: {
    tokenExchangeFailure: {
    tokenExchange: {
    on: {
    RETRY: {
    target: "#xeroOrg.exchangingToken",
    @@ -63,7 +63,7 @@ const xeroOrgMachine = Machine({
    },
    },
    },
    requestAuthUrlFailure: {
    requestAuthUrl: {
    on: {
    RETRY: {
    target: "#xeroOrg.noOrg"
  9. Larry Botha revised this gist Sep 20, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ const xeroOrgMachine = Machine({

    error: {
    states: {
    exchangeToken: {
    tokenExchangeFailure: {
    on: {
    RETRY: {
    target: "#xeroOrg.exchangingToken",
    @@ -63,7 +63,7 @@ const xeroOrgMachine = Machine({
    },
    },
    },
    requestAuthUrl: {
    requestAuthUrlFailure: {
    on: {
    RETRY: {
    target: "#xeroOrg.noOrg"
  10. Larry Botha revised this gist Sep 20, 2019. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion machine.js
    Original file line number Diff line number Diff line change
    @@ -75,6 +75,16 @@ const xeroOrgMachine = Machine({

    orgFound: {},

    selectOrg: {},
    selectOrg: {
    on: {
    ORG_SELECTED: {
    target: "#xeroOrg.exchangingToken"
    }
    }
    },
    }
    }, {
    guards: {
    hasMultipleOrgs: () => true
    }
    });
  11. Larry Botha created this gist Sep 20, 2019.
    80 changes: 80 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    const xeroOrgMachine = Machine({
    id: 'xeroOrg',

    initial: 'noOrg',

    states: {
    noOrg: {
    invoke: {
    src: "requestAuthUrl",
    onDone: "exchangingToken",
    onError: "error.requestAuthUrl",
    },
    },

    exchangingToken: {
    invoke: {
    src: "exchangeToken",
    onDone: "orgFound",
    onError: [
    {
    cond: "hasMultipleOrgs",
    target: "multipleOrgs",
    },
    {
    target: "error.exchangeToken"
    },
    ],
    },
    },

    multipleOrgs: {
    initial: "fetching",

    invoke: {
    src: "getOrgs",
    onDone: "multipleOrgs.success",
    onError: "#xeroOrg.error.multipleOrgs",
    },

    states: {
    fetching: {},
    success: {
    on: {
    SELECT_ORG: "#xeroOrg.exchangingToken",
    },
    },
    }
    },

    error: {
    states: {
    exchangeToken: {
    on: {
    RETRY: {
    target: "#xeroOrg.exchangingToken",
    },
    },
    },
    multipleOrgs: {
    on: {
    RETRY: {
    target: "#xeroOrg.multipleOrgs"
    },
    },
    },
    requestAuthUrl: {
    on: {
    RETRY: {
    target: "#xeroOrg.noOrg"
    }
    }
    },
    },
    },

    orgFound: {},

    selectOrg: {},
    }
    });