Skip to content

Instantly share code, notes, and snippets.

@xavhan
Last active October 15, 2020 15:21
Show Gist options
  • Save xavhan/789c6895b098ad56a673713a135a5d0a to your computer and use it in GitHub Desktop.
Save xavhan/789c6895b098ad56a673713a135a5d0a 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: {
'': {
target: 'active',
cond: 'isActivated'
},
}
},
active: {
on: {
'': {
target: 'ended',
cond: 'isEnded'
},
},
},
ended: {},
},
};
const campaignMachine = Machine({
id: 'Campaign State',
initial: 'draft',
context: {
campaign: {
// you can play with those dates
// activation_date: '2020-10-14',
// deactivation_date: '2020-10-16',
}
},
states: {
draft: {
on: {
SHARE: 'shared'
}
},
shared: {
...sharedStates,
on: {
DISABLE: 'disabled',
}
},
disabled: {
on: {
ENABLE: 'shared',
DELETE: 'deleted',
}
},
deleted: {
type: 'final',
},
},
}, {
guards: {
isActivated,
isEnded,
}
});
function isActivated(c, e) {
if (c.campaign.activation_date) {
const now = new Date();
return new Date(c.campaign.activation_date) < now;
}
return true;
}
function isEnded(c, e) {
if(c.campaign.deactivation_date) {
const now = new Date();
return new Date(c.campaign.deactivation_date) < now;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment