feat: Merge sales branch

This commit is contained in:
Ahmed Bouhuolia
2020-08-19 02:17:23 +02:00
parent c2a60e6ba5
commit b46570dc01
97 changed files with 9901 additions and 48 deletions

View File

@@ -0,0 +1,140 @@
import ApiService from 'services/ApiService';
import t from 'store/types';
export const submitEstimate = ({ form }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
dispatch({
type: t.SET_DASHBOARD_REQUEST_LOADING,
});
ApiService.post('sales/estimates', 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 editEstimate = (id, form) => {
return (dispatch) =>
new Promise((resolve, reject) => {
dispatch({
type: t.SET_DASHBOARD_REQUEST_LOADING,
});
ApiService.post(`sales/estimates/${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 deleteEstimate = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.delete(`sales/estimates/${id}`)
.then((response) => {
dispatch({
type: t.ESTIMATE_DELETE,
payload: { id },
});
resovle(response);
})
.catch((error) => {
reject(error.response.data.errors || []);
});
});
};
export const fetchEstimate = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.get(`sales/estimates/${id}`)
.then((response) => {
const { estimate } = response.data;
dispatch({
type: t.ESTIMATE_SET,
payload: {
id,
estimate,
},
});
resovle(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};
export const fetchEstimatesTable = ({ query = {} }) => {
return (dispatch, getState) =>
new Promise((resolve, rejcet) => {
const pageQuery = getState().sales_estimates.tableQuery;
dispatch({
type: t.ESTIMATES_TABLE_LOADING,
payload: {
loading: true,
},
});
ApiService.get('sales/estimates', {
params: { ...pageQuery, ...query },
})
.then((response) => {
dispatch({
type: t.ESTIMATES_PAGE_SET,
payload: {
sales_estimates: response.data.sales_estimates.results,
pagination: response.data.sales_estimates.pagination,
customViewId: response.data.customViewId || -1,
},
});
dispatch({
type: t.ESTIMATES_ITEMS_SET,
payload: {
sales_estimates: response.data.sales_estimates.results,
},
});
dispatch({
type: t.ESTIMATES_PAGINATION_SET,
payload: {
pagination: response.data.sales_estimates.pagination,
customViewId: response.data.customViewId || -1,
},
});
dispatch({
type: t.ESTIMATES_TABLE_LOADING,
payload: {
loading: false,
},
});
resolve(response);
})
.catch((error) => {
rejcet(error);
});
});
};

View File

@@ -0,0 +1,107 @@
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
import t from 'store/types';
const initialState = {
items: {},
views: {},
loading: false,
tableQuery: {
page_size: 5,
page: 1,
},
currentViewId: -1,
};
const defaultEstimate = {
entries: [],
};
const reducer = createReducer(initialState, {
[t.ESTIMATE_SET]: (state, action) => {
const { id, estimate } = action.payload;
const _estimate = state.items[id] || {};
state.items[id] = { ...defaultEstimate, ..._estimate, ...estimate };
},
[t.ESTIMATES_ITEMS_SET]: (state, action) => {
const { sales_estimates } = action.payload;
const _estimates = {};
sales_estimates.forEach((estimate) => {
_estimates[estimate.id] = {
...defaultEstimate,
...estimate,
};
});
state.items = {
...state.items,
..._estimates,
};
},
[t.ESTIMATES_TABLE_LOADING]: (state, action) => {
const { loading } = action.payload;
state.loading = loading;
},
[t.ESTIMATES_SET_CURRENT_VIEW]: (state, action) => {
state.currentViewId = action.currentViewId;
},
[t.ESTIMATE_DELETE]: (state, action) => {
const { id } = action.payload;
if (typeof state.items[id] !== 'undefined') {
delete state.items[id];
}
},
[t.ESTIMATES_PAGE_SET]: (state, action) => {
// @todo camelCase keys.
const { customViewId, sales_estimates, pagination } = action.payload;
const viewId = customViewId || -1;
const view = state.views[viewId] || {};
state.views[viewId] = {
...view,
pages: {
...(state.views?.[viewId]?.pages || {}),
[pagination.page]: {
ids: sales_estimates.map((i) => i.id),
},
},
};
},
[t.ESTIMATES_PAGINATION_SET]: (state, action) => {
const { pagination, customViewId } = action.payload;
const mapped = {
pageSize: parseInt(pagination.pageSize, 10),
page: parseInt(pagination.page, 10),
total: parseInt(pagination.total, 10),
};
const paginationMeta = {
...mapped,
pagesCount: Math.ceil(mapped.total / mapped.pageSize),
pageIndex: Math.max(mapped.page - 1, 0),
};
state.views = {
...state.views,
[customViewId]: {
...(state.views?.[customViewId] || {}),
paginationMeta,
},
};
},
});
export default createTableQueryReducers('sales_estimates', reducer);
export const getEstimateById = (state, id) => {
return state.sales_estimates.items[id];
};

View File

@@ -0,0 +1,51 @@
import { createSelector } from '@reduxjs/toolkit';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
const estimateTableQuery = (state) => state.sales_estimates.tableQuery;
const estimateByIdSelector = (state, props) =>
state.sales_estimates.items[props.estimateId];
const estimatesCurrentViewSelector = (state, props) => {
const viewId = state.sales_estimates.currentViewId;
return state.sales_estimates.views?.[viewId];
};
const estimateItemsSelector = (state) => state.sales_estimates.items;
const estimatesPageSelector = (state, props, query) => {
const viewId = state.sales_estimates.currentViewId;
return state.sales_estimates.views?.[viewId]?.pages?.[query.page];
};
export const getEstimatesTableQueryFactory = () =>
createSelector(
paginationLocationQuery,
estimateTableQuery,
(locationQuery, tableQuery) => {
return {
...locationQuery,
...tableQuery,
};
},
);
export const getEstimateCurrentPageFactory = () =>
createSelector(
estimatesPageSelector,
estimateItemsSelector,
(estimatePage, estimateItems) => {
return typeof estimatePage === 'object'
? pickItemsFromIds(estimateItems, estimatePage.ids) || []
: [];
},
);
export const getEstimateByIdFactory = () =>
createSelector(estimateByIdSelector, (estimate) => {
return estimate;
});
export const getEstimatesPaginationMetaFactory = () =>
createSelector(estimatesCurrentViewSelector, (estimateView) => {
return estimateView?.paginationMeta || {};
});

View File

@@ -0,0 +1,13 @@
export default {
ESTIMATES_LIST_SET: 'ESTIMATES_LIST_SET',
ESTIMATE_SET: 'ESTIMATE_SET',
ESTIMATE_DELETE: 'ESTIMATE_DELETE',
ESTIMATES_BULK_DELETE: 'ESTIMATES_BULK_DELETE',
ESTIMATES_SET_CURRENT_VIEW: 'ESTIMATES_SET_CURRENT_VIEW',
ESTIMATES_TABLE_QUERIES_ADD: 'ESTIMATES_TABLE_QUERIES_ADD',
ESTIMATES_TABLE_LOADING: 'ESTIMATES_TABLE_LOADING',
ESTIMATES_PAGINATION_SET: 'ESTIMATES_PAGINATION_SET',
ESTIMATES_PAGE_SET: 'ESTIMATES_PAGE_SET',
ESTIMATES_ITEMS_SET:'ESTIMATES_ITEMS_SET',
};