Last active
October 15, 2020 14:40
-
-
Save xavhan/325b74112962b706348034a751003138 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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
const sharedStates = { | |
initial: 'pending', | |
states: { | |
pending: { | |
on: { | |
ACTIVATE: 'active', | |
} | |
}, | |
active: { | |
on: { | |
DEACTIVATE: 'ended', | |
} | |
}, | |
ended: {}, | |
}, | |
}; | |
function isActivated(c, e) { | |
if(c.campaigns.activation_date && c.campaigns.deactivation_date) { | |
return new Date(c.campaigns.activation_date) < new Date() && new Date() < new Date(c.campaigns.deactivation_date); | |
} | |
if (c.campaigns.activation_date) { | |
return new Date(c.campaigns.activation_date) < new Date(); | |
} | |
if (c.campaigns.deactivation_date) { | |
return new Date() < new Date(c.campaigns.deactivation_date); | |
} | |
return true; | |
} | |
function isEnded(c, e) { | |
if(c.campaigns.deactivation_date) { | |
return new Date(c.campaigns.deactivation_date) < new Date(); | |
} | |
return false; | |
} | |
const campaignMachine = Machine({ | |
initial: 'draft', | |
context: { | |
campaign: { | |
activation_date: '2020-10-15', | |
} | |
}, | |
states: { | |
draft: { | |
on: { | |
SHARE: [ | |
{ | |
target: 'shared.ended', | |
cond: 'isEnded' | |
}, | |
{ | |
target: 'shared.active', | |
cond: 'isActivated' | |
}, | |
{ | |
target: 'shared.pending', | |
}, | |
] | |
} | |
}, | |
shared: { | |
...sharedStates, | |
on: { | |
DISABLE: 'disabled', | |
} | |
}, | |
disabled: { | |
on: { | |
ENABLE: 'shared', | |
DELETE: 'deleted', | |
} | |
}, | |
deleted: { | |
type: 'final', | |
}, | |
}, | |
}, { | |
guards: { | |
isActivated, | |
isEnded, | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment