Skip to content

Instantly share code, notes, and snippets.

@xavhan
Last active October 15, 2020 14:40
Show Gist options
  • Save xavhan/325b74112962b706348034a751003138 to your computer and use it in GitHub Desktop.
Save xavhan/325b74112962b706348034a751003138 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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