UNPKG

@adyen/adyen-platform-experience-web

Version:

![Platform Experience header](https://github.com/Adyen/adyen-platform-experience-web/assets/7926613/18094965-9e01-450e-8dc9-ea84e6b22c2b)

78 lines (77 loc) 2.93 kB
import { useState as l, useRef as y, useMemo as R, useCallback as L, useEffect as M } from "../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js"; import H from "./usePageLimit.js"; import K from "./useCursorPagination.js"; import Q from "./useOffsetPagination.js"; import W from "./usePaginatedRecordsFilters.js"; import Z from "../../../../hooks/useBooleanState.js"; import ee from "../../../../hooks/useMounted.js"; import { PaginationType as te, PageNeighbour as N } from "../types.js"; import { EMPTY_OBJECT as re } from "../../../../utils/value/constants.js"; import { boolOrFalse as w } from "../../../../utils/value/bool.js"; const se = ["hasNext", "hasPrevious"], _ = (e) => { const t = Object.getOwnPropertyNames(e); return !se.some((r) => t.includes(r)); }, oe = (e, t = "data") => { const r = e[t]; if (_(e)) { const a = Object.fromEntries( Object.entries(e._links).map(([n, s]) => [n, s.cursor]) ); return { records: r, paginationData: a }; } throw new TypeError("MALFORMED_PAGINATED_DATA"); }, ae = (e, t = "data") => { const r = e[t]; if (!_(e)) { const { hasNext: a, hasPrevious: n } = e, s = { [N.NEXT]: w(a), [N.PREV]: w(n) }; return { records: r, paginationData: s }; } throw new TypeError("MALFORMED_PAGINATED_DATA"); }, Re = ({ dataField: e = "data", fetchRecords: t, filterParams: r = re, initialFiltersSameAsDefault: a = !0, initialize: n, onFiltersChanged: s, pagination: D, preferredLimit: C, preferredLimitOptions: b, enabled: i }) => { const [v, $] = l([]), [j, u] = Z(i !== void 0 ? i : !0), [x, E] = l(), [I, S] = l(C), O = ee(), p = y(!0), f = y(1), A = W(r, a), { limit: m, limitOptions: k } = H({ preferredLimit: I, preferredLimitOptions: b }), { defaultFilters: ne, filters: o, updateFilters: q, ...B } = A, [G, z] = R( () => D === te.CURSOR ? [oe, K] : [ae, Q], [D] ), J = L((g) => S(g), []), { goto: d, page: U, pages: V, resetPagination: T, ...X } = z( L( async ({ page: g, ...Y }, h) => { try { if (E(void 0), f.current = g, !O.current || u(!0)) return; const c = await t({ ...Y, ...o }, h), { records: P, paginationData: F } = G(c, e); return p.current && (n?.([P, F], A), p.current = !1), O.current && ($(P), u(!1)), { ...F, size: P?.length }; } catch (c) { if (h?.aborted) return; u(!1), E(c), console.error(c); } }, // eslint-disable-next-line react-hooks/exhaustive-deps [t, o, m] ), m ); return R(() => { p.current = !0; }, [r]), R(() => { T(), f.current = 1; }, [o, m, T]), M(() => { i && d(f.current); }, [d, i]), M(() => { s?.(o); }, [o, s]), { error: x, fetching: j, filters: o, goto: d, limitOptions: k, page: U, pages: V, records: v, updateFilters: q, updateLimit: J, ...B, ...X }; }; export { Re as default };