Skip to content

Instantly share code, notes, and snippets.

@JodiWarren
Last active November 1, 2019 16:51
Show Gist options
  • Save JodiWarren/7715e7847aba0915f39c926bd37c2f19 to your computer and use it in GitHub Desktop.
Save JodiWarren/7715e7847aba0915f39c926bd37c2f19 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const initialState = {
selectedFilters: [],
searchedFilters: [],
searchTerm: "",
page: 1,
retries: 0,
};
const fetchMachine = Machine({
id: "peopleFilters",
initial: "closed",
context: Object.assign({}, initialState),
states: {
closed: {
on: {
OPEN: "open",
SEARCH: 'loading'
},
},
open: {
on: {
CLOSE: "closed",
SELECT: {
actions: assign({
selectedFilters: context => [...selectedFilters, context.filter]
})
},
UNSELECT: {
actions: assign({
selectedFilters: context => [...selectedFilters].filter(item => item !== context.filter)
})
},
SEARCH: 'loading'
},
},
loading: {
on: {
SUCCESS: {
target: "closed"
},
FAILURE: {
target: "failure"
}
}
},
failure: {
on: {
RETRY: {
target: "loading",
actions: assign({
retries: (context, event) => context.retries + 1
})
}
}
}
},
on: {
RESET: {
target: 'closed',
actions: assign(Object.assign({}, initialState))
},
VIEW_MORE: {
target: 'loading',
actions: assign({
page: (context, event) => context.page + 1
})
}
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment