Feat : Vendors

This commit is contained in:
elforjani3
2020-11-18 15:12:22 +02:00
parent c0b16ad0ff
commit e97da1b3fe
21 changed files with 1347 additions and 73 deletions

View File

@@ -9,15 +9,14 @@ export const fetchVendorsTable = ({ query }) => {
type: t.VENDORS_TABLE_LOADING,
payload: { loading: true },
});
ApiService.get(`vendors`, { params: { ...pageQuery, ...query } })
.then((response) => {
dispatch({
type: t.VENDORS_PAGE_SET,
payload: {
vendors: response.data.vendors,
pagination: response.data.pagination,
customViewId: response.data.customViewId || -1,
paginationMeta: response.data.pagination,
},
});
dispatch({
@@ -37,7 +36,6 @@ export const fetchVendorsTable = ({ query }) => {
type: t.VENDORS_TABLE_LOADING,
payload: { loading: false },
});
resolve(response);
})
.catch((error) => {
@@ -81,18 +79,32 @@ export const submitVendor = ({ form }) => {
new Promise((resolve, reject) => {
ApiService.post('vendors', 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 fetchVendor = ({ id }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.get(`vendors/${id}`)
.then((response) => {
dispatch({
type: t.VENDOR_SET,
payload: {
id,
vendor: response.data.vendor,
},
});
resolve(response);
})
.catch((error) => {
reject(error);
});
});
};

View File

@@ -1,5 +1,8 @@
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
import {
viewPaginationSetReducer,
createTableQueryReducers,
} from 'store/journalNumber.reducer';
import t from 'store/types';
@@ -7,19 +10,23 @@ const initialState = {
items: {},
views: {},
loading: false,
currentViewId: -1,
tableQuery: {
page_size: 5,
page: 1,
},
currentViewId: -1,
};
const reducer = createReducer(initialState, {
export default createReducer(initialState, {
[t.VENDOR_SET]: (state, action) => {
const { id, vendor } = action.payload;
const _vendors = state.items[id] || {};
state.items[id] = { ..._vendors, ...vendor };
},
[t.VENDORS_TABLE_LOADING]: (state, action) => {
const { loading } = action.payload;
state.loading = loading;
},
[t.VENDORS_ITEMS_SET]: (state, action) => {
const { vendors } = action.payload;
const _vendors = {};
@@ -33,24 +40,21 @@ const reducer = createReducer(initialState, {
..._vendors,
};
},
[t.VENDORS_PAGE_SET]: (state, action) => {
const { customViewId, vendors, pagination } = action.payload;
const { customViewId, vendors, paginationMeta } = action.payload;
const viewId = customViewId || -1;
const view = state.views[viewId] || {};
state.views[viewId] = {
...view,
pages: {
...(state.views?.[viewId]?.pages || {}),
[pagination.page]: {
[paginationMeta.total]: {
ids: vendors.map((i) => i.id),
},
},
};
},
[t.VENDOR_DELETE]: (state, action) => {
const { id } = action.payload;
@@ -58,39 +62,6 @@ const reducer = createReducer(initialState, {
delete state.items[id];
}
},
[t.VENDORS_SET_CURRENT_VIEW]: (state, action) => {
state.currentViewId = action.currentViewId;
},
[t.VENDORS_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,
},
};
},
// [t.VENDOR_SET]: (state, action) => {
// const { id, vendor } = action.payload;
// const _venders = state.items[id] || {};
// state.items[id] = { ..._venders, ...vendor };
// },
// ...viewPaginationSetReducer(t.VENDORS_PAGINATION_SET),
...createTableQueryReducers('VENDORS'),
});
export default createTableQueryReducers('vendors', reducer);

View File

@@ -1,10 +1,18 @@
import { createSelector } from '@reduxjs/toolkit';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
import {
pickItemsFromIds,
paginationLocationQuery,
defaultPaginationMeta,
} from 'store/selectors';
const vendorsTableQuery = (state) => {
return state.vendors.tableQuery;
};
const vendorByIdSelector = (state, props) => {
return state.vendors.items[props.vendorId];
};
export const getVendorsTableQuery = createSelector(
paginationLocationQuery,
vendorsTableQuery,
@@ -18,7 +26,10 @@ export const getVendorsTableQuery = createSelector(
const vendorsPageSelector = (state, props, query) => {
const viewId = state.vendors.currentViewId;
return state.vendors.views?.[viewId]?.pages?.[query.page];
const currentView = state.vendors.views?.[viewId];
const currentPageId = currentView?.pages;
return currentView?.pages?.[currentPageId];
// return state.vendors.views?.[viewId]?.pages?.[query.page];
};
const vendorsItemsSelector = (state) => state.vendors.items;
@@ -41,14 +52,13 @@ const vendorsPaginationSelector = (state, props) => {
export const getVendorsPaginationMetaFactory = () =>
createSelector(vendorsPaginationSelector, (vendorPage) => {
return vendorPage?.paginationMeta || {};
return {
...defaultPaginationMeta(),
...(vendorPage?.paginationMeta || {}),
};
});
const vendorByIdSelector = (state, props) => {
return state.vendors.items[props.vendorId];
};
export const getEstimateByIdFactory = () =>
export const getVendorByIdFactory = () =>
createSelector(vendorByIdSelector, (vendor) => {
return vendor;
});