Last active
April 3, 2020 16:10
-
-
Save farhanjiwani/f69d6484949e8789e0318bd3420b34c3 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
/* Actions & Guards **********************************************************/ | |
const hasCookie = context => context.isCookieSet; | |
const isNotShowing = context => context.isShown === false; | |
const collapse = assign({ | |
isShown: false, | |
}); | |
const setCookie = assign({ | |
isCookieSet: true, | |
}); | |
/* end Action & Guards ********************************************************/ | |
/* Possible States ************************************************************/ | |
const statesForFocus = { | |
// parallel | |
unfocused: { | |
on: { | |
FOCUS: 'focused', | |
}, | |
}, | |
focused: { | |
on: { | |
UNFOCUS: 'unfocused', | |
}, | |
}, | |
}; | |
const stateInitial = { | |
after: { | |
PAGE_LOAD: [ | |
{ | |
target: 'messageCollapsed', | |
cond: 'hasCookie', | |
}, | |
{ | |
target: 'messageCollapsed', | |
cond: 'isNotShowing', | |
}, | |
{ | |
target: 'messageShowing', | |
}, | |
], | |
}, | |
}; | |
const stateShowing = { | |
on: { | |
USER_CLOSE: { | |
target: 'messageCollapsed', | |
actions: ['setCookie', 'collapse'], | |
}, | |
PAGE_REFRESH: 'initial', | |
}, | |
initial: 'unfocused', | |
states: { | |
...statesForFocus, | |
}, | |
}; | |
const stateCollapsed = { | |
on: { | |
PAGE_REFRESH: 'initial', | |
}, | |
}; | |
/* end Possible States */ | |
const PrivacyPolicyMachine = isShown => { | |
return Machine( | |
{ | |
id: 'Privacy Policy Machine', | |
strict: true, | |
initial: 'initial', | |
context: { | |
isCookieSet: false, | |
isShown: isShown, | |
}, | |
states: { | |
initial: stateInitial, | |
messageShowing: stateShowing, | |
messageCollapsed: stateCollapsed, | |
}, | |
}, | |
{ | |
actions: { | |
setCookie, | |
collapse, | |
}, | |
guards: { | |
hasCookie, | |
isNotShowing, | |
}, | |
delays: { | |
PAGE_LOAD: 1000, | |
}, | |
} | |
); | |
}; | |
// uncomment the next line if pasting into the Viz | |
PrivacyPolicyMachine(true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment