WIP / Features / Sate

This commit is contained in:
elforjani3
2020-07-27 02:39:26 +02:00
parent 56278a25f0
commit 4f2679592f
51 changed files with 4515 additions and 44 deletions

View File

@@ -0,0 +1,138 @@
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(`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(`estimates/${id}`)
.then((response) => {
dispatch({ type: t.ESTIMATE_DELETE });
resovle(response);
})
.catch((error) => {
reject(error.response.data.errors || []);
});
});
};
export const fetchEstimate = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.get(`estimate/${id}`)
.then((response) => {
dispatch({
type: t.ESTIMATE_SET,
payload: {
id,
estimate: response.data.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().estimates.tableQuery;
dispatch({
type: t.ESTIMATES_TABLE_LOADING,
payload: {
loading: true,
},
});
ApiService.get('estimates', {
params: { ...pageQuery, ...query },
})
.then((response) => {
dispatch({
type: t.ESTIMATES_PAGE_SET,
payload: {
estimates: response.data.estimates.results,
pagination: response.data.estimates.pagination,
customViewId: response.data.customViewId || -1,
},
});
dispatch({
type: t.ESTIMATES_ITEMS_SET,
payload: {
estimates: response.data.estimates.results,
},
});
dispatch({
type: t.ESTIMATES_PAGINATION_SET,
payload: {
pagination: response.data.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,105 @@
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
import t from 'store/types';
const initialState = {
items: {},
views: {},
loading: false,
tableQuery: {
page_size: 12,
page: 1,
},
currentViewId: -1,
};
const defaultEstimate = {
products: [],
};
const reducer = createReducer(initialState, {
[t.ESTIMATE_SET]: (state, action) => {
const { id, estiamate } = action.payload;
const _estimate = state.items[id] || {};
state.items[id] = { ...defaultEstimate, ..._estimate, ...estiamate };
},
[t.ESTIMATES_ITEMS_SET]: (state, action) => {
const { estiamates } = action.payload;
const _estimates = {};
estiamates.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) => {
const { customViewId, 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: 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('estimates', reducer);
export const getEstimateById = (state, id) => {
return state.estiamates.items[id];
};

View File

@@ -0,0 +1,44 @@
import { createSelector } from '@reduxjs/toolkit';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
const estimateTableQuery = (state) => state.estimates.tableQuery;
export const getEstimatesTableQuery = createSelector(
paginationLocationQuery,
estimateTableQuery,
(location, query) => ({
...location,
...query,
}),
);
const estimatesSelector = (state, props, query) => {
const viewId = state.estimates.currentViewId;
return state.estimates.views?.[viewId]?.pages?.[query.page];
};
const EstimateItemsSelector = (state) => state.estimates.items;
export const getEstimateCurrentPage = () =>
createSelector(estimatesSelector, EstimateItemsSelector, (page, items) => {
return typeof page === 'object'
? pickItemsFromIds(items, page.ids) || []
: [];
});
const estimateByIdSelector = (state, props) => state.estimates.items;
export const getEstimateByIdFactory = () =>
createSelector(estimateByIdSelector, (estimate) => {
return estimate;
});
const paginationSelector = (state, props) => {
const viewId = state.estimates.currentViewId;
return state.estimates.views?.[viewId];
};
export const getEstimatesPaginationMetaFactory = () =>
createSelector(paginationSelector, (estimatePage) => {
return estimatePage?.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',
};