Skip to content

Instantly share code, notes, and snippets.

@tmlangley
Created April 21, 2020 16:33
Show Gist options
  • Save tmlangley/acab7a33a08f61e445b4601e8bca5ecc to your computer and use it in GitHub Desktop.
Save tmlangley/acab7a33a08f61e445b4601e8bca5ecc to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const createFilterMachine = (id, applyTransformer) => Machine({
id,
context: {
filterData: {}
},
initial: 'idle',
states: {
idle: {
on: {
OPEN: 'editing',
}
},
loading: {
on: {
SUCCESS: 'idle'
}
},
editing: {
on: {
'CLOSE': {
target: 'idle',
actions: 'applyFilter'
},
'UPDATE': {
target: 'editing',
actions: 'filterChanged'
}
}
}
},
}, {
actions: {
filterChanged: (context, event) => {
assign({ filterData: applyTransformer(event.value) })
},
applyFilter: sendParent('FILTER_APPLIED')
}
});
const filterSetupConfig = {
guestCount: {
type: 'count',
transformFn: data => data
}
};
const AppMachine = Machine({
id: 'appMachine',
initial: 'fetchingAllOptions',
context: {
allAvailableOptions: {},
filterRefs: null,
lastAppliedFilter: '',
},
states: {
fetchingAllOptions: {
entry: 'createFilters',
after: {
1000: 'interactive'
},
},
interactive: {
type: 'parallel',
states: {
filters: {
initial: 'idle',
states: {
idle: {
entry: (context) => console.log('idle', context),
on: {
FILTER_APPLIED: {
target: ['fetching', '#resultsFetching'],
actions: [(context) => console.log(context, 'fetching things')]
}
}
},
fetching: {
after: {
1000: 'idle'
},
entry: () => console.log('fetching')
}
}
},
results: {
initial: 'idle',
states: {
idle: {},
fetching: {
id: 'resultsFetching',
after: {
1200: 'idle'
},
}
}
},
}
},
},
}, {
actions: {
createFilters: assign((context) => {
const filterRefs = Object.entries(filterSetupConfig).reduce((acc, filterConfig) => ({
...acc,
[filterConfig[0]]: spawn(createFilterMachine(filterConfig[0], filterConfig[1].transformFn))
}), {});
return { ...context, filterRefs }
})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment