import { CollectionOptimisticActions, CollectionState, Filter, FiltersMap, } from '@wix/bex-core'; import { addEventListener } from '@wix/bex-core/util'; import mapValues from 'lodash/mapValues'; import { action, makeObservable } from 'mobx'; export interface FilterValueTotalStateParams { readonly filter: Filter; readonly collection: CollectionState; readonly value: V | null | undefined; readonly optimisticActions?: CollectionOptimisticActions; readonly ignoreSearch?: boolean; readonly fetchData?: CollectionState['fetchData']; } export class FilterValueTotalState { readonly collection; readonly totalsCollection; readonly optimisticActions; readonly ignoreSearch; tabFilter: Filter | null | undefined; value: V | null | undefined; constructor(params: FilterValueTotalStateParams) { this.collection = params.collection; this.optimisticActions = params.optimisticActions; this.ignoreSearch = params.ignoreSearch; this.value = params.value; const { errorMonitor, errorHandler, onlineState, history, translate, queryCache, lodash, queryName, itemKey, itemName, paginationMode, selectionConsistencyMode, query: { customFilters, search }, fetchData: fetchDataCollection, } = params.collection.result.fetchTotal?._totalsCollection ?? params.collection; const fetchData = params.fetchData ?? fetchDataCollection; const filters = mapValues(customFilters, (filter) => { if (filter === params.filter) { const tabFilter = filter.clone(); this.tabFilter = tabFilter; if (params.value != null) { tabFilter.setValue([params.value]); } else { tabFilter.reset(); } return tabFilter; } return filter; }) as F; this.totalsCollection = new CollectionState({ errorMonitor, errorHandler, onlineState, history, translate, queryCache, lodash, queryName, itemKey, itemName, limit: 1, fetchData: async (query) => { // const fetchDataPromise = fetchData(query); // // const collectionUpdateResultPromise = new Promise((resolve) => { // this.collection.emitter.on('refreshUpdateResult', resolve); // }); // // await Promise.all([fetchDataPromise, collectionUpdateResultPromise]); return fetchData(query); }, paginationMode: paginationMode.id, selectionConsistencyMode, search: params.ignoreSearch ? undefined : search, filters, }); makeObservable(this, { init: action, }); } refresh(value: V | null | undefined) { const { tabFilter } = this; this.value = value; if (tabFilter) { if (value != null) { tabFilter.refresh([value]); } else { tabFilter.reset(); } } } init() { const { totalsCollection, optimisticActions, ignoreSearch, collection } = this; const disposers = [ totalsCollection.init(), addEventListener(collection.emitter, 'search', () => { if (!ignoreSearch) { totalsCollection.clearResultAndMoveToStart(); } }), addEventListener(collection.emitter, 'clearSearch', () => { if (!ignoreSearch) { totalsCollection.clearResultAndMoveToStart(); } }), addEventListener(collection.emitter, 'refreshAllPages', () => { totalsCollection.refreshAllPages(); }), addEventListener(collection.emitter, 'refreshCurrentPage', () => { totalsCollection.refreshCurrentPage(); }), ...(optimisticActions ? [optimisticActions.initCollection(totalsCollection)] : []), ]; return () => { disposers.forEach((d) => d()); }; } }