Skip to content

Instantly share code, notes, and snippets.

@austinsamsel
Last active July 9, 2021 14:46
Show Gist options
  • Save austinsamsel/ca04d3b9dfb1370a98c8ddce80250d3e to your computer and use it in GitHub Desktop.
Save austinsamsel/ca04d3b9dfb1370a98c8ddce80250d3e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const FS = {
IDLE: 'IDLE',
LOADING: 'LOADING',
AUTHENTICATED: 'AUTHENTICATED',
NOT_AUTHED: 'NOT_AUTHED',
AUTH_PROCESSING: 'AUTH_PROCESSING',
REDIRECT: 'REDIRECT',
ERROR: 'ERROR',
PATCHING_PROFILE: 'PATCHING_PROFILE',
};
const EVENTS = {
SIGN_IN: 'SIGN_IN',
SIGN_OUT: 'SIGN_OUT',
UPDATE: 'UPDATE',
SIGN_UP_WALLET: 'SIGN_UP_WALLET',
SIGN_IN_WALLET: 'SIGN_IN_WALLET',
SIGN_OUT_WALLET: 'SIGN_OUT_WALLET',
}
const definition = {
id: 'session',
type: 'parallel',
context: {
FS,
EVENTS,
auth: undefined,
wallet: undefined,
profile: undefined,
client: undefined,
},
states: {
auth: {
initial: FS.LOADING,
states: {
[FS.LOADING]: {
invoke: {
src: 'getSession',
onDone: [
{
target: FS.AUTHENTICATED,
actions: 'assignAuth',
cond: 'isAuthenticated',
},
{
target: FS.NOT_AUTHED,
},
],
onError: {
target: FS.NOT_AUTHED,
},
},
},
[FS.REDIRECT]: {
invoke: {
src: 'authRedirect',
},
},
[FS.AUTHENTICATED]: {
initial: FS.PATCHING_PROFILE,
states: {
[FS.PATCHING_PROFILE]: {
invoke: {
src: 'patchUserProfile',
onDone: [
{
target: FS.IDLE,
actions: 'assignProfile',
},
],
onError: {
target: FS.ERROR,
},
},
},
[FS.IDLE]: {},
[FS.ERROR]: {},
},
},
[FS.NOT_AUTHED]: {},
},
on: {
[EVENTS.SIGN_IN]: {
target: `auth.${FS.REDIRECT}`,
},
[EVENTS.SIGN_OUT]: {
target: `auth.${FS.REDIRECT}`,
},
},
},
wallet: {
initial: FS.LOADING,
states: {
[FS.LOADING]: {},
[FS.AUTH_PROCESSING]: {
invoke: {
src: 'fclAuth',
},
},
[FS.AUTHENTICATED]: {
initial: FS.PATCHING_PROFILE,
entry: 'assignWallet',
states: {
[FS.PATCHING_PROFILE]: {
invoke: {
src: 'patchUserProfile',
onDone: [
{
target: FS.IDLE,
actions: 'assignProfile',
},
],
onError: {
target: FS.ERROR,
},
},
},
[FS.IDLE]: {},
[FS.ERROR]: {},
},
},
[FS.NOT_AUTHED]: {
entry: 'clearWallet',
},
[FS.ERROR]: {},
},
on: {
[EVENTS.UPDATE]: [
{
target: `wallet.${FS.AUTHENTICATED}`,
cond: 'hasWallet',
},
{
target: `wallet.${FS.NOT_AUTHED}`,
},
],
[EVENTS.SIGN_UP_WALLET]: {
target: `wallet.${FS.AUTH_PROCESSING}`,
},
[EVENTS.SIGN_IN_WALLET]: {
target: `wallet.${FS.AUTH_PROCESSING}`,
},
[EVENTS.SIGN_OUT_WALLET]: {
target: `wallet.${FS.AUTH_PROCESSING}`,
},
},
},
},
};
const services = {};
const guards = {
isAuthenticated: () => true,
hasWallet: () => true,
};
const machine = Machine(definition,
{
actions,
services,
guards,
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment