Skip to content

Instantly share code, notes, and snippets.

@martypenner
Last active December 3, 2019 20:55
Show Gist options
  • Save martypenner/2cf1aebfefc37c40a535c6a9ea1eeb26 to your computer and use it in GitHub Desktop.
Save martypenner/2cf1aebfefc37c40a535c6a9ea1eeb26 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const taxSettingsMachine = Machine(
{
id: 'taxSettings',
type: 'parallel',
context: {
eventInfo: {
country: '',
status: null,
},
isMarketplaceState: false,
is501c3Eligible: false,
isRunBy501c3Organization: false,
},
states: {
marketplaceStatus: {
initial: 'unknown',
states: {
unknown: {
on: {
'': [
{
target: 'addressIsInUnitedStates',
cond: 'addressIsInUnitedStates',
},
{ target: 'addressIsNotInUnitedStates' },
],
},
},
addressIsInUnitedStates: {
initial: 'unknown',
states: {
unknown: {
on: {
'': [
{
target: 'isMarketplaceState',
cond: 'isMarketplaceState',
},
{ target: 'isNotMarketplaceState' },
],
},
},
isMarketplaceState: {
type: 'parallel',
states: {
addressChangeability: {
initial: 'unknown',
states: {
unknown: {
on: {
'': [
{
target: 'canBeChanged',
cond: 'addressCanBeChanged',
},
{ target: 'canNotBeChanged' },
],
},
},
canBeChanged: {
initial: 'addressValidity',
states: {
addressValidity: {
initial: 'validating',
states: {
validating: {
invoke: {
src: 'validateAddress',
onDone: [
{
target: 'isValid',
cond: 'isAddressValid',
},
{ target: 'isNotValid' },
],
onError: 'failedToValidate',
},
},
failedToValidate: {},
isValid: {},
isNotValid: {},
},
on: {
CHANGE_ADDRESS: '.validating',
},
},
},
},
canNotBeChanged: {},
},
},
'501(c)3 eligibility': {
initial: 'unknown',
states: {
unknown: {
on: {
'': [{ target: 'isEligible', cond: 'is501c3Eligible' }, { target: 'isNotEligible' }],
},
},
isEligible: {
initial: 'unknown',
states: {
unknown: {
on: {
'': [
{
target: 'is run by a 501(c)3 organization',
cond: 'isRunBy501c3Organization',
},
{
target: 'is not run by a 501(c)3 organization',
},
],
},
},
'is run by a 501(c)3 organization': {
// todo: fill this out with each US state's questions and whether they're locked down
initial: 'answersNotValid',
states: {
answersValid: {},
answersNotValid: {},
},
on: {
TOGGLE_RUN_BY_501C3_ORG: 'is not run by a 501(c)3 organization',
},
},
'is not run by a 501(c)3 organization': {
on: {
TOGGLE_RUN_BY_501C3_ORG: 'is run by a 501(c)3 organization',
},
},
},
},
isNotEligible: {},
},
},
},
},
isNotMarketplaceState: {},
},
},
addressIsNotInUnitedStates: {},
},
},
subEvents: {},
products: {},
additionalCharges: {},
},
},
{
guards: {
isAddressValid: (ctx, event) => false, // todo: fill this in
addressCanBeChanged: (ctx) => ctx.eventInfo.status !== 'live',
addressIsInUnitedStates: (ctx) => ctx.eventInfo.country.toLowerCase() === 'us',
isMarketplaceState: (ctx) => ctx.isMarketplaceState,
is501c3Eligible: (ctx) => ctx.is501c3Eligible,
isRunBy501c3Organization: (ctx) => ctx.isRunBy501c3Organization,
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment