import { EntityFilter, SerializedEntity } from "@cuba-platform/rest"; import { IReactComponent } from "mobx-react/dist/types/IReactComponent"; import * as React from "react"; import { DataContainer, DataContainerStatus } from "./DataContext"; import { WithId } from '../util/metadata'; /** * Retrieves entity instances from some source (such as the Generic REST API) * based on given criteria such as filters, sort order, limit and offset. * When entities are retrieved from REST API the entity graph is limited using a given view. * Stores retrieved entity instances. * * @typeparam T - entity type. */ export interface DataCollectionStore extends DataContainer { /** * Retrieved entity instances. MobX observable. */ items: Array>; /** * A deep copy of {@link items}. */ readonly readOnlyItems: Array>; /** * Not implemented */ readonly properties: string[]; /** * Name of a view used to limit the entity graph. MobX observable. */ view: string; /** * Sort order. Property name opionally preceeded by `+` or `-` character. * If the name is preceeded by `+`, or there is no preceeding character, then the sort order is ascending. * If the name is preceeded by `-`, then the sort order is descending. * MobX observable. */ sort?: string; /** * An object describing the filtering criteria. * MobX observable. */ filter?: EntityFilter; /** * Maximum number of entities to retrieve. * MobX observable. */ limit?: number; /** * Position of the first entity to retrieve. Useful if you want to skip first N entities * (for example, in a pagination scenario). * MobX observable. */ offset?: number; /** * Total number of entities available in the source. * MobX observable. */ count?: number; /** * When the source of entity instances is REST API, the {@link load} method will by default * request a total count of entity instances to be sent along with the retrieved instances. * This number will be stored as {@link count}. * When `skipCount` is `true`, the total count will not be queried for. */ skipCount?: boolean; /** * Name of the ID attribute of a String ID entity. * Indicates that the entity is a String ID entity. * Mandatory for String ID entities, shall be omitted otherwise. */ stringIdName?: string; /** * Retrieves the entity instances. Once retrieval is complete, the instances will be * available as {@link items}. * When the source of entity instances is REST API, this method will by default * request a total count of entity instances to be sent along with the retrieved instances. * This number will be stored as {@link count}. * Use {@link skipCount} to disable this behavior. * * @returns promise that resolves when retrieval is complete. */ load: () => Promise; /** * Clears {@link items}. */ clear: () => void; /** * Deletes an entity instance from the source. * @param e - entity instance to be deleted. * @returns promise that resolves when deletion is complete. */ delete: (e: T & WithId) => Promise; } /** * A variant of {@link DataCollectionStore} that works with a client-side source of entities. * * @typeparam T - entity type. */ export interface ClientSideDataCollectionStore extends DataCollectionStore { /** * The client-side source from where the entities are being retrieved. * An array of entity instances with default sort order and no filtering applied. */ allItems: Array>; /** * Sets {@link items} based on {@link allItems} and other conditions. * Currently it only performs client-side sorting based on {@link sort} field, * client-side filtering is currently not supported. */ adjustItems: () => void; } export interface DataCollectionOptions { /** * Whether to call the {@link DataCollectionStore.load} method immediately after the * {@link DataCollectionStore} is constructed. */ loadImmediately?: boolean; /** * See {@link DataCollectionStore.view}. */ view?: string; /** * See {@link DataCollectionStore.sort}. */ sort?: string; /** * See {@link DataCollectionStore.limit}. */ limit?: number; /** * See {@link DataCollectionStore.offset}. */ offset?: number; /** * See {@link DataCollectionStore.filter}. */ filter?: EntityFilter; /** * See {@link DataCollectionStore.stringIdName}. */ stringIdName?: string; /** * Whether to track the changed items. When `true`, the changes will be available via * {@link DataContainer.changedItems} field. */ trackChanges?: boolean; } export interface ClientSideDataCollectionOptions extends DataCollectionOptions { /** * See {@link ClientSideDataCollectionStore.allItems}. */ allItems?: Array>; } export declare const defaultOpts: DataCollectionOptions; export declare function fromRestModel(items: Array>, stringIdName?: string): Array>; export declare const withDataCollection: (entityName: string, opts?: DataCollectionOptions) => >(target: T) => T & import("mobx-react").IWrappedComponent; /** * A hook that returns a mutable ref object containing a {@link DataCollectionStore} * initialized with provided entity name and options. The {@link DataCollectionStore} * value will be preserved between renders. * * @typeparam T - entity type. * * @param entityName * @param opts */ export declare const useCollection: (entityName: string, opts: DataCollectionOptions) => React.MutableRefObject>; /** * Initialization function that instantiates a {@link DataCollectionStore} implementation * which uses Generic REST API as the source. * * @typeparam E - entity type. * * @param entityName - name of the entity to be retrieved. * @param opts - {@link DataCollectionStore} configuration. */ export declare const collection: (entityName: string, opts?: DataCollectionOptions) => DataCollectionStore; /** * Initialization function that instantiates a {@link ClientSideDataCollectionStore} implementation * * @typeparam E - entity type. * * @param entityName - name of the entity to be retrieved. * @param opts - {@link ClientSideDataCollectionStore} configuration. */ export declare const clientSideCollection: (entityName: string, opts?: ClientSideDataCollectionOptions) => ClientSideDataCollectionStore; export interface DataCollectionInjected { dataCollection?: DataCollectionStore; } export interface DataCollectionProps extends DataCollectionOptions { entityName: string; children?: (store: Partial>) => React.ReactNode; } export declare class Collection extends React.Component> { store: DataCollectionStore; constructor(props: DataCollectionProps); render(): React.ReactNode; get childrenProps(): { items: SerializedEntity[]; status: DataContainerStatus; load: () => Promise; clear: () => void; }; }