Created
July 30, 2020 13:33
-
-
Save gustavo-depaula/ea061b1fd745c26aa285cb3ce9631918 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
const ACTIONS = { | |
CHOOSE_PROBLEM: 'CHOOSE_PROBLEM', | |
CONTINUE: 'CONTINUE', | |
CHANGE_BARCODE_ID: 'CHANGE_BARCODE_ID', | |
CLEAN_NOTIFICATION: 'CLEAN_NOTIFICATION', | |
BACK: 'BACK', | |
CONFIRM_PROBLEM: 'CONFIRM_PROBLEM', | |
SKIP: 'SKIP', | |
UPDATE_STATUS: 'UPDATE_STATUS' | |
}; | |
const STATES = { | |
selection: 'selection', | |
loading: 'loading', | |
successStep: 'successStep', | |
offlineStep: 'offlineStep', | |
redirectStep: 'redirect', | |
detailsStep: 'details' | |
}; | |
const deliveringProblemsMachine = Machine( | |
{ | |
id: 'deliveringProblems', | |
context: { | |
packageId: null, | |
problem: null, | |
notificationMessage: null | |
}, | |
initial: STATES.selection, | |
states: { | |
[STATES.selection]: { | |
on: { | |
BACK: { | |
actions: ['goBack'] | |
}, | |
[ACTIONS.CHOOSE_PROBLEM]: { | |
target: STATES.detailsStep, | |
actions: assign({ | |
problem: (_, event) => event | |
}) | |
} | |
} | |
}, | |
[STATES.detailsStep]: { | |
on: { | |
BACK: STATES.selection, | |
SKIP: STATES.successStep, | |
UPDATE_STATUS: STATES.loading, | |
[ACTIONS.CONFIRM_PROBLEM]: { | |
actions: 'skipUpdateStatusIfInvalidId' | |
} | |
} | |
}, | |
[STATES.loading]: { | |
invoke: { | |
id: 'update status', | |
src: context => | |
getPosition() | |
.then(({ latitude, longitude }) => | |
updateStatus({ | |
latitude, | |
longitude, | |
receiverName: '', | |
receiverDocument: '', | |
status: context.problem.id, | |
process: context.problem.process, | |
destination: context.problem.destination, | |
packageId: context.packageId | |
}) | |
) | |
.catch(error => { | |
if (!error.response) { | |
return { | |
offline: true | |
}; | |
} | |
throw error; | |
}), | |
onDone: { | |
target: STATES.successStep | |
}, | |
onError: { | |
target: STATES.selection, | |
actions: assign({ | |
notificationMessage: (_, event) => | |
event.data.message || | |
messages.notifications.defaultUpdateStatusFailureText | |
}) | |
} | |
} | |
}, | |
[STATES.successStep]: { | |
on: { | |
[ACTIONS.CONTINUE]: STATES.redirectStep | |
} | |
}, | |
[STATES.redirectStep]: {} | |
}, | |
on: { | |
[ACTIONS.CLEAN_NOTIFICATION]: { | |
actions: assign({ | |
notificationMessage: '' | |
}) | |
} | |
} | |
}, | |
{ | |
actions: { | |
goBack: () => { | |
throw new Error( | |
'this must be passed down from whom is instantiating the machine' | |
); | |
}, | |
skipUpdateStatusIfInvalidId: send(context => { | |
// this is used for "presentational" problems | |
// presentational problems are problems that appear on the | |
// delivering problems list, but do not do an update status op | |
const invalidProblemId = '-1'; | |
const { id } = context.problem; | |
const eventType = | |
id === invalidProblemId ? ACTIONS.SKIP : ACTIONS.UPDATE_STATUS; | |
const event = { | |
type: eventType | |
}; | |
return event; | |
}) | |
} | |
} | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment