import { StoreState } from 'app/common/store/store.types'; import { createSelector } from 'reselect'; import datePipe from '../../../shared/pipes/date.pipe'; import moment from 'moment'; import { DOCUMENTS } from './documents.constants'; import { getLoadingStatus } from '../../../shared/modules/loading/getStatus'; const documentsSlice = (state: StoreState) => state.documents; export const selectDocuments = (state: StoreState) => documentsSlice(state).collection; export const selectCategories = (state: StoreState) => documentsSlice(state).categories; export const selectDocumentsLimit = (state: StoreState) => documentsSlice(state).collectionLimit; export const selectDocumentsFilters = (state: StoreState) => documentsSlice(state).filters; export const selectDocumentBookmarks = (state: StoreState) => documentsSlice(state).bookmarks; export const selectDocumentsBookmarksFilter = (state: StoreState) => selectDocumentsFilters(state).bookmarksOnly; export const selectDocumentsCategoryFilter = (state: StoreState) => selectDocumentsFilters(state).categoryIds; export const selectDocumentsLoadingStatus = (state: StoreState) => getLoadingStatus([ DOCUMENTS.FETCH_COLLECTION_SELECTOR ])(state); export const selectDocumentBookmarksLoadingStatus = (state: StoreState) => getLoadingStatus( [ DOCUMENTS.FETCH_BOOKMARKS_SELECTOR, DOCUMENTS.ADD_BOOKMARK_SELECTOR, DOCUMENTS.REMOVE_BOOKMARK_SELECTOR ] )(state); export const selectDocumentsIsMoreStatus = createSelector( [selectDocuments, selectDocumentsLimit], (documents, limit) => documents.length > limit, ); export const selectDocumentsViewList = createSelector( [ selectDocuments, selectDocumentsCategoryFilter, selectDocumentsBookmarksFilter, selectDocumentsLimit, selectCategories, selectDocumentBookmarks, ], ( documents, categoryIds, bookmarksOnly, limit, categories, bookmarks ) => { return documents .filter(document => categoryIds.length > 0 ? categoryIds.includes(document.categoryId) : true) .filter(document => bookmarksOnly ? bookmarks.includes(document.id) : true) .sort((a, b) => moment(b.date).isBefore(moment(a.date)) ? -1 : 1) .map((item) => ({ ...item, date: datePipe.formatLongNotUS(item.date, false), category: categories.find(category => category.id === item.categoryId)?.label, })) .slice(0, limit); } ); export const selectCategoryDropdownList = (state: StoreState) => selectCategories(state) .map((category) => ({ value: category.id, label: category.label }));