mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 14:20:31 +00:00
WIP/customers
This commit is contained in:
93
client/src/store/customers/customers.actions.js
Normal file
93
client/src/store/customers/customers.actions.js
Normal file
@@ -0,0 +1,93 @@
|
||||
import ApiService from 'services/ApiService';
|
||||
import t from 'store/types';
|
||||
|
||||
export const submitCustomer = ({ form }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_LOADING,
|
||||
});
|
||||
|
||||
ApiService.post('customers', form)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const editCustomer = ({ form, id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_LOADING,
|
||||
});
|
||||
|
||||
ApiService.post(`customers/${id}`, form)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchCustomers = ({ query }) => {
|
||||
return (dispatch, getState) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const pageQuery = getState().items.tableQuery;
|
||||
dispatch({
|
||||
type: t.ITEMS_TABLE_LOADING,
|
||||
payload: { loading: true },
|
||||
});
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_LOADING,
|
||||
});
|
||||
ApiService.get(`customers`, { params: { ...pageQuery, ...query } })
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.CUSTOMER_SET,
|
||||
customers: response.data.customers.results,
|
||||
});
|
||||
dispatch({
|
||||
type: t.CUSTOMERS_PAGE_SET,
|
||||
customers: response.data.customers.results,
|
||||
customViewId: response.data.customers.customViewId,
|
||||
paginationMeta: response.data.customers.pagination,
|
||||
});
|
||||
dispatch({
|
||||
type: t.CUSTOMERS_TABLE_LOADING,
|
||||
payload: { loading: false },
|
||||
});
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
45
client/src/store/customers/customers.reducer.js
Normal file
45
client/src/store/customers/customers.reducer.js
Normal file
@@ -0,0 +1,45 @@
|
||||
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];
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
export default createTableQueryReducers('customers', customersReducer);
|
||||
|
||||
export const getCustomerById = (state, id) => {
|
||||
return state.customers[id];
|
||||
};
|
||||
10
client/src/store/customers/customers.selectors.js
Normal file
10
client/src/store/customers/customers.selectors.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import { pickItemsFromIds } from 'store/selectors';
|
||||
|
||||
export const getCustomersItems = (state, viewId) => {
|
||||
const customersView = state.customers.views[viewId || -1];
|
||||
const customersItems = state.customers.items;
|
||||
|
||||
return typeof customersView === 'object'
|
||||
? pickItemsFromIds(customersItems, customersView.ids) || []
|
||||
: [];
|
||||
};
|
||||
8
client/src/store/customers/customers.type.js
Normal file
8
client/src/store/customers/customers.type.js
Normal file
@@ -0,0 +1,8 @@
|
||||
export default {
|
||||
CUSTOMERS_ITEMS_SET: 'CUSTOMERS_ITEMS_SET',
|
||||
CUSTOMER_SET: 'CUSTOMER_SET',
|
||||
CUSTOMERS_PAGE_SET: 'CUSTOMERS_PAGE_SET',
|
||||
CUSTOMERS_TABLE_LOADING: 'CUSTOMERS_TABLE_LOADING',
|
||||
CUSTOMERS_TABLE_QUERIES_ADD: 'CUSTOMERS_TABLE_QUERIES_ADD',
|
||||
CUSTOMER_DELETE:'CUSTOMER_DELETE'
|
||||
};
|
||||
Reference in New Issue
Block a user