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

View File

@@ -0,0 +1,137 @@
import ApiService from 'services/ApiService';
import t from 'store/types';
export const submitInvoice = ({ form }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
dispatch({
type: t.SET_DASHBOARD_REQUEST_LOADING,
});
ApiService.post('sales/invoices', 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 deleteInvoice = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.delete(`invoice/${id}`)
.then((response) => {
dispatch({ type: t.INVOICE_DELETE });
resovle(response);
})
.catch((error) => {
reject(error.response.data.errors || []);
});
});
};
export const editInvoice = (id, form) => {
return (dispatch) =>
new Promise((resolve, reject) => {
dispatch({
type: t.SET_DASHBOARD_REQUEST_LOADING,
});
ApiService.post(`invoice/${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 fetchInvoicesTable = ({ query = {} }) => {
return (dispatch, getState) =>
new Promise((resolve, rejcet) => {
const pageQuery = getState().invoices.tableQuery;
dispatch({
type: t.INVOICES_TABLE_LOADING,
payload: {
loading: true,
},
});
ApiService.get('invoices', {
params: { ...pageQuery, ...query },
})
.then((response) => {
dispatch({
type: t.INVOICES_PAGE_SET,
payload: {
invoices: response.data.invoices.results,
pagination: response.data.invoices.pagination,
customViewId: response.data.customViewId || -1,
},
});
dispatch({
type: t.INVOICES_ITEMS_SET,
payload: {
invoices: response.data.invoices.results,
},
});
dispatch({
type: t.INVOICES_PAGINATION_SET,
payload: {
pagination: response.data.invoices.pagination,
customViewId: response.data.customViewId || -1,
},
});
dispatch({
type: t.INVOICES_TABLE_LOADING,
payload: {
loading: false,
},
});
resolve(response);
})
.catch((error) => {
rejcet(error);
});
});
};
export const fetchInvoice = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.get(`invoices/${id}`)
.then((response) => {
dispatch({
type: t.INVOICE_SET,
payload: {
id,
invoice: response.data.invoice,
},
});
resovle(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};

View File

@@ -0,0 +1,32 @@
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 defaultInvoice = {
entires: [],
};
const reducer = createReducer(initialState, {
[t.INVOICE_SET]:(state,actio)=>{
const {id,INVOICE_SET} = action.payload;
}
});

View File

@@ -0,0 +1,9 @@
import { createSelector } from '@reduxjs/toolkit';
const invoiceByIdSelector = (state, props) =>
state.invoices.items[props.invoiceId];
export const getInvoiceById = () =>
createSelector(invoiceByIdSelector, (_invoice) => {
return _invoice;
});

View File

@@ -0,0 +1,12 @@
export default {
INVOICE_DELETE: 'INVOICE_DELETE',
INVOICES_BULK_DELETE: 'INVOICES_BULK_DELETE',
INVOICES_LIST_SET: 'INVOICES_LIST_SET',
INVOICE_SET: 'INVOICE_SET',
INVOICES_SET_CURRENT_VIEW: 'INVOICES_SET_CURRENT_VIEW',
INVOICES_TABLE_QUERIES_ADD: 'INVOICES_TABLE_QUERIES_ADD',
INVOICES_TABLE_LOADING: 'INVOICES_TABLE_LOADING',
INVOICES_PAGINATION_SET: 'INVOICES_PAGINATION_SET',
INVOICES_PAGE_SET: 'INVOICES_PAGE_SET',
INVOICES_ITEMS_SET: 'INVOICES_ITEMS_SET',
};

View File

@@ -0,0 +1,136 @@
import ApiService from 'services/ApiService';
import t from 'store/types';
export const submitReceipt = ({ form }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
dispatch({
type: t.SET_DASHBOARD_REQUEST_LOADING,
});
ApiService.post('sales/receipts', 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 deleteReceipt = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.delete(`receipts/${id}`)
.then((response) => {
dispatch({ type: t.RECEIPT_DELETE });
resovle(response);
})
.catch((error) => {
reject(error.response.data.errors || []);
});
});
};
export const editReceipt = (id, form) => {
return (dispatch) =>
new Promise((resolve, rejcet) => {
dispatch({
type: t.SET_DASHBOARD_REQUEST_LOADING,
});
ApiService.delete(`receipt/${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,
});
rejcet(data?.errors);
});
});
};
export const fetchReceipt = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.get(`receipt/${id}`)
.then((response) => {
dispatch({
type: t.RECEIPT_SET,
payload: {
id,
receipt: response.data.receipt,
},
});
resovle(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};
export const fetchReceiptsTable = ({ query = {} }) => {
return (dispatch, getState) =>
new Promise((resolve, rejcet) => {
const pageQuery = getState().receipt.tableQuery;
dispatch({
type: t.RECEIPTS_TABLE_LOADING,
payload: {
loading: true,
},
});
ApiService.get('receipts', {
params: { ...pageQuery, ...query },
})
.then((response) => {
dispatch({
type: t.RECEIPTS_PAGE_SET,
payload: {
receipts: response.data.receipts.results,
pagination: response.data.receipts.pagination,
customViewId: response.data.customViewId || -1,
},
});
dispatch({
type: t.RECEIPTS_ITEMS_SET,
payload: {
receipts: response.data.receipts.results,
},
});
dispatch({
type: t.RECEIPTS_PAGINATION_SET,
payload: {
pagination: response.data.receipts.pagination,
customViewId: response.data.customViewId || -1,
},
});
dispatch({
type: t.RECEIPTS_TABLE_LOADING,
payload: {
loading: false,
},
});
resolve(response);
})
.catch((error) => {
rejcet(error);
});
});
};

View File

@@ -0,0 +1,12 @@
export default {
RECEIPT_DELETE: 'RECEIPT_DELETE',
RECEIPTS_BULK_DELETE: 'RECEIPTS_BULK_DELETE',
RECEIPTS_LIST_SET: 'RECEIPTS_LIST_SET',
RECEIPT_SET: 'RECEIPT_SET',
RECEIPTS_SET_CURRENT_VIEW: 'RECEIPTS_SET_CURRENT_VIEW',
RECEIPTS_TABLE_QUERIES_ADD: 'RECEIPTS_TABLE_QUERIES_ADD',
RECEIPTS_TABLE_LOADING: 'RECEIPTS_TABLE_LOADING',
RECEIPTS_PAGINATION_SET: 'RECEIPTS_PAGINATION_SET',
RECEIPTS_PAGE_SET: 'RECEIPTS_PAGE_SET',
RECEIPTS_ITEMS_SET: 'RECEIPTS_ITEMS_SET',
};

View File

@@ -18,6 +18,7 @@ import globalSearch from './search/search.reducer';
import exchangeRates from './ExchangeRate/exchange.reducer';
import globalErrors from './globalErrors/globalErrors.reducer';
import customers from './customers/customers.reducer';
import estimates from './Estimate/estimates.reducer';
export default combineReducers({
authentication,
@@ -38,4 +39,5 @@ export default combineReducers({
exchangeRates,
globalErrors,
customers,
estimates
});

View File

@@ -17,6 +17,9 @@ import search from './search/search.type';
import register from './registers/register.type';
import exchangeRate from './ExchangeRate/exchange.type';
import customer from './customers/customers.type';
import estimates from './Estimate/estimates.types';
import invoices from './Invoice/invoices.types';
import receipts from './receipt/receipt.type';
export default {
...authentication,
@@ -38,4 +41,7 @@ export default {
...register,
...exchangeRate,
...customer,
...estimates,
...invoices,
...receipts
};