Skip to content

Instantly share code, notes, and snippets.

@RafalFilipek
Last active June 2, 2020 11:01
Show Gist options
  • Save RafalFilipek/e432e6d8c53bdcc93afea8ae0e2f8481 to your computer and use it in GitHub Desktop.
Save RafalFilipek/e432e6d8c53bdcc93afea8ae0e2f8481 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const Options = {
actions: {
setContent: "setContent",
setGroups: "setGroups",
setSelectedType: "setSelectedType",
setOnlyType: "setOnlyType",
setSelectedUpsellOffer: "setSelectedUpsellOffer"
},
services: {
loadData: "loadData",
loadContent: "loadContent"
},
guards: {
hasOnlyOneType: "hasOnlyOneType"
}
};
const m = Machine({
strict: true,
context: {
content: {
title: ""
},
types: [],
selectedOfferType: undefined,
upsell: null,
selectedUpsellOffer: null
},
id: "offerTypesMachine",
initial: "loading",
states: {
loading: {
type: "parallel",
onDone: {
target: "ready"
},
states: {
data: {
initial: "loading",
states: {
loading: {
invoke: {
src: Options.services.loadData,
onDone: {
target: "ready",
actions: Options.actions.setGroups
}
}
},
ready: {
type: "final"
}
}
},
content: {
initial: "loading",
states: {
loading: {
invoke: {
src: Options.services.loadContent,
onDone: {
target: "ready",
actions: Options.actions.setContent
}
}
},
ready: {
type: "final"
}
}
},
debounce: {
initial: "waiting",
states: {
waiting: {
after: {
4000: {
target: "ready"
}
}
},
ready: {
type: "final"
}
}
}
}
},
ready: {
on: {
"": {
actions: Options.actions.setOnlyType,
cond: Options.guards.hasOnlyOneType,
target: "success"
},
SELECT_OFFER_TYPE: {
actions: Options.actions.setSelectedType,
target: "success"
},
SELECT_UPSELL_OFFER: {
actions: Options.actions.setSelectedUpsellOffer,
target: "success"
}
}
},
success: {
type: "final",
data: context => {
return {
__type: "success",
selectedOfferType: context.selectedOfferType,
selectedUpsellOffer: context.selectedUpsellOffer
};
}
},
error: {
id: "error",
type: "final"
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment