Skip to content

Instantly share code, notes, and snippets.

@timdeschryver
Created April 28, 2018 20:48
Show Gist options
  • Save timdeschryver/6e1bc20fbf8d09e6849360d983ab939d to your computer and use it in GitHub Desktop.
Save timdeschryver/6e1bc20fbf8d09e6849360d983ab939d to your computer and use it in GitHub Desktop.
// State
export interface State {
customers: Customer[];
selectedCustomer: Customer;
}
// Reducer
export function reducer(state, action) {
switch (action.type) {
...
case EDIT_CUSTOMER:
return {
customers: state.customers.map(c => (c.id === action.payload.id ? action.payload : c)),
selectedCustomer:
state.selectedCustomer && state.selectedCustomer.id === action.payload.id
? action.payload
: state.selectedCustomer,
};
...
}
}
// Selectors
export const selectCustomers = createSelector(selectCustomersState, state => state.customers);
export const selectSelectedCustomer = createSelector(selectCustomersState, state => state.selectedCustomer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment