Skip to content

Instantly share code, notes, and snippets.

@jasonraimondi
Last active May 10, 2021 06:40
Show Gist options
  • Save jasonraimondi/586eb6a6a074ceecd4ac903bbcc502c6 to your computer and use it in GitHub Desktop.
Save jasonraimondi/586eb6a6a074ceecd4ac903bbcc502c6 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const privateViewState = {
initial: "checkAuthorization",
states: {
checkAuthorization: {
on: {
SUCCESS: "showPrivatePage",
FAILURE: "canRefreshToken",
},
},
canRefreshToken: {
on: {
SUCCESS: "attemptTokenRefresh",
FAILURE: "redirectToLogin",
},
},
attemptTokenRefresh: {
on: {
SUCCESS: "checkRoles",
FAILURE: "redirectToLogin",
},
},
checkRoles: {
on: {
SUCCESS: "showPrivatePage",
FAILURE: "showDashboard",
},
},
redirectToLogin: {
type: "final",
},
showDashboard: {
type: "final",
},
showPrivatePage: {
type: "final",
},
},
};
const publicViewState = {
initial: "checkIsLoginPage",
states: {
checkIsLoginPage: {
on: {
SUCCESS: "checkAuthorization",
FAILURE: "showPublicPage",
},
invoke: {
id: 'getUser',
src: (context, event) => console.log("AM I THE LOGIN PAGE?"),
onDone: {
// target: 'checkAuthorization',
actions: assign({ user: (context, event) => event.data })
},
onError: {
// target: 'failure',
actions: assign({ error: (context, event) => event.data })
}
}
},
checkAuthorization: {
on: {
SUCCESS: "canRefreshToken",
FAILURE: "showPublicPage",
},
},
canRefreshToken: {
on: {
SUCCESS: "attemptTokenRefresh",
FAILURE: "showPublicPage",
},
},
attemptTokenRefresh: {
on: {
SUCCESS: "redirectToDashboard",
FAILURE: "showPublicPage",
},
},
showPublicPage: {
type: "final",
},
redirectToDashboard: {
type: "final",
},
},
};
const fetchMachine = Machine({
id: "fetch",
initial: "idle",
states: {
idle: {
on: {
PUBLIC_PAGE: "publicView",
PRIVATE_PAGE: "privateView",
},
},
publicView: {
...publicViewState,
},
privateView: {
...privateViewState,
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment