Skip to content

Instantly share code, notes, and snippets.

@antonkuzmin
Last active June 30, 2020 09:52
Show Gist options
  • Select an option

  • Save antonkuzmin/c7c15be72a810ef5a198433caccc6033 to your computer and use it in GitHub Desktop.

Select an option

Save antonkuzmin/c7c15be72a810ef5a198433caccc6033 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
// VERSION 2 //
const user = {
roles: [
'smi-admin',
],
};
const hasRole = (role) => (ctx, e) =>
user.roles.includes(role);
const flighcost2Machine = Machine({
id: 'flighcost',
initial: 'создано',
context: {
doc: false,
afd_id: null,
afd_status: null,
excess: null, // сумма / пож
resolution: 'не акцептовано',
},
states: {
'создано': {
on: {
'удалить': 'удалено',
'взять в работу': 'в работе',
}
},
'удалено': {
type: 'final'
},
'аннулировано': {
type: 'final',
},
'в работе': { // сверка
on: {
'аннулировать': 'аннулировано',
'редактировать': '.',
'акцептовать все строки': {
target: '.',
actions: assign({
resolution: (ctx,e) => 'акцептовано все',
}),
},
'акцептовать часть строк': {
target: '.',
actions: assign({
resolution: (ctx,e) => 'акцептовано частично',
}),
},
'завершить сверку':{
target: 'сверено',
},
}
},
'сверено': {
on: {
'аннулировать': 'аннулировано',
'вернуть в работу': 'в работе',
'вложить перв.док.': {
target: '.',
actions: assign({
doc: (ctx,e) => true,
}),
},
'отправить в АФД': [
{
target: 'согласование АФД',
cond: 'hasDoc',
actions: assign({
afd_id: () => 1,
afd_status: (ctx) => 'to_approve',
}),
},
],
}
},
'согласование АФД': {
on: {
'аннулировать': {
target: 'аннулировано',
actions: assign({
afd_status: (ctx,e) => 'cancelled',
}),
},
'[АФД] согласовано': {
target: 'обработано АФД',
actions: assign({
afd_status: (ctx,e) => 'approved',
}),
},
'': { // согласование не требовалось
target: 'обработано АФД',
cond: 'isAcceptedAll',
actions: assign({
afd_status: (ctx,e) => 'done',
}),
},
}
},
'обработано АФД': {
on: {
'завершить': 'завершено',
'аннулировать': {
target: 'аннулировано',
actions: assign({
afd_status: (ctx,e) => 'cancelled',
}),
},
}
},
'завершено': {
type: 'final',
on: {
'вернуть в работу': {
target: 'в работе',
cond: 'isAdmin',
},
},
},
}
},
{
guards: {
hasDoc: (ctx, e) => ctx.doc,
hasExcess: (ctx, e) => ctx.excess !== null,
isAdmin: hasRole('smi-admin'),
isAcceptedAll: (ctx, e) => ctx.doc && ctx.resolution === 'акцептовано все',
isAcceptedPartial: (ctx, e) => ctx.doc && ctx.resolution && ctx.resolution !== 'акцептовано все',
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment