import { CollectionState, FiltersMap, WixPatternsContainer, } from '@wix/bex-core'; import { ToolbarCollectionState, ToolbarCollectionStateParamsInitParams, } from './ToolbarCollectionState'; import { GridBaseState } from './GridBaseState'; import { GridSectionsState } from '../components/GridSections/GridSectionsState'; import { ICollectionComponentState } from './ICollectionComponentState'; import { GridSizePreset } from './gridSizePresets'; import { makeObservable, observable } from 'mobx'; export interface GridStateParams { readonly collection: CollectionState; readonly container: WixPatternsContainer; readonly toolbar?: ToolbarCollectionState; } export interface GridStateInitParams extends ToolbarCollectionStateParamsInitParams {} export interface GridStatePublicAPI { /** * Underlying [CollectionState](./?path=/story/common-state--collectionstate) instance * @external */ readonly collection: CollectionState; /** * Underlying [ToolbarCollectionState](./?path=/story/common-state--toolbarcollectionstate) instance * @external */ readonly toolbar: ToolbarCollectionState; /** * Reset active filters & sorting and adds new items to the collection. * @param items - new items to add to the collection * @external */ onAddItemActionComplete(items: T[]): void; } export class GridState implements GridStatePublicAPI, ICollectionComponentState { readonly toolbar; readonly container; readonly collection; readonly _subGrids = [] as GridBaseState[]; _sizePreset = undefined as GridSizePreset | undefined; _sections?: GridSectionsState; constructor(params: GridStateParams) { this.container = params.container; this.collection = params.collection; this.toolbar = params.toolbar ?? new ToolbarCollectionState({ collection: params.collection, container: params.container, componentType: 'Grid', }); makeObservable(this, { _sizePreset: observable.ref, }); } init(params: GridStateInitParams) { const disposers = [this.toolbar.init(params)]; return () => { for (const disposer of disposers) { disposer(); } }; } onAddItemActionComplete = async (items: T[]) => { return this.toolbar.onAddItemActionComplete(items); }; }