Skip to content

Instantly share code, notes, and snippets.

@farhanjiwani
Created March 31, 2020 03:14
Show Gist options
  • Save farhanjiwani/112dbe70b2ea690cfce18dd2df18ba05 to your computer and use it in GitHub Desktop.
Save farhanjiwani/112dbe70b2ea690cfce18dd2df18ba05 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
/* Actions & Guards **********************************************************/
const collapse = assign({
isShown: false,
});
const setCookie = assign({
isCookieSet: true,
});
const hasCookie = context => context.isCookieSet;
const isNotShowing = context => context.isShown === false;
/* end Action & Guards ********************************************************/
/* Possible States ************************************************************/
const stateInitial = {
on: {
PAGE_LOAD: [
{
target: 'messageCollapsed',
cond: 'hasCookie',
},
{
target: 'messageCollapsed',
cond: 'isNotShowing',
},
{
target: 'messageShowing',
},
],
},
};
const stateShowing = {
on: {
USER_CLOSE: {
target: 'messageCollapsed',
actions: ['setCookie', 'collapse'],
},
},
initial: 'unfocused',
states: {
unfocused: {
on: {
FOCUS: 'focused',
},
},
focused: {
on: {
UNFOCUS: 'unfocused',
},
},
},
};
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 || false,
},
states: {
initial: stateInitial,
messageShowing: stateShowing,
messageCollapsed: stateCollapsed,
},
},
{
actions: {
setCookie,
collapse,
},
guards: {
hasCookie,
isNotShowing,
},
}
);
};
PrivacyPolicyMachine(true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment