Skip to content

Instantly share code, notes, and snippets.

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