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,95 @@
import ApiService from 'services/ApiService';
import t from 'store/types';
export const fetchBillsTable = ({ query = {} }) => {
return (dispatch, getState) =>
new Promise((resolve, rejcet) => {
const pageQuery = getState().bills.tableQuery;
dispatch({
type: t.BILLS_TABLE_LOADING,
payload: {
loading: true,
},
});
ApiService.get('purchases/bills', {
params: { ...pageQuery, ...query },
})
.then((response) => {
dispatch({
type: t.BILLS_PAGE_SET,
payload: {
bills: response.data.bills.results,
pagination: response.data.bills.pagination,
customViewId: response.data.customViewId || -1,
},
});
dispatch({
type: t.BILLS_ITEMS_SET,
payload: {
bills: response.data.bills.results,
},
});
dispatch({
type: t.BILLS_PAGINATION_SET,
payload: {
pagination: response.data.bills.pagination,
customViewId: response.data.customViewId || -1,
},
});
dispatch({
type: t.BILLS_TABLE_LOADING,
payload: {
loading: false,
},
});
resolve(response);
})
.catch((error) => {
rejcet(error);
});
});
};
export const deleteBill = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.delete(`purchases/bills/${id}`)
.then((response) => {
dispatch({ type: t.BILL_DELETE, payload: { id } });
resovle(response);
})
.catch((error) => {
reject(error.response.data.errors || []);
});
});
};
export const submitBill = ({ form }) => {
return (dispatch) => ApiService.post('purchases/bills', form);
};
export const fetchBill = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.get(`purchases/bills/${id}`)
.then((response) => {
const { bill } = response.data;
dispatch({
type: t.BILL_SET,
payload: { id, bill },
});
resovle(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};
export const editBill = (id, form) => {
return (dispatch) => ApiService.post(`purchases/bills/${id}`, form);
};

View File

@@ -0,0 +1,104 @@
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
import t from 'store/types';
const initialState = {
items: {},
views: {},
loading: false,
currentViewId: -1,
tableQuery: {
page_size: 5,
page: 1,
},
};
const defaultBill = {
entries: [],
};
const reducer = createReducer(initialState, {
[t.BILL_SET]: (state, action) => {
const { id, bill } = action.payload;
const _bill = state.items[id] || {};
state.items[id] = { ...defaultBill, ..._bill, ...bill };
},
[t.BILLS_TABLE_LOADING]: (state, action) => {
const { loading } = action.payload;
state.loading = loading;
},
[t.BILLS_SET_CURRENT_VIEW]: (state, action) => {
state.currentViewId = action.currentViewId;
},
[t.BILLS_ITEMS_SET]: (state, action) => {
const { bills } = action.payload;
const _bills = {};
bills.forEach((bill) => {
const oldBill = state.items[bill.id] || {};
_bills[bill.id] = {
...defaultBill,
...oldBill,
...bill,
};
});
state.items = {
...state.items,
..._bills,
};
},
[t.BILL_DELETE]: (state, action) => {
const { id } = action.payload;
if (typeof state.items[id] !== 'undefined') {
delete state.items[id];
}
},
[t.BILLS_PAGE_SET]: (state, action) => {
const { customViewId, bills, pagination } = action.payload;
const viewId = customViewId || -1;
const view = state.views[viewId] || {};
state.views[viewId] = {
...view,
pages: {
...(state.views?.[viewId]?.pages || {}),
[pagination.page]: {
ids: bills.map((i) => i.id),
},
},
};
},
[t.BILLS_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('bills', reducer);

View File

@@ -0,0 +1,53 @@
import { createSelector } from '@reduxjs/toolkit';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
const billTableQuery = (state) => state.bills.tableQuery;
const billPageSelector = (state, props, query) => {
const viewId = state.bills.currentViewId;
return state.bills.views?.[viewId]?.pages?.[query.page];
};
const billItemsSelector = (state) => state.bills.items;
const billByIdSelector = (state, props) => state.bills.items[props.billId];
const billPaginationSelector = (state, props) => {
const viewId = state.bills.currentViewId;
return state.bills.views?.[viewId];
};
export const getBillTableQueryFactory = () =>
createSelector(
paginationLocationQuery,
billTableQuery,
(locationQuery, tableQuery) => {
return {
...locationQuery,
...tableQuery,
};
},
);
/**
* Get current page bills items.
* @return {Array}
*/
export const getBillCurrentPageFactory = () =>
createSelector(billPageSelector, billItemsSelector, (billPage, billItems) => {
return typeof billPage === 'object'
? pickItemsFromIds(billItems, billPage.ids) || []
: [];
});
/**
* Retrieve bill details of the given bill id.
*/
export const getBillByIdFactory = () =>
createSelector(billByIdSelector, (bill) => {
return bill;
});
export const getBillPaginationMetaFactory = () =>
createSelector(billPaginationSelector, (billPage) => {
return billPage?.paginationMeta || {};
});

View File

@@ -0,0 +1,12 @@
export default {
BILL_DELETE: 'BILL_DELETE',
BILLS_BULK_DELETE: 'BILLS_BULK_DELETE',
BILLS_LIST_SET: 'BILLS_LIST_SET',
BILL_SET: 'BILL_SET',
BILLS_SET_CURRENT_VIEW: 'BILLS_SET_CURRENT_VIEW',
BILLS_TABLE_QUERIES_ADD: 'BILLS_TABLE_QUERIES_ADD',
BILLS_TABLE_LOADING: 'BILLS_TABLE_LOADING',
BILLS_PAGINATION_SET: 'BILLS_PAGINATION_SET',
BILLS_PAGE_SET: 'BILLS_PAGE_SET',
BILLS_ITEMS_SET: 'BILLS_ITEMS_SET',
};