import { CollectionOptimisticActions, CollectionState, FiltersMap, TaskState, WixPatternsContainer, WixPatternsContainerParams, } from '@wix/bex-core'; import { computed, makeObservable, runInAction } from 'mobx'; import { EditableTableState, EditableTablePermissions, EditableTableMutationHandlers, } from './EditableTable'; import { GridState } from './GridState'; import { ICollectionComponentState } from './ICollectionComponentState'; import { ILayoutSwitchState } from './ILayoutSwitchState'; import { TableState } from './TableState'; import { ToolbarCollectionState } from './ToolbarCollectionState'; import { ViewType, ViewTypeState } from './ViewTypeState'; export interface TableLayoutConfig {} export interface GridLayoutConfig {} export interface SpreadsheetLayoutConfig { mutations: EditableTableMutationHandlers; permissions?: EditableTablePermissions; createOptimisticActions: ( collection: CollectionState, ) => CollectionOptimisticActions; } export interface LayoutSwitchStateParams { collection: CollectionState; container: WixPatternsContainer; containerOverrides: Partial; table?: TableLayoutConfig; grid?: GridLayoutConfig; spreadsheet?: SpreadsheetLayoutConfig; } const componentType = 'LayoutSwitch'; export class LayoutSwitchState implements ICollectionComponentState, ILayoutSwitchState { readonly collection: CollectionState; readonly toolbar: ToolbarCollectionState; readonly containerOverrides: Partial; readonly tableState: TableState | undefined; readonly gridState: GridState | undefined; readonly spreadsheetState: EditableTableState | undefined; readonly viewTypeState: ViewTypeState; readonly initTask = new TaskState(); get viewType(): ViewType { return this.viewTypeState.viewType; } set viewType(v: ViewType) { runInAction(() => { this.viewTypeState.viewType = v; }); } get options(): ViewType[] { return this.viewTypeState.options; } constructor(params: LayoutSwitchStateParams) { this.collection = params.collection; this.containerOverrides = params.containerOverrides; this.toolbar = new ToolbarCollectionState({ componentType, collection: this.collection, container: params.container, reportBi: (biParams) => params.container.reportBi({ ...biParams, params: { ...biParams.params, componentStatus: this.viewTypeState.viewType, }, }), }); this.tableState = params.table ? new TableState({ collection: this.collection, container: params.container, toolbar: this.toolbar, }) : undefined; this.gridState = params.grid ? new GridState({ collection: this.collection, container: params.container, toolbar: this.toolbar, }) : undefined; this.spreadsheetState = params.spreadsheet ? new EditableTableState({ collection: this.collection, container: params.container, toolbar: this.toolbar, ...params.spreadsheet, }) : undefined; const enabledLayouts: ViewType[] = [ params.table ? 'table' : null, params.grid ? 'grid' : null, params.spreadsheet ? 'spreadsheet' : null, ].filter(Boolean) as ViewType[]; this.viewTypeState = new ViewTypeState({ container: params.container, dataCapsule: this.toolbar.dataCapsule, componentType, collection: this.collection, options: enabledLayouts, }); this.toolbar.layoutState = this; makeObservable(this, { viewType: computed, options: computed, }); } init() { const { viewTypeState, initTask } = this; viewTypeState.init(); initTask.runOnce(async () => { viewTypeState.initTask.runOnce(); await viewTypeState.initTask.status.promise; }); } }