Merge remote-tracking branch 'origin/customers'

This commit is contained in:
Ahmed Bouhuolia
2020-06-14 14:34:35 +02:00
17 changed files with 538 additions and 45 deletions

View 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);
});
});
};

View 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];
};

View 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) || []
: [];
};

View 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'
};

View File

@@ -15,9 +15,9 @@ import itemCategories from './itemCategories/itemsCategory.reducer';
import settings from './settings/settings.reducer';
import manualJournals from './manualJournals/manualJournals.reducers';
import globalSearch from './search/search.reducer';
import exchangeRates from './ExchangeRate/exchange.reducer'
import exchangeRates from './ExchangeRate/exchange.reducer';
import globalErrors from './globalErrors/globalErrors.reducer';
import customers from './customers/customers.reducer';
export default combineReducers({
authentication,
@@ -37,4 +37,5 @@ export default combineReducers({
globalSearch,
exchangeRates,
globalErrors,
customers,
});

View File

@@ -16,6 +16,7 @@ import settings from './settings/settings.type';
import search from './search/search.type';
import register from './registers/register.type';
import exchangeRate from './ExchangeRate/exchange.type';
import customer from './customers/customers.type';
export default {
...authentication,
@@ -36,4 +37,5 @@ export default {
...search,
...register,
...exchangeRate,
...customer,
};