autumn-js
Version:
Autumn JS Library
53 lines (51 loc) • 1.16 kB
JavaScript
"use client";
import useSWR from "swr";
import { getEntityAction } from "../../../next/server/cusActions";
import { useAutumnContext } from "../AutumnContext";
const useEntityProvider = (entityId, params) => {
const { encryptedCustomerId } = useAutumnContext();
const queryKey = ["entity", entityId, params?.expand];
const fetchEntity = async () => {
if (!entityId) {
return null;
}
const { data: data2, error: error2 } = await getEntityAction({
encryptedCustomerId,
entityId,
params
});
if (error2) {
throw error2;
}
if (!data2) {
return null;
}
return data2;
};
const { data, error, isLoading, mutate } = useSWR(queryKey, fetchEntity, {
fallbackData: null,
onErrorRetry: (error2, key, config) => {
if (error2.code == "entity_not_found") {
return false;
}
return true;
}
});
if (!entityId) {
return {
entity: null,
isLoading: false,
error: null,
refetch: mutate
};
}
return {
entity: error ? null : data,
isLoading,
error,
refetch: mutate
};
};
export {
useEntityProvider
};