Created
October 3, 2019 14:31
-
-
Save cogell/24e324463c03f9f92dcd494d6fb3f366 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
const s = { | |
idle: "idle", | |
loading: "loading", | |
loggedIn: "loggedIn", | |
error: "error" | |
}; | |
// event types | |
const e = { | |
login: "login", | |
logout: "logout", | |
success: "success", | |
error: "error" | |
}; | |
// private context | |
const initContext = { | |
name: null, | |
dogPerson: null, | |
catPerson: null | |
}; | |
const machine = Machine({ | |
id: "userMachine", | |
initial: [s.idle], | |
context: initContext, | |
states: { | |
[s.idle]: { | |
on: { | |
login: s.loading | |
} | |
}, | |
[s.loading]: { | |
invoke: { | |
id: "authUser", | |
src: "authUser", | |
onDone: { | |
target: s.loggedIn, | |
actions: assign({ | |
name: (_, e) => e.data.name, | |
dogPerson: (_, e) => e.data.dogPerson, | |
catPerson: (_, e) => e.data.catPerson | |
}) | |
}, | |
onError: { | |
target: e.error, | |
actions: (_, e) => { | |
// log | |
console.log("error", e); | |
} | |
} | |
}, | |
on: { | |
[e.success]: s.loggedIn, | |
[e.error]: s.error | |
} | |
}, | |
[s.loggedIn]: { | |
on: { | |
[e.logout]: s.idle | |
} | |
}, | |
[s.error]: {} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment