Created
April 4, 2021 09:03
-
-
Save horacioh/2e007659ff123a8c30178f45e4ddd33f to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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 fetchMachine = Machine({ | |
id: 'editor', | |
initial: 'idle', | |
context: { | |
document: null, | |
errorMessage: null | |
}, | |
states: { | |
idle: { | |
exit: ["clearErrorMessage"], | |
on: { | |
FETCH: 'loading' | |
} | |
}, | |
loading: { | |
on: { | |
LOAD_DRAFT: 'editing' | |
}, | |
invoke: { | |
src: "fetchDraft", | |
onError: { | |
target: "idle", | |
actions: "assignErrorMessageToContext", | |
}, | |
} | |
}, | |
editing: { | |
exit: ["clearErrorMessage"], | |
on: { | |
SAVE: 'saving', | |
PUBLISH: 'publishing' | |
} | |
}, | |
saving: { | |
on: { | |
SUCCESS: 'editing' | |
}, | |
invoke: { | |
src: 'saveDraft', | |
onError: { | |
target: 'editing', | |
actions: 'assignErrorMessageToContext' | |
}, | |
onDone: { | |
target: 'editing' | |
} | |
} | |
}, | |
publishing: { | |
on: { | |
SUCCESS: 'published', | |
ERROR: 'editing' | |
}, | |
invoke: { | |
src: 'publishDraft', | |
onDone: { | |
target: "published", | |
}, | |
onError: { | |
target: 'editing', | |
actions: 'assignErrorMessageToContext' | |
} | |
} | |
}, | |
published: { | |
type: 'final' | |
} | |
} | |
}, { | |
actions: { | |
clearErrorMessage: assign((context) => ({ | |
errorMessage: undefined, | |
})), | |
assignErrorMessageToContext: assign((context, event) => { | |
return { | |
errorMessage: event.data?.message || "An unknown error occurred", | |
}; | |
}), | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment