Skip to content

Instantly share code, notes, and snippets.

@tmbtech
Created July 31, 2021 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmbtech/55a42d188dfdb09e124c3094e68c0b7d to your computer and use it in GitHub Desktop.
Save tmbtech/55a42d188dfdb09e124c3094e68c0b7d 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)
const fetchMachine = Machine({
id: 'transaction_machine',
context: {
patientId: "",
transactionId:"",
patient: null,
transaction: null,
officePromotions: [],
brands: [],
patientAndBrandsError: '',
treatments: [],
warnings: null,
awards: null,
allerganBrandTreatmentSelected: false,
promotionsEnabled: [],
promotionsPermanentlyEnabled: [],
usePointsEnabled: false,
usePoints: 0,
pointsEarned: 0,
pointsUsed: 0,
agreementAccepted: true,
brandErrors: [],
agreementAcceptedError: false,
serverError: null,
},
initial: 'dataPending',
states: {
dataPending: {
initial: 'loading',
states: {
loading: {
invoke: {
id: 'getPatientProfilePromotionsAndBrands',
src: 'getPatientProfilePromotionsAndBrands',
onDone: {
target: '#dataReady',
actions: 'storePatientAndBrands',
},
onError: {
target: 'error',
actions: 'storePatientAndBrandsError',
},
},
},
error: {
type: 'final',
},
},
},
dataReady: {
id: 'dataReady',
type: 'parallel',
on: {
NEW_PATIENT_ID: {
target: 'dataPending',
actions: 'setPatientId',
},
PROMOTIONS_ENABLED_CHANGED: {
actions: 'setPromotionsEnabled',
},
USE_POINTS_ENABLED_CHANGED: {
actions: 'setUsePointsEnabled',
},
USE_POINTS_CHANGED: {
actions: 'setUsePoints',
},
AGREEMENT_ACCEPTED_CHANGED: {
actions: 'setAgreementAccepted',
},
},
states: {
transaction: {
initial: 'idle',
states: {
idle: {
on: {
CREATE_TRANSACTION: {
target: 'validating',
},
UPDATE_TRANSACTION: {
target: 'validating',
},
},
},
validating: {
invoke: {
id: 'validateFields',
src: 'validateFields',
onDone: {
target: 'validationComplete',
actions: 'storeValidationErrors',
},
},
},
validationComplete: {
always: [
{
target: 'validationError',
cond: 'validationErrorsPresent',
},
{
target: 'create',
cond: 'isCreateTransaction',
},
{
target: 'update',
cond: 'isUpdateTransaction',
},
],
},
validationError: {
on: {
CREATE_TRANSACTION: {
target: 'validating',
},
UPDATE_TRANSACTION: {
target: 'validating',
},
},
entry: 'onValidationError',
},
create: {
invoke: {
id: 'createTransaction',
src: 'createTransaction',
onDone: {
target: '#success',
actions: 'onSuccess',
},
onError: {
target: 'error',
actions: 'onError',
},
},
},
update: {
invoke: {
id: 'updateTransaction',
src: 'updateTransaction',
onDone: {
target: '#success',
actions: 'onSuccess',
},
onError: {
target: 'error',
actions: 'onError',
},
},
},
error: {
on: {
CREATE_TRANSACTION: {
target: 'validating',
},
UPDATE_TRANSACTION: {
target: 'validating',
},
},
},
},
},
checkoutSummary: {
initial: 'idle',
states: {
idle: {
on: {
TREATMENTS_CHANGED: {
target: 'loading',
actions: ['setTreatments', 'onTreatmentsChanged'],
},
},
},
refetch: {
entry: send('TRANSACTION_SUMMARY_FETCH'),
on: {
TRANSACTION_SUMMARY_FETCH: 'loading',
},
},
loading: {
on: {
UPDATE: {
actions: [
'storePointsEarned',
'setWarnings',
'setAwards',
'updatePromotionsEnabled',
],
},
TREATMENTS_CHANGED: {
target: 'refetch',
actions: ['setTreatments', 'onTreatmentsChanged'],
},
},
invoke: {
id: 'patientTransactionSummary',
src: 'patientTransactionSummary',
onDone: 'idle',
},
},
},
},
},
},
success: {
id: 'success',
type: 'final',
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment