Last active
April 26, 2021 16:38
-
-
Save austinsamsel/059d0f834c6bc662a83ed01ebb99623e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. when user accesses the site, we immediately check for an existing session (amazon cognito), wallet (dapper). | |
// 2. if there's some data for the session or wallet, we'll assign it to context, otherwise it'll be null. | |
// 3. if there's some data for the session, we'll pass along a token to the BE to get the user's profile from the API. if there's no reason to make the request, we can skip it and drive the state forward to IDLE. | |
// 4. from IDLE, we can await user action. not worrying about "checks" for appropriate actions right now, especially since flows are not yet determined. | |
// NOTE: a REDIRECT means user is leaving site and upon returning the machine "starts over" from LOADING_SESSION | |
// docs: https://github.com/davidkpiano/xstate/tree/master/packages/xstate-fsm#example | |
const sessionDefinition = { | |
initial: 'LOADING', | |
context: { | |
session: null, | |
wallet: null, | |
// profile as it is on the DB, not the wallet "profile" | |
profile: null, | |
}, | |
states: { | |
LOADING: { | |
on: { | |
LOADED: { | |
target: 'IDLE', | |
}, | |
}, | |
exit: assign({ | |
session: (context, { data }) => { | |
return data?.session; | |
}, | |
}), | |
}, | |
// for now, rely on context to determine if user is logged in to each | |
// service or not. | |
IDLE: { | |
on: { | |
SIGN_IN_SESSION: { | |
target: 'REDIRECT', | |
actions: ['signIn'], | |
}, | |
CLEAR_SESSION: { | |
target: 'REDIRECT', | |
actions: ['clearSession'], | |
}, | |
WALLET_LOADED: { | |
target: 'IDLE', | |
actions: assign({ | |
wallet: (context, { data }) => { | |
return data?.wallet; | |
}, | |
}), | |
}, | |
SIGN_UP_WALLET: { | |
target: 'AUTH_WALLET', | |
actions: ['fclSignUp'], | |
}, | |
SIGN_IN_WALLET: { | |
target: 'AUTH_WALLET', | |
actions: ['fclSignIn'], | |
}, | |
CLEAR_WALLET: { | |
target: 'IDLE', | |
actions: ['fclClearSession'], | |
}, | |
}, | |
}, | |
// fcl auth pop up: | |
AUTH_WALLET: { | |
on: { | |
WALLET_LOADED: { | |
target: 'IDLE', | |
actions: assign({ | |
wallet: (context, { data }) => { | |
return data?.wallet; | |
}, | |
}), | |
}, | |
}, | |
}, | |
REDIRECT: {}, // redirect happens here | |
}, | |
}; | |
const fetchMachine = Machine(sessionDefinition); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment