Skip to content

Instantly share code, notes, and snippets.

@jaetask
Last active March 21, 2020 18:10
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 jaetask/3cf23278a3c08bac8b24de283bc99ec5 to your computer and use it in GitHub Desktop.
Save jaetask/3cf23278a3c08bac8b24de283bc99ec5 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const assignByValue = name => assign({ [name]: (context, event) => event.value });
const addProduct = (logic, product) =>
assign({
products: (c, e) => (logic(c, e) ? c.products.concat(product) : c.products),
});
const installCostsMachine = Machine(
{
id: 'install_costs',
initial: 'idle',
context: {
additionalFaces: 0,
additionalStories: 0,
complexInstall: false,
inRoof: false,
isBatteryPackage: false,
isEvReady: false,
isPvPackage: false,
panelCount: 0,
rosemarySlate: false,
trenching: 0,
products: [],
},
states: {
idle: {
on: {
ADDITIONAL_FACES: { target: 'calculating', actions: 'additionalFaces' },
ADDITIONAL_STORIES: { target: 'calculating', actions: 'additionalStories' },
COMPLEX_INSTALL: { target: 'calculating', actions: 'complexInstall' },
IN_ROOF: { target: 'calculating', actions: 'inRoof' },
IS_BATTERY_PACKAGE: { target: 'calculating', actions: 'isBatteryPackage' },
IS_EV_READY: { target: 'calculating', actions: 'isEvReady' },
IS_PV_PACKAGE: { target: 'calculating', actions: 'isPvPackage' },
PANEL_COUNT: { target: 'calculating', actions: 'panelCount' },
ROSEMARY_SLATE: { target: 'calculating', actions: 'rosemarySlate' },
TRENCHING: { target: 'calculating', actions: 'trenching' },
},
},
calculating: {
on: {
'': {
// Transient transition
target: 'idle',
actions: ['clearProducts', 'checkBaseInstall', 'checkScaffoldFacesXStoreys', 'logContext'],
},
},
},
},
},
{
actions: {
additionalFaces: assignByValue('additionalFaces'),
additionalStories: assignByValue('additionalStories'),
checkBaseInstall: addProduct(c => c.isPvPackage, { prod_id: 'BASE-INSTALL' }),
checkScaffoldFacesXStoreys: addProduct(c => c.additionalFaces > 1 && c.additionalStories > 1, {
prod_id: 'SCAFF-FACES-X-STOREYS',
}),
clearProducts: assign({ products: (c, e) => [] }),
complexInstall: assignByValue('complexInstall'),
inRoof: assignByValue('inRoof'),
isBatteryPackage: assignByValue('isBatteryPackage'),
isEvReady: assignByValue('isEvReady'),
isPvPackage: assignByValue('isPvPackage'),
logContext: (c, e) => console.log(c),
panelCount: assignByValue('panelCount'),
rosemarySlate: assignByValue('rosemarySlate'),
trenching: assignByValue('trenching'),
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment