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,138 @@
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(`sales/receipts/${id}`)
.then((response) => {
dispatch({
type: t.RECEIPT_DELETE,
payload: { id },
});
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.post(`sales/receipts/${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(`sales/receipts/${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().sales_receipts.tableQuery;
dispatch({
type: t.RECEIPTS_TABLE_LOADING,
payload: {
loading: true,
},
});
ApiService.get('sales/receipts', {
params: { ...pageQuery, ...query },
})
.then((response) => {
dispatch({
type: t.RECEIPTS_PAGE_SET,
payload: {
sales_receipts: response.data.sales_receipts.results,
pagination: response.data.sales_receipts.pagination,
customViewId: response.data.customViewId || -1,
},
});
dispatch({
type: t.RECEIPTS_ITEMS_SET,
payload: {
sales_receipts: response.data.sales_receipts.results,
},
});
dispatch({
type: t.RECEIPTS_PAGINATION_SET,
payload: {
pagination: response.data.sales_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,103 @@
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 defaultReceipt = {
entries: [],
};
const reducer = createReducer(initialState, {
[t.RECEIPT_SET]: (state, action) => {
const { id, receipt } = action.payload;
const _receipt = state.items[id] || {};
state.items[id] = { ...defaultReceipt, ..._receipt, ...receipt };
},
[t.RECEIPTS_ITEMS_SET]: (state, action) => {
const { sales_receipts } = action.payload;
const _receipts = {};
sales_receipts.forEach((receipt) => {
_receipts[receipt.id] = {
...defaultReceipt,
...receipt,
};
});
state.items = {
...state.items,
..._receipts,
};
},
[t.RECEIPTS_TABLE_LOADING]: (state, action) => {
const { loading } = action.payload;
state.loading = loading;
},
[t.RECEIPT_DELETE]: (state, action) => {
const { id } = action.payload;
if (typeof state.items[id] !== 'undefined') {
delete state.items[id];
}
},
[t.RECEIPTS_SET_CURRENT_VIEW]: (state, action) => {
state.currentViewId = action.currentViewId;
},
[t.RECEIPTS_PAGE_SET]: (state, action) => {
const { customViewId, sales_receipts, 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_receipts.map((i) => i.id),
},
},
};
},
[t.RECEIPTS_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_receipts', reducer);
export const getReceiptById = (state, id) => {
return state.receipts.items[id];
};

View File

@@ -0,0 +1,52 @@
import { createSelector } from '@reduxjs/toolkit';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
const receiptsPageSelector = (state, props, query) => {
const viewId = state.sales_receipts.currentViewId;
return state.sales_receipts.views?.[viewId]?.pages?.[query.page];
};
const receiptsPaginationSelector = (state, props) => {
const viewId = state.sales_receipts.currentViewId;
return state.sales_receipts.views?.[viewId];
};
const receiptItemsSelector = (state) => state.sales_receipts.items;
const receiptTableQuery = (state) => state.sales_receipts.tableQuery;
const receiptByIdSelector = (state, props) => state.sales_receipts.items[props.receiptId];
export const getReceiptCurrentPageFactory = () =>
createSelector(
receiptsPageSelector,
receiptItemsSelector,
(receiptPage, receiptItems) => {
return typeof receiptPage === 'object'
? pickItemsFromIds(receiptItems, receiptPage.ids) || []
: [];
},
);
export const getReceiptsTableQueryFactory = () =>
createSelector(
paginationLocationQuery,
receiptTableQuery,
(locationQuery, tableQuery) => {
return {
...locationQuery,
...tableQuery,
};
},
);
export const getReceiptByIdFactory = () =>
createSelector(receiptByIdSelector, (receipt) => {
return receipt;
});
export const getReceiptsPaginationMetaFactory = () =>
createSelector(receiptsPaginationSelector, (receiptPage) => {
return receiptPage?.paginationMeta || {};
});

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