Created
June 18, 2021 20:27
-
-
Save kbingman/1e1cfdf6bacd9ef2ebc1b1faa3263bd3 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 UPDATE_EVENT = { | |
UPDATE_QUERY: { | |
target: 'selected', | |
actions: assign((_, evt) => ({ | |
q: evt.q || '', | |
})), | |
}, | |
}; | |
const RESET_EVENT = { | |
RESET: { | |
target: 'idle', | |
}, | |
}; | |
const LOAD_EVENT = { | |
LOAD: { | |
target: 'success', | |
actions: assign((_, evt) => ({ | |
q: evt.q || '', | |
results: evt.results || [], | |
error: null, | |
})), | |
}, | |
}; | |
const ERROR_EVENT = { | |
ERROR: { | |
target: 'failure', | |
actions: assign((_, evt) => ({ | |
error: evt.message, | |
})), | |
}, | |
}; | |
const fetchMachine = Machine( | |
{ | |
id: 'fetch-machine', | |
initial: 'idle', | |
context: { | |
q: '', | |
offset: 0, | |
results: [], | |
filters: {}, | |
error: '', | |
}, | |
states: { | |
idle: { | |
on: { | |
...UPDATE_EVENT, | |
...LOAD_EVENT, | |
...ERROR_EVENT, | |
}, | |
}, | |
selected: { | |
on: { | |
FETCH: 'pending', | |
...UPDATE_EVENT, | |
}, | |
}, | |
pending: { | |
on: { | |
...LOAD_EVENT, | |
...ERROR_EVENT, | |
}, | |
}, | |
success: { | |
on: { | |
...UPDATE_EVENT, | |
...RESET_EVENT, | |
}, | |
}, | |
failure: { | |
on: { | |
...UPDATE_EVENT, | |
...RESET_EVENT, | |
}, | |
}, | |
}, | |
}, | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment