{"version":3,"sources":["src/app/store/common/paginated-list-entity.ts","src/app/store/common/pagination.ts"],"sourcesContent":["import { StateContext, StateOperator } from '@ngxs/store';\nimport { patch } from '@ngxs/store/operators';\nimport { Observable, catchError, of, tap } from 'rxjs';\nimport {\n IdentifiableEntities,\n IteratableListEntities,\n ListEntities,\n ListEntitiesToIterable,\n} from './list-entity';\nimport {\n IPagination,\n PaginatedResponse,\n PaginationOptions,\n PaginationState,\n} from './pagination';\n\nexport interface PaginatedListEntitiesState\n extends ListEntities {\n pagination: P;\n}\n\nexport interface IterablePaginatedListEntitiesState\n extends IteratableListEntities {\n pagination: P;\n}\n\nexport function TransformPaginatedEntitiesToIterableEntities<\n T,\n P = PaginationState,\n>(\n state: PaginatedListEntitiesState\n): IterablePaginatedListEntitiesState {\n return {\n ...state,\n entities: ListEntitiesToIterable(state).entities,\n };\n}\nexport class PaginatedListEntities {\n private pagination: P;\n private key: keyof T;\n\n constructor(key: keyof T, pagination: P) {\n this.pagination = pagination;\n this.key = key;\n }\n\n loadEntititesPaginated(\n ctx: StateContext,\n paginationOptions: PaginationOptions,\n observable: Observable>,\n append = false\n ) {\n ctx.setState(\n setFields(this.key, {\n isLoading: true,\n loadingError: undefined,\n pagination: patch(\n this.pagination.optionsToState(paginationOptions)\n ),\n ...(append ? {} : { entities: {} }),\n })\n );\n\n return observable.pipe(\n tap((response: PaginatedResponse>) => {\n ctx.setState(\n setFields(this.key, {\n isLoading: false,\n ...this.pagination.responseToState(\n ctx.getState()[this.key] as PaginatedListEntitiesState,\n response\n ),\n })\n );\n }),\n catchError((error: any) => {\n ctx.setState(\n setFields(this.key, {\n isLoading: false,\n loadingError: error.toString(),\n })\n );\n return of();\n })\n );\n }\n}\n\nexport type PaginatedListEntityFields> = {\n [K in keyof PaginatedListEntitiesState]?:\n | PaginatedListEntitiesState[K]\n | StateOperator;\n};\n\nfunction setFields(\n key: keyof T,\n fields: PaginatedListEntityFields\n): StateOperator {\n return patch({\n [key]: patch>({\n ...fields,\n }),\n });\n}\n","import { patch } from '@ngxs/store/operators';\nimport {\n DefaultListEntitiesState,\n IdentifiableEntities,\n ListEntities,\n} from './list-entity';\nimport {\n PaginatedListEntitiesState,\n PaginatedListEntityFields,\n} from './paginated-list-entity';\n\nexport type PaginatedResponse =\n | CursorPaginatedResponse\n | OffsetPaginatedRespose;\n\nexport interface CursorPaginatedResponse {\n items: IdentifiableEntities;\n totalSize: number;\n nextPageToken: string;\n}\n\nexport interface OffsetPaginatedRespose {\n items: IdentifiableEntities;\n totalSize: number;\n}\n\nexport type PaginationOptions =\n | CursorPaginationOptions\n | OffsetPaginationOptions;\n\nexport interface CursorPaginationOptions {\n pageToken?: string;\n pageSize: number;\n}\n\nexport interface OffsetPaginationOptions {\n page: number;\n pageSize: number;\n}\n\nexport type PaginationState = CursorPaginationState | OffsetPaginationState;\n\nexport interface CursorPaginationState {\n totalRecords: number;\n pageSize: number;\n nextPageToken: string;\n pageToken: string;\n}\n\nexport interface OffsetPaginationState {\n totalRecords: number;\n pageSize: number;\n page: number;\n}\n\nexport type Pagination = CursorPagination | OffsetPagination;\n\nexport interface IPagination {\n optionsToState(options: PaginationOptions): object;\n responseToState(\n state: PaginatedListEntitiesState,\n response: PaginatedResponse\n ): PaginatedListEntityFields;\n}\n\nexport class CursorPagination implements IPagination {\n static GetDefaultState(): PaginatedListEntitiesState<\n T,\n CursorPaginationState\n > {\n return {\n ...DefaultListEntitiesState,\n pagination: {\n pageSize: 10,\n nextPageToken: '',\n totalRecords: 0,\n pageToken: '',\n },\n };\n }\n\n responseToState(\n state: PaginatedListEntitiesState,\n response: CursorPaginatedResponse\n ): PaginatedListEntityFields {\n return {\n entities:\n state.pagination.pageToken === ''\n ? response.items\n : patch(response.items),\n pagination: patch({\n totalRecords: response.totalSize,\n nextPageToken: response.nextPageToken,\n }),\n };\n }\n\n optionsToState(options: CursorPaginationOptions) {\n return options;\n }\n}\n\nexport class OffsetPagination implements IPagination {\n static GetDefaultState(\n defaultPageSize = 10\n ): PaginatedListEntitiesState {\n return {\n ...DefaultListEntitiesState,\n pagination: {\n page: 1,\n pageSize: defaultPageSize,\n totalRecords: 0,\n },\n };\n }\n\n responseToState(\n state: PaginatedListEntitiesState,\n response: OffsetPaginatedRespose\n ): PaginatedListEntityFields> {\n return {\n entities: response.items,\n pagination: patch({\n totalRecords: response.totalSize,\n }),\n };\n }\n\n optionsToState(options: OffsetPaginationOptions) {\n return options;\n }\n}\n"],"names":["TransformPaginatedEntitiesToIterableEntities","state","__spreadProps","__spreadValues","entities","ListEntitiesToIterable","PaginatedListEntities","constructor","key","pagination","loadEntititesPaginated","ctx","paginationOptions","observable","append","setState","setFields","isLoading","loadingError","undefined","patch","optionsToState","pipe","tap","response","responseToState","getState","catchError","error","toString","of","fields","CursorPagination","GetDefaultState","__spreadProps","__spreadValues","DefaultListEntitiesState","pagination","pageSize","nextPageToken","totalRecords","pageToken","responseToState","state","response","entities","items","patch","totalSize","optionsToState","options","OffsetPagination","defaultPageSize","page"],"mappings":";;6HA0BM,SAAUA,EAIdC,EAAuC,CAEvC,OAAOC,EAAAC,EAAA,GACFF,GADE,CAELG,SAAUC,EAAuBJ,CAAK,EAAEG,UAE5C,CACM,IAAOE,EAAP,KAA4B,CAIhCC,YAAYC,EAAcC,EAAa,CACrC,KAAKA,WAAaA,EAClB,KAAKD,IAAMA,CACb,CAEAE,uBACEC,EACAC,EACAC,EACAC,EAAS,GAAK,CAEdH,OAAAA,EAAII,SACFC,EAAU,KAAKR,IAAKL,EAAA,CAClBc,UAAW,GACXC,aAAcC,OACdV,WAAYW,EACV,KAAKX,WAAWY,eAAeT,CAAiB,CAAC,GAE/CE,EAAS,CAAA,EAAK,CAAEV,SAAU,CAAA,CAAE,EACjC,CAAC,EAGGS,EAAWS,KAChBC,EAAKC,GAA0D,CAC7Db,EAAII,SACFC,EAAU,KAAKR,IAAKL,EAAA,CAClBc,UAAW,IACR,KAAKR,WAAWgB,gBACjBd,EAAIe,SAAQ,EAAG,KAAKlB,GAAG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