@premieroctet/next-admin
Version:
Next-Admin provides a customizable and turnkey admin dashboard for applications built with Next.js and powered by the Prisma ORM. It aims to simplify the development process by providing a turnkey admin system that can be easily integrated into your proje
101 lines (100 loc) • 3.94 kB
JavaScript
;
var __webpack_require__ = {};
(()=>{
__webpack_require__.d = (exports1, definition)=>{
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
enumerable: true,
get: definition[key]
});
};
})();
(()=>{
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
__webpack_require__.r = (exports1)=>{
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
value: 'Module'
});
Object.defineProperty(exports1, '__esModule', {
value: true
});
};
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
default: ()=>hooks_useSearchPaginatedResource
});
const external_react_namespaceObject = require("react");
const ConfigContext_js_namespaceObject = require("../context/ConfigContext.js");
const useSearchPaginatedResource = ({ fieldName, initialOptions })=>{
const [isPending, setIsPending] = (0, external_react_namespaceObject.useState)(false);
const { apiBasePath, schema, resource } = (0, ConfigContext_js_namespaceObject.useConfig)();
const searchPage = (0, external_react_namespaceObject.useRef)(1);
const totalSearchedItems = (0, external_react_namespaceObject.useRef)(0);
const [allOptions, setAllOptions] = (0, external_react_namespaceObject.useState)(initialOptions ?? []);
const [hasNextPage, setHasNextPage] = (0, external_react_namespaceObject.useState)(false);
const runSearch = async (query, resetOptions = true)=>{
const perPage = 25;
const schemaResourceProperties = schema.definitions[resource].properties;
const schemaFieldEntry = Object.entries(schemaResourceProperties).find(([key])=>key === fieldName);
if (!schemaFieldEntry) return;
const [, schemaField] = schemaFieldEntry;
const model = schemaField.__nextadmin?.type;
try {
setIsPending(true);
const response = await fetch(`${apiBasePath}/options`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
originModel: resource,
property: fieldName,
model,
query,
page: searchPage.current,
perPage
})
});
if (response.ok) {
const responseJson = await response.json();
if (!responseJson.error) {
totalSearchedItems.current = responseJson.total;
setAllOptions((old)=>{
if (resetOptions) return responseJson.data;
return [
...old,
...responseJson.data
];
});
}
}
} finally{
setIsPending(false);
}
};
(0, external_react_namespaceObject.useEffect)(()=>{
if (allOptions?.length > 0) setHasNextPage(allOptions.length < totalSearchedItems.current);
}, [
allOptions
]);
return {
allOptions,
setAllOptions,
runSearch,
searchPage,
totalSearchedItems,
isPending,
hasNextPage
};
};
const hooks_useSearchPaginatedResource = useSearchPaginatedResource;
exports["default"] = __webpack_exports__["default"];
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
"default"
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
Object.defineProperty(exports, '__esModule', {
value: true
});