Files
bigcapital/client/src/store/selectors.js
2020-04-12 12:03:45 +02:00

36 lines
1.2 KiB
JavaScript

import {pick} from 'lodash';
export const getItemById = (items, id) => {
return items[id] || null;
};
export const pickItemsFromIds = (items, ids) => {
return Object.values(pick(items, ids));
}
export const getCurrentPageResults = (items, pages, pageNumber) => {
const currentPage = pages[pageNumber]
return typeof currentPage == 'undefined' ?
[] : Object.values(pick(items || [], currentPage.ids));
}
export const getCurrentTotalResultsCount = (pagination, name) => {
const currentPageUrl = pagination.currentPages[name]
const currentPage = pagination.pages[currentPageUrl]
return typeof currentPageUrl == 'undefined' ? 0 : pagination.params[currentPage.params]
}
export const getAllResults = (items, pagination, name) => {
const currentPage = pagination.pages[pagination.currentPages[name]]
if (typeof currentPage == 'undefined') {
return []
}
let allPagesKeys = Object.keys(pagination.pages)
let allPagesIds = []
for (let key of allPagesKeys) {
if (pagination.pages[key].params == currentPage.params) {
allPagesIds = allPagesIds.concat(pagination.pages[key].ids)
}
}
return Object.values(pick(items || [], allPagesIds))
}