UNPKG

@gpa-gemstone/react-interactive

Version:
161 lines (160 loc) 6.28 kB
import { AsyncThunk, Slice } from '@reduxjs/toolkit'; import { Application } from '@gpa-gemstone/application-typings'; import { Search } from './SearchBar'; import { WritableDraft } from 'immer/dist/types/types-external'; interface IOptions<T extends U> { /** * Optional function triggered on specific action dependencies. * @param state - The current state of type IState<T>. * @param action - The action triggering the dependency. * @param arg - Additional argument for the dependency. */ ActionDependencies?: (state: IState<T>, action: string, arg: any) => void; /** * Optional function triggered on pending action dependencies. * @param state - The current state of type IState<T>. * @param action - The action triggering the dependency. * @param arg - Additional argument for the dependency. * @param requestID - The ID associated with the request. * */ ActionPendingDependencies?: (state: IState<T>, action: string, arg: any, requestID: string) => void; /** * Optional function triggered on action error dependencies. * @param state - The current state of type IState<T>. * @param action - The action triggering the dependency. * @param arg - Additional argument for the dependency. * @param requestID - The ID associated with the request. */ ActionErrorDependencies?: (state: IState<T>, action: string, arg: any, requestID: string) => void; /** * Optional function triggered on action fulfilled dependencies. * @param state - The current state of type IState<T>. * @param action - The action triggering the dependency. * @param arg - Additional argument for the dependency. * @param requestID - The ID associated with the request. */ ActionFullfilledDependencies?: (state: IState<T>, action: string, arg: any, requestID: string) => void; /** * Array of additional thunks of type IAdditionalThunk<T>. */ AddionalThunks?: IAdditionalThunk<T>[]; } interface IAdditionalThunk<T extends U> { Name: string; Fetch: (state: IState<T>, args: any | void) => null | JQuery.jqXHR<any>; OnSuccess?: (state: WritableDraft<IState<T>>, requestId: string, data: any, args: any | void) => void; OnFailure?: (state: WritableDraft<IState<T>>, requestId: string, args: any | void, error: any) => void; OnPending?: (state: WritableDraft<IState<T>>, requestId: string, args: any | void) => void; } /** * Common properties of an object type U with an ID of type number or string, including error message, verb, and time in string format. */ interface U { ID: number | string; } interface IError { Message: string; Verb: 'POST' | 'DELETE' | 'PATCH' | 'FETCH' | 'SEARCH' | 'PAGE'; Time: string; } /** * Represents the state of the application with generic type T extending U. */ export interface IState<T extends U> { Status: Application.Types.Status; ActiveFetchID: string[]; SearchStatus: Application.Types.Status; ActiveSearchID: string[]; Error: (IError | null); Data: T[]; SortField: keyof T; Ascending: boolean; ParentID: (number | null | string); SearchResults: T[]; Filter: Search.IFilter<T>[]; } /** * Interface representing a state with paging capabilities, extending IState<T>. */ interface IPagedState<T extends U> extends IState<T> { PagedStatus: Application.Types.Status; ActivePagedID: string[]; CurrentPage: number; TotalPages: number; TotalRecords: number; PagedData: T[]; PagedSortField: keyof T; PagedAscending: boolean; PagedFilter: Search.IFilter<T>[]; } /** * A generic class providing functionalities related to a slice of data. */ export default class GenericSlice<T extends U> { Name: string; APIPath: string; Slice: (Slice<IPagedState<T>>); Fetch: (AsyncThunk<any, void | number | string, {}>); SetChanged: (AsyncThunk<any, void, {}>); DBAction: (AsyncThunk<any, { verb: 'POST' | 'DELETE' | 'PATCH'; record: T; }, {}>); DBSearch: (AsyncThunk<any, { filter: Search.IFilter<T>[]; sortField?: keyof T; ascending?: boolean; }, {}>); PagedSearch: (AsyncThunk<any, { filter?: Search.IFilter<T>[]; sortField?: keyof T; ascending?: boolean; page?: number; }, {}>); Sort: (AsyncThunk<any, { SortField: keyof T; Ascending: boolean; }, {}>); AdditionalThunk: { [key: string]: AsyncThunk<any, any, {}>; }; Reducer: any; private fetchHandle; private searchHandle; private pageHandle; private controller; private actionDependency; private actionFullfilledDependency; private actionPendingDependency; private actionErrorDependency; /** * Creates a new GenericSlice of type T, which can be used to perform basic CRUD operations against * a specified web api. * @typeParam T - Model of Generic Slice * @param {string} name - string defining the name of the slice in the store * @param {string} apiPath - string containing relative path to web api * @param {keyof T} defaultSort - string showing default sort field * @param {boolean} ascending - (optional) default sort direction - defaults to true * @returns a new GenericSlice<T> */ constructor(name: string, apiPath: string, defaultSort: keyof T, ascending?: boolean, options?: IOptions<T> | null); Data: (state: any) => T[]; Error: (state: any) => IError; Datum: (state: any, id: number | string) => T; Status: (state: any) => Application.Types.Status; SortField: (state: any) => keyof T; Ascending: (state: any) => boolean; ParentID: (state: any) => number | string; SearchResults: (state: any) => T[]; SearchStatus: (state: any) => Application.Types.Status; SearchFilters: (state: any) => Search.IFilter<T>[]; PagedResults: (state: any) => T[]; PagedStatus: (state: any) => Application.Types.Status; PagedFilters: (state: any) => Search.IFilter<T>[]; PagedSortField: (state: any) => keyof T; PagedAscending: (state: any) => boolean; CurrentPage: (state: any) => number; TotalPages: (state: any) => number; TotalRecords: (state: any) => number; } export {};