@adyen/adyen-platform-experience-web
Version:

40 lines (39 loc) • 1.15 kB
JavaScript
import { useRef as l, useReducer as p, useCallback as y, useEffect as g } from "../external/preact/hooks/dist/hooks.module.js";
import { boolOrTrue as u } from "../utils/value/bool.js";
function F({
fetchOptions: { keepPrevData: d, onSuccess: e, enabled: c } = { keepPrevData: !0 },
queryFn: s
}) {
const r = l(!1), a = {
error: void 0,
data: void 0,
isFetching: u(c)
}, f = (t, o) => {
switch (o.type) {
case "loading":
return { ...a, isFetching: !0, data: d ? t.data : void 0 };
case "fetched":
return { ...a, data: o.payload, isFetching: !1 };
case "error":
return { ...a, error: o.payload, isFetching: !1 };
default:
return t;
}
}, [h, i] = p(f, a), n = y(async () => {
i({ type: "loading" });
try {
if (r.current) return;
const t = await s();
e == null || e(t), i({ type: "fetched", payload: t });
} catch (t) {
if (r.current) return;
i({ type: "error", payload: t });
}
}, [i, s, e]);
return g(() => (r.current = !1, u(c) && n(), () => {
r.current = !0;
}), [c, n]), h;
}
export {
F as useFetch
};