Infact there are several things that confuse me with xstate but the one I am frustrated with at this moment is with an invoked service...
How is it possible that the onDone event transitions to the next state BEFORE the service gets a response ???
Example here is the service ...
const postEnrolmentForm = (enrolment: TEnrolment) => {
const progressHandler = ({progress}: AxiosProgressEvent) => {
console.log('Upload Enrolment Form Progress:', progress);
send({ type: 'UPLOAD_PROGRESS', progress: Math.ceil((progress || 0) * 100)}); // progress is between 0 and 1
};
return axios.post(`${baseUrl}/enrol`, enrolment, {
onUploadProgress: (progress) => progressHandler(progress),
}).then(res => {
console.log('Got Back from /enrol : ', res.data);
return res.data
})
};
And here is the invoke....
submitForm: {
invoke: {
id: 'postSubmitForm',
src: 'postEnrolmentForm',
// onDone: 'idle',
onDone: {
target: '#registrationForm.editing.success',
},
onError: {
target: '#registrationForm.error',
actions: ['setAsyncService', 'setErrorMessage'],
},
},
The success target in the submitForm invoke is transitioned WAY before the actual service returns a value