Skip to content

Instantly share code, notes, and snippets.

@tmikeschu
Last active December 22, 2019 04:57
Show Gist options
  • Save tmikeschu/fcad6db973e52ca4c279eaf5a24ba7e4 to your computer and use it in GitHub Desktop.
Save tmikeschu/fcad6db973e52ca4c279eaf5a24ba7e4 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const BOOK_OR_REVIEW = { BOOK: '#schedule', DATES: '#dates' };
const routesMachine = Machine(
{
id: 'routes',
initial: 'codeOfConduct',
context: {
lastScreen: '',
},
states: {
codeOfConduct: {
on: {
'': { target: 'schedule', cond: 'hasAcceptedCodeOfConduct' },
},
},
schedule: {
id: 'schedule',
entry: 'setLastToSchedule',
on: {
DATES: 'dates.hist',
SCHEDULE: 'schedule',
SETTINGS: 'settings',
},
initial: 'home',
states: {
home: { on: { FRIEND: 'friend', SOMEONE_NEW: 'someoneNew' } },
friend: {
initial: 'person',
states: {
person: { on: { TIME: 'time' } },
time: { on: { SUBMIT: 'reserved' } },
reserved: { on: BOOK_OR_REVIEW },
},
},
someoneNew: {
initial: 'person',
states: {
person: { on: { TIMES: 'times' } },
times: { on: { INTERESTS: 'interests' } },
interests: { on: { RESERVE: 'reserved' } },
reserved: { on: BOOK_OR_REVIEW },
},
},
hist: { type: 'history', history: 'deep' },
},
},
dates: {
id: 'dates',
entry: 'setLastToDates',
on: {
SCHEDULE: 'schedule.hist',
DATES: 'dates',
SETTINGS: 'settings',
},
initial: 'home',
states: {
home: { on: { DECLINE: 'decline', CANCEL: 'cancel' } },
hist: { type: 'history', history: 'deep' },
decline: {
initial: 'confirm',
states: {
confirm: { on: { DECLINE: 'reason', CANCEL: '#dates' } },
reason: {
on: {
NOT_AVAILABLE: 'notAvailable',
NOT_INTERESTED: 'notInterested',
},
},
notAvailable: { on: BOOK_OR_REVIEW },
notInterested: { on: BOOK_OR_REVIEW },
},
},
cancel: {
initial: 'confirm',
states: {
confirm: { on: { DECLINE: 'declined', CANCEL: '#dates' } },
declined: { on: { BOOK: '#schedule', DATES: '#dates' } },
},
},
},
},
settings: {
on: {
CLOSE: [
{
target: 'schedule.hist',
cond: 'wasSchedule',
},
{
target: 'dates.hist',
cond: 'wasDates',
},
{ target: 'schedule' },
],
},
},
},
},
{
guards: {
hasAcceptedCodeOfConduct: () => {
// TODO: localStorage check
return true;
},
wasSchedule: ({ lastScreen }) => lastScreen === 'schedule',
wasDates: ({ lastScreen }) => lastScreen === 'dates',
},
actions: {
setLastToSchedule: assign({
lastScreen: 'schedule',
}),
setLastToDates: assign({
lastScreen: 'dates',
}),
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment