@prezly/theme-kit-react
Version:
Data layer and utility library for developing Prezly themes with NextJS
121 lines (119 loc) • 4.44 kB
JavaScript
'use client';
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { useCallback, useEffect, useReducer } from 'react';
var DEFAULT_RETRIES = 3;
function reduce(state, action) {
switch (action.type) {
case 'LOAD':
{
return _objectSpread(_objectSpread({}, state), {}, {
loading: true,
error: undefined,
tries: state.tries + 1
});
}
case 'LOADED':
{
var {
data,
offset: _offset,
total
} = action;
return _objectSpread(_objectSpread({}, state), {}, {
loading: false,
data: [...state.data.slice(0, _offset), ...data],
total,
tries: 0
});
}
case 'ERROR':
{
return _objectSpread(_objectSpread({}, state), {}, {
loading: false,
error: action.error
});
}
default:
return state;
}
}
function initReducer() {
var {
data,
total
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return {
error: undefined,
data: data !== null && data !== void 0 ? data : [],
loading: typeof data === 'undefined',
total,
tries: 0
};
}
export function useInfiniteLoading(loaderFn, initialData) {
var {
retryOnError = DEFAULT_RETRIES
} = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var [state, dispatch] = useReducer(reduce, initialData && Array.isArray(initialData) ? {
data: initialData
} : initialData, initReducer);
var {
loading,
tries,
error,
data,
total
} = state;
var offset = data.length;
var load = useCallback(/*#__PURE__*/_asyncToGenerator(function* () {
if (loading) return;
dispatch({
type: 'LOAD'
});
try {
var loaded = yield loaderFn(offset);
dispatch({
type: 'LOADED',
offset,
data: loaded.data,
total: loaded.total
});
} catch (loadingError) {
dispatch({
type: 'ERROR',
error: toError(loadingError)
});
}
}), [loading, offset, loaderFn]);
useEffect(() => {
if (data.length === 0 && (typeof total === 'undefined' || total > 0)) {
load();
}
}, []); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
if (error && tries <= retryOnError) {
load();
}
}, [error, tries, retryOnError, load]);
return {
load,
loading,
error,
data,
count: data.length,
total,
done: typeof total !== 'undefined' && data.length >= total
};
}
function toError(value) {
if (value instanceof Error) {
return value;
}
return new Error("Unknown error.");
}