Files
bigcapital/client/src/store/customers/customers.reducer.js
2020-06-17 15:56:46 +02:00

50 lines
1.2 KiB
JavaScript

import t from 'store/types';
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
const initialState = {
items: {},
views: {},
loading: false,
currentViewId: -1,
errors: [],
};
const customersReducer = createReducer(initialState, {
[t.CUSTOMER_SET]: (state, action) => {
const _customers = {};
action.customers.forEach((customer) => {
_customers[customer.id] = customer;
});
state.items = {
...state.items,
..._customers,
};
},
[t.CUSTOMERS_PAGE_SET]: (state, action) => {
const viewId = action.customViewId || -1;
const view = state.views[viewId] || {};
state.views[viewId] = {
...view,
ids: action.customers.map((i) => i.id),
};
},
[t.CUSTOMER_DELETE]: (state, action) => {
if (typeof state.items[action.id] !== 'undefined') {
delete state.items[action.id];
}
},
[t.CUSTOMERS_TABLE_LOADING]: (state, action) => {
const { loading } = action.payload;
state.loading = !!loading;
},
});
export default createTableQueryReducers('customers', customersReducer);
export const getCustomerById = (state, id) => {
return state.customers.items[id];
};