Created
March 25, 2020 05:53
-
-
Save jacobparis/4265381c7de1c9de2077cc22f52a8284 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const THREE_DAYS_FROM_NOW = new Date().getTime() + 3 * 24 * 3600 * 1000 | |
const formMachine = Machine({ | |
id: 'form', | |
initial: 'editing', | |
context: { | |
retries: 0, | |
name: '', | |
email: '', | |
budget: '', | |
deadline: new Date('2022'), | |
message: '' | |
}, | |
states: { | |
editing: { | |
on: { | |
SUBMIT: [ | |
{ | |
target: 'editing.name.error.empty', | |
cond: 'isNameEmpty' | |
}, | |
{ | |
target: 'editing.email.error.empty', | |
cond: 'isEmailEmpty' | |
}, | |
{ | |
target: 'editing.email.error.badFormat', | |
cond: 'isEmailBadFormat' | |
}, | |
{ | |
target: 'editing.budget.error.empty', | |
cond: 'isBudgetEmpty' | |
}, | |
{ | |
target: 'editing.budget.error.badFormat', | |
cond: 'isBudgetBadFormat' | |
}, | |
{ | |
target: 'editing.budget.error.tooLow', | |
cond: 'isBudgetTooLow' | |
}, | |
{ | |
target: 'editing.deadline.error.empty', | |
cond: 'isDeadlineEmpty' | |
}, | |
{ | |
target: 'editing.deadline.error.tooEarly', | |
cond: 'isDeadlineTooEarly' | |
}, | |
{ | |
target: 'editing.message.error.empty', | |
cond: 'isMessageEmpty' | |
}, | |
{ | |
target: 'editing.message.error.tooShort', | |
cond: 'isMessageTooShort' | |
}, | |
{ | |
target: 'submitting' | |
} | |
], | |
INPUT: { | |
target: 'editing', | |
actions: 'cache' | |
} | |
}, | |
type: 'parallel', | |
states: { | |
name: { | |
initial: 'valid', | |
states: { | |
valid: {}, | |
error: { | |
initial: 'empty', | |
states: { | |
empty: {} | |
} | |
} | |
} | |
}, | |
email: { | |
initial: 'valid', | |
states: { | |
valid: {}, | |
error: { | |
initial: 'empty', | |
states: { | |
empty: {}, | |
badFormat: {} | |
} | |
} | |
} | |
}, | |
budget: { | |
initial: 'valid', | |
states: { | |
valid: {}, | |
error: { | |
initial: 'empty', | |
states: { | |
empty: {}, | |
badFormat: {}, | |
tooLow: {} | |
} | |
} | |
} | |
}, | |
deadline: { | |
initial: 'valid', | |
states: { | |
valid: {}, | |
error: { | |
initial: 'empty', | |
states: { | |
empty: {}, | |
tooEarly: {} | |
} | |
} | |
} | |
}, | |
message: { | |
initial: 'valid', | |
states: { | |
valid: {}, | |
error: { | |
initial: 'empty', | |
states: { | |
empty: {}, | |
tooShort: {} | |
} | |
} | |
} | |
} | |
} | |
}, | |
submitting: { | |
on: { | |
RESOLVE: 'success', | |
REJECT: 'failure' | |
} | |
}, | |
success: { | |
type: 'final' | |
}, | |
failure: { | |
on: { | |
RETRY: { | |
target: 'submitting', | |
actions: 'incrementRetry' | |
} | |
} | |
} | |
} | |
}, { | |
actions: { | |
cache: assign((context, event) => event), | |
incrementRetry: assign((context, event) => context.retries + 1) | |
}, | |
guards: { | |
isNameEmpty: context => context.name.length === 0, | |
isEmailEmpty: context => context.email.length === 0, | |
isEmailBadFormat: context => !context.email.includes('@'), | |
isBudgetEmpty: context => context.budget.length === 0, | |
isBudgetBadFormat: context => !context.budget.match(/^[0-9$]\d*(((,\d{3}){1})?(\.\d{0,2})?)$/), | |
isBudgetTooLow: context => parseFloat(context.budget) < 1000, | |
isDeadlineEmpty: context => !context.deadline, | |
isDeadlineTooEarly: context => context.deadline <= THREE_DAYS_FROM_NOW, | |
isMessageEmpty: context => context.message.length === 0, | |
isMessageTooShort: context => context.message.length < 10 | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment