Created
March 31, 2020 03:14
-
-
Save farhanjiwani/112dbe70b2ea690cfce18dd2df18ba05 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 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