Skip to content

Instantly share code, notes, and snippets.

@jibwa
Created December 4, 2019 23:23
Show Gist options
  • Save jibwa/d366c63a5d52ec001e3b77a3090b2938 to your computer and use it in GitHub Desktop.
Save jibwa/d366c63a5d52ec001e3b77a3090b2938 to your computer and use it in GitHub Desktop.
updateItem example no mutation
selectService(
context: StateContext<ServiceDetailDialogModel>,
{ payload: {
serviceName,
doSelect
} }: ServiceDetailDialogActions.SelectService) {
const { serviceHosts } = context.getState();
// get the index
const serviceIndex: number = serviceHosts.findIndex(service_ => service_.serviceName === serviceName);
if (serviceIndex < 0) {
//throw error or something?
}
// clone and update
const service = {
...serviceHosts[serviceIndex],
selected: doSelect,
hosts: hosts.map(host => ({
...host,
selected: doSelect
}))
};
ctx.setState(
patch({
serviceHosts: updateItem(serviceIndex, service)
})
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment