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.
Generated by XState Viz: https://xstate.js.org/viz
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: {
tokenExchangeFailure: {
on: {
RETRY: {
target: "#xeroOrg.exchangingToken",
},
},
},
multipleOrgs: {
on: {
RETRY: {
target: "#xeroOrg.multipleOrgs"
},
},
},
requestAuthUrlFailure: {
on: {
RETRY: {
target: "#xeroOrg.noOrg"
}
}
},
},
},
orgFound: {},
selectOrg: {
on: {
ORG_SELECTED: {
target: "#xeroOrg.exchangingToken"
}
}
},
}
}, {
guards: {
hasMultipleOrgs: () => true
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment