Last active
October 20, 2020 13:34
-
-
Save lifedraft/96d9f4e9d33b88c99a11b2814b56025f 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 fetchMachine = Machine({ | |
id: 'fetch', | |
initial: 'idle', | |
context: { | |
loading: false, | |
errors: false, | |
searchTerm: '', | |
searchResults: null | |
}, | |
states: { | |
idle: { | |
on: { | |
ACTIVATE: 'active' | |
} | |
}, | |
active: { | |
initial: 'idle', | |
states: { | |
idle: {}, | |
fetchLiveSearchResults: { | |
entry: assign({ | |
loading: (_, _event) => true, | |
errors: (_, _event) => false, | |
}), | |
invoke: 'fetchLiveSearchResults', | |
onDone: { | |
target: 'results', | |
actions: [ | |
assign({ | |
loading: (_, _event) => false, | |
errors: (_, _event) => false, | |
}) | |
] | |
}, | |
}, | |
results: {} | |
}, | |
on: { | |
CLEAR_SEARCH_TERM: { | |
target: '.idle', | |
actions: assign({ | |
searchTerm: (_, event) => "" | |
}), | |
} | |
} | |
}, | |
}, | |
on: { | |
SEARCH: [ | |
{ | |
target: '.active.fetchLiveSearchResults', | |
actions: assign({ | |
searchTerm: (_, event) => event.data | |
}), | |
cond: {type: 'searchValid'} | |
}, | |
{ | |
target: '.active.idle', | |
actions: assign({ | |
searchTerm: (_, event) => event.data | |
}) | |
} | |
] | |
} | |
}, { | |
guards: { | |
searchValid: (context, event) => { | |
return context.searchTerm.length > 3 | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment