mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
feat: Items data table.
This commit is contained in:
@@ -11,10 +11,16 @@ export const editItem = ({ id, form }) => {
|
||||
|
||||
export const fetchItems = ({ query }) => {
|
||||
return (dispatch) => new Promise((resolve, reject) => {
|
||||
ApiService.get(`items`, query).then(response => {
|
||||
ApiService.get(`items`).then(response => {
|
||||
dispatch({
|
||||
type: t.ITEMS_LIST_SET,
|
||||
items: response.data.items,
|
||||
type: t.ITEMS_SET,
|
||||
items: response.data.items.results,
|
||||
});
|
||||
dispatch({
|
||||
type: t.ITEMS_PAGE_SET,
|
||||
items: response.data.items.results,
|
||||
customViewId: response.data.customViewId,
|
||||
paginationMeta: response.data.items.pagination,
|
||||
});
|
||||
resolve(response);
|
||||
}).catch(error => { reject(error); });
|
||||
|
||||
@@ -1,6 +1,92 @@
|
||||
import t from 'store/types';
|
||||
import { createReducer } from '@reduxjs/toolkit';
|
||||
import {
|
||||
getItemsViewPages,
|
||||
} from 'store/items/items.selectors';
|
||||
|
||||
const initialState = {
|
||||
list: [],
|
||||
itemById: {},
|
||||
items: {},
|
||||
views: {},
|
||||
itemsRelation: {},
|
||||
currentPage: 1,
|
||||
currentViewId: -1,
|
||||
bulkActions: {},
|
||||
};
|
||||
|
||||
export default createReducer(initialState, {
|
||||
[t.ITEMS_SET]: (state, action) => {
|
||||
const _items = {};
|
||||
|
||||
action.items.forEach(item => {
|
||||
_items[item.id] = item;
|
||||
});
|
||||
state.items = {
|
||||
...state.items,
|
||||
..._items,
|
||||
};
|
||||
},
|
||||
|
||||
[t.ITEMS_PAGE_SET]: (state, action) => {
|
||||
const { items, customViewId, paginationMeta } = action;
|
||||
|
||||
const viewId = customViewId || -1;
|
||||
const view = state.views[viewId] || {};
|
||||
const viewPages = getItemsViewPages(state.views, viewId);
|
||||
|
||||
items.forEach((item) => {
|
||||
const stateItem = state.items[item.id];
|
||||
const itemRelation = state.itemsRelation[stateItem.id];
|
||||
|
||||
if (typeof itemRelation === 'undefined') {
|
||||
state.itemsRelation[item.id] = [];
|
||||
}
|
||||
const filteredRelation = state.itemsRelation[item.id]
|
||||
.filter((relation) => (
|
||||
relation.viewId === viewId &&
|
||||
relation.pageNumber === paginationMeta.page
|
||||
));
|
||||
|
||||
filteredRelation.push({
|
||||
viewId,
|
||||
pageNumber: paginationMeta.page,
|
||||
});
|
||||
state.itemsRelation[item.id] = filteredRelation;
|
||||
});
|
||||
|
||||
state.views[viewId] = {
|
||||
...view,
|
||||
pages: {
|
||||
...viewPages,
|
||||
[paginationMeta.page]: {
|
||||
ids: items.map(i => i.id),
|
||||
meta: paginationMeta,
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
[t.ITEM_BULK_ACTION_ADD]: (state, action) => {
|
||||
state.bulkActions[action.itemId] = true;
|
||||
},
|
||||
|
||||
[t.ITEM_BULK_ACTION_REMOVE]: (state, action) => {
|
||||
delete state.bulkActions[action.itemId];
|
||||
},
|
||||
|
||||
[t.ITEM_DELETE]: (state, action) => {
|
||||
const { itemId } = action;
|
||||
|
||||
if (state.items[itemId]) {
|
||||
const item = state.items[itemId];
|
||||
const itemPageNumber = item._page_number;
|
||||
|
||||
delete state.items[itemId];
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
});
|
||||
|
||||
export const getItemById = (state, id) => {
|
||||
return state.items.items[id];
|
||||
};
|
||||
7
client/src/store/items/items.selectors.js
Normal file
7
client/src/store/items/items.selectors.js
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
|
||||
|
||||
export const getItemsViewPages = (itemsViews, viewId) => {
|
||||
return itemsViews[viewId] ?
|
||||
itemsViews[viewId].pages : {};
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
|
||||
export default {
|
||||
ITEMS_SET: 'ITEMS_SET',
|
||||
ITEMS_PAGE_SET: 'ITEMS_PAGE_SET',
|
||||
ITEM_DELETE: 'ITEM_DELETE',
|
||||
ITEM_BULK_ACTION_ADD: 'ITEM_BULK_ACTION_ADD',
|
||||
ITEM_BULK_ACTION_REMOVE: 'ITEM_BULK_ACTION_REMOVE',
|
||||
}
|
||||
Reference in New Issue
Block a user