import { ArrayType, AssetValueType, AwaitedDebounceInstance, DBProto, InvalidResult, Logger, RecursiveReadonly, ResolvableContent, SubsetKeys } from '@nu-art/ts-common'; import { ModuleFE_BaseApi } from '../modules/db-api-gen/ModuleFE_BaseApi.js'; export type UIProps_EditableItem = AssetValueType> = { editable: EditableItem; prop: Prop; ignoreError?: boolean; }; type ValidationErrors = { autoSave: boolean; editing: boolean; results: InvalidResult; }; export type Editable_OnError = (item: Partial, err: Error) => any | Promise; export type Editable_SaveAction = (item: T) => Promise; export type Editable_PreSaveAction = (item: T) => void; export type Editable_DeleteAction = (item: T) => Promise; export type Editable_OnChange = (editable: EditableItem) => Promise; export declare const EditableItemStatus_Saving = "saving"; export declare const EditableItemStatus_SavedWithErrors = "saved-with-errors"; export declare const EditableItemStatus_FailedValidation = "failed-validation"; export declare const EditableItemStatus_ErrorSaving = "error-saving"; export declare const EditableItemStatus_UnsavedChanges = "unsaved-changes"; export declare const EditableItemStatus_Creating = "creating"; export declare const EditableItemStatus_Saved = "saved"; export declare const EditableItemStatus_Unknown = "unknown"; declare const EditableItemStatuses: string[]; export type EditableItem_Status = typeof EditableItemStatuses[number]; export interface EditableItemStatusListener { onEditableItemStatusChanged(newStatus: EditableItem_Status, prevStatus: EditableItem_Status, editableItem: EditableItem): any | Promise; } /** * A utility class for editing any item of type T. * It encapsulates an item along with functions to save and delete the item. * This class can be used with any type of item that can be modified, saved, and deleted. * * @template T The type of the item. */ export declare class EditableItem extends Logger { static AUTO_SAVE: boolean; protected readonly internalSaveAction: Editable_SaveAction; private status; private statusListener; readonly item: Partial; protected originalItem: Partial; protected validationResults?: ValidationErrors; protected conflictingItem?: T; protected _autoSave: boolean; protected _isSaving: boolean; protected saveError?: Error; protected onSaveCompleted?: (item: T) => any; protected onChanged?: Editable_OnChange; protected preSaveAction?: Editable_PreSaveAction; private deleteAction; private saveAction; private onError?; /** * Constructs an EditableItem instance. * * @param item The item to be edited. * @param saveAction The function to be called when saving the item. * @param deleteAction The function to be called when deleting the item. */ constructor(item: Partial); private updateItemImpl; setOnError(onError: Editable_OnError): this; setOnChanged(onChanged?: Editable_OnChange): this; protected handleValidationError(err: Error): void; setSaveAction(saveAction: Editable_SaveAction): this; setDeleteAction(deleteAction: Editable_DeleteAction): this; setPreSaveAction(preSaveAction: Editable_PreSaveAction): this; setOnDelete(onDelete: (item: T) => Promise): this; setOnSaveCompleted(onSaved?: (item: T) => any): this; setConflictingItem(conflictingItem?: T): void; protected hasConflicts(conflictingItem: T): boolean; protected hasChanges(): boolean; addStatusListener(listener: EditableItemStatusListener): void; removeStatusListener(listener: EditableItemStatusListener): void; protected calculateState(): void; protected setStatus(newStatus: EditableItem_Status): void; getStatus(): string; /** * Set the auto-save mode.Page_ItemsEditor * * @param mode The auto-save mode. * @returns The instance itself for method chaining. */ setAutoSave(mode: boolean): this; /** * Get the saving status of the current editable instance * @returns The saving status as boolean */ isSaving(): boolean; /** * Set the value of a specific property in the item. * * @template K The type of the key. * @param key The key of the property. * @param value The new value of the property. * @returns A boolean indicating whether the value has been set. */ set(key: K, value: ResolvableContent): boolean; /** * Updates the item with the given values and performs auto-save if enabled. * This method allows partial updates, only the properties provided in the values object will be updated. * * @param values An object with partial properties of T. * @returns A promise representing the auto-save operation if changes were made and auto-save is enabled. */ updateObj(values: Partial<{ [K in keyof T]: ResolvableContent; }>): Promise; /** * Equivalent to array.push * @param value * @param index */ updateArrayAt(value: ArrayType, index?: number): Promise; removeArrayItem(index: number): Promise; /** * Update the value of a specific property in the item and perform auto-save if enabled. * * @template K The type of the key. * @param key The key of the property. * @param value The new value of the property. * @returns A promise representing the auto-save operation if enabled, undefined otherwise. */ update(key: K, value: ((item?: T[K]) => T[K]) | T[K] | undefined): Promise; /** * Get a value of a specific property * * @template K The type of the key. * @param key The key of the property. * @param fallbackValue * @returns Readonly type of the value in T[K]. */ get(key: K, fallbackValue?: T[K]): RecursiveReadonly; hasError(key?: K): (Readonly> | Readonly) | undefined; hasValidationError: () => boolean; protected setValidationResults(ValidationResults?: ValidationErrors): this; /** * If auto save in the editable item is true, preform the save action * Can be overridden in deriving classes * @protected */ protected autoSaveImpl(): Promise; protected processResponse(dbItem: T): T; private autoSave; /** * Save the item by calling the saveAction function. * * @returns The promise returned by the saveAction function. */ save(consumeError?: boolean): Promise; /** * Delete the item by calling the deleteAction function. * * @template K The type of the key. * @returns A promise representing the delete operation. */ delete(): Promise; /** * Return a new EditableItem that represents a property of the current item. * * @template K The type of the key. * @param key The key of the property. * @param defaultValue The default value of the property. * @returns The new EditableItem. */ editProp(key: K, defaultValue: ResolvableContent>>): EditableItem>; /** * Return a new EditableItem for the DB Entity described by the reference id associated with the provided key and module. * * @template K The type of the key. * @param key The key of the reference id. * @param module The module associated with the reference id. * @param initialValue An initial value to use in case there is no reference id. * * @returns The new EditableItem. */ editRefV3, K extends SubsetKeys>(key: K, module: ModuleFE_BaseApi, initialValue: Partial): EditableDBItemV3; /** * Implement in children! validate the item using custom logic * @protected */ validate(): void; /** * Trigger on change with all necessary actions for the editable instance * @protected */ protected callOnChange(): void; /** * handle item update from a different source out of the editable item * @param newItem The item passed to the instance from the third party */ updateItem(newItem: T): void; } /** * A utility class for editing any item of type T that can be stored in a database. * This class extends EditableItem and adds functionality related to database operations. * * @template T The type of the item that extends DB_Object. * @template Ks The keys of the T type. Default is '_id'. */ export declare class EditableDBItemV3> extends EditableItem { private readonly module; private debounceInstance; private debounceTimeout; /** * Constructs an EditableDBItemV3 instance. * * @param item The item to be edited. * @param module The module for database operations. * @param onCompleted The function to be called when the operation is completed. * @param debounceInstance Debounce instance from previous editable item */ constructor(item: Partial, module: ModuleFE_BaseApi, debounceInstance?: AwaitedDebounceInstance); save(consumeError?: boolean): Promise; protected processResponse(dbItem: Proto['dbType']): Proto['dbType']; /** * Preform auto save in editable db item will be in debounce * @protected */ protected autoSaveImpl(): Promise; setDebounceTimeout: (timeout: number) => EditableDBItemV3; protected hasConflicts(_conflictingItem: Proto['dbType']): boolean; protected calculateState(): void; /** * Use the db module provided to validate and update the validation results accordingly * @return preDB item if validation succeeded otherwise returns void */ validate(): Proto["preDbType"] | undefined; /** * Update db item from a source out of the save action merged with the current UI item in the instance * @param newItem The item passed to the instance from the third party */ updateItem(newItem: Proto['uiType']): void; protected handleValidationError(e: Error): void; } export {};