Skip to content

Instantly share code, notes, and snippets.

@chrisdhanaraj
Last active July 16, 2019 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdhanaraj/e6a52598aade491affc02010caf938d5 to your computer and use it in GitHub Desktop.
Save chrisdhanaraj/e6a52598aade491affc02010caf938d5 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 suggestionMachine = Machine({
id: "suggestion",
initial: "idle",
context: {
suggestions: []
},
states: {
idle: {},
fetching: {},
successWithResults: {
on: {
ADD_ITEM: {
target: 'idle',
action: sendParent(context => {
return {
type: 'ADD_ITEM',
data: context
}
})
}
}
},
successNoResults: {},
failure: {}
}
});
const facetMachine = Machine({
type: "parallel",
id: "facet",
initial: "fetching",
context: {
facets: {}
},
states: {
idle: {
on: {
FETCH: "fetching"
}
},
fetching: {
on: {
SUCCESS: "success",
FAILURE: "failure"
}
},
success: {
on: {
FETCH: 'fetching'
}
},
failure: {
on: {
FETCH: 'fetching'
}
}
}
});
const searchResultsMachine = Machine({
type: "parallel",
id: "searchResults",
initial: "fetching",
context: {
searchResults: [],
page: 0,
pagePer: 25,
sortKey: null,
sortDir: 'desc'
},
states: {
idle: {
on: {
FETCH: "fetching"
}
},
fetching: {
on: {
SUCCESS: "success",
FAILURE: "failure"
}
},
success: {
on: {
FETCH: "fetching"
}
},
failure: {}
}
});
const searchMachine = Machine({
id: "search",
initial: "idle",
context: {
selectedItems: new Map()
},
states: {
idle: {
entry: assign({
suggestionRef: () => spawn(suggestionMachine, 'suggestions')
}),
on: {
ADD_ITEM: "active",
SET_ITEM: "active"
}
},
active: {
entry: assign({
facetRef: () => spawn(facetMachine, 'facet'),
searchResultsRef: () => spawn(searchResultsMachine, 'searchResults')
}),
on: {
ADD_ITEM: {
target: "active",
actions: [
send("FETCH", {
to: context => {
console.log(context);
return context.facetRef
}
}),
send("FETCH", {
to: context => context.facetRef
})
]
},
SET_ITEM: "active",
REMOVE_ITEM: "active"
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment