UNPKG

@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

171 lines (170 loc) 7.5 kB
import external_lodash_clonedeep_default from "lodash.clonedeep"; import { getSchema, initGlobals } from "./globals.mjs"; import { getClientActionsComponents, getCustomInputs } from "./options.mjs"; import { getDataItem, getMappedDataList, mapModelFilters } from "./prisma.mjs"; import { applyVisiblePropertiesInSchema, getEnableToExecuteActions, getModelIdProperty, getResourceFromParams, getResourceIdFromParam, getResources, getToStringForModel, transformSchema } from "./server.mjs"; import { extractSerializable } from "./tools.mjs"; async function getPropsFromParams({ params, searchParams, options, prisma, isAppDir = true, locale, getMessages, basePath, apiBasePath }) { const { resource, resources, resourcesTitles, resourcesIdProperty, customPages, title, sidebar, resourcesIcons, externalLinks } = await getMainLayoutProps({ basePath, apiBasePath, options, params, isAppDir }); const clientOptions = extractSerializable(options); let defaultProps = { resources, basePath, apiBasePath, isAppDir, customPages, resourcesTitles, resourcesIdProperty, options: clientOptions, title, sidebar, resourcesIcons, externalLinks, locale: locale ?? null, schema: getSchema() }; if (!params) return defaultProps; if (!resource) return defaultProps; const actions = options?.model?.[resource]?.actions?.map((action)=>{ const { action: _, ...actionRest } = action; return actionRest; }); if (getMessages) { const messages = await getMessages(locale); const dottedProperty = {}; const dot = (obj, prefix = "")=>{ Object.entries(obj).forEach(([key, value])=>{ if ("object" == typeof value) dot(value, `${prefix}${key}.`); else dottedProperty[`${prefix}${key}`] = value; }); }; dot(messages); defaultProps = { ...defaultProps, translations: dottedProperty }; } const dialogActionsComponents = isAppDir ? getClientActionsComponents(resource, options) : null; switch(params.length){ case 1: { const { data, total, error, rawData } = await getMappedDataList({ prisma, resource, options, searchParams: new URLSearchParams(searchParams), context: { locale }, appDir: isAppDir }); if (options?.model?.[resource]?.list?.filters) clientOptions.model[resource].list.filters = await mapModelFilters(options.model[resource].list.filters); const dataIds = data.map((item)=>item[getModelIdProperty(resource)].value); const fullfilledAction = await getEnableToExecuteActions(resource, prisma, dataIds, actions); let serializedActions = extractSerializable(external_lodash_clonedeep_default(fullfilledAction), isAppDir); return { ...defaultProps, resource, data, total, error: error ?? searchParams?.error ?? null, actions: serializedActions, dialogComponents: dialogActionsComponents, rawData: extractSerializable(rawData), listFilterOptions: clientOptions?.model?.[resource]?.list?.filters ?? null }; } case 2: { const resourceId = getResourceIdFromParam(params[1], resource); const edit = options?.model?.[resource]?.edit; let deepCopySchema = await transformSchema(resource, edit, options)(external_lodash_clonedeep_default(getSchema())); const customInputs = isAppDir ? getCustomInputs(resource, options) : null; if (void 0 !== resourceId) { const { data, relationshipsRawData } = await getDataItem({ prisma, resource, resourceId, options, locale, isAppDir }); const toStringFunction = getToStringForModel(options?.model?.[resource]); const slug = toStringFunction ? toStringFunction(data) : resourceId.toString(); applyVisiblePropertiesInSchema(resource, edit, data, deepCopySchema); const dataId = data[getModelIdProperty(resource)]; const fullfilledAction = await getEnableToExecuteActions(resource, prisma, [ dataId ], actions); let serializedActions = extractSerializable(external_lodash_clonedeep_default(fullfilledAction), isAppDir); return { ...defaultProps, resource, data, slug, schema: deepCopySchema, customInputs, actions: serializedActions, dialogComponents: dialogActionsComponents, relationshipsRawData: extractSerializable(relationshipsRawData, isAppDir) }; } if ("new" === params[1]) return { ...defaultProps, resource, schema: deepCopySchema, customInputs }; return defaultProps; } default: return defaultProps; } } const getMainLayoutProps = async ({ basePath, apiBasePath, options, params, isAppDir = true })=>{ if (void 0 !== params && !Array.isArray(params)) throw new Error("`params` parameter in `getMainLayoutProps` should be an array of strings."); await initGlobals(); const resources = getResources(options); const resource = getResourceFromParams(params ?? [], resources); const resourcesIdProperty = resources.reduce((acc, resource)=>{ acc[resource] = getModelIdProperty(resource); return acc; }, {}); const customPages = Object.keys(options?.pages ?? {}).map((path)=>({ title: options?.pages[path].title ?? path, path: path, icon: options?.pages[path].icon })); const resourcesTitles = resources.reduce((acc, resource)=>{ acc[resource] = options?.model?.[resource]?.title ?? resource; return acc; }, {}); const resourcesIcons = resources.reduce((acc, resource)=>{ if (!options?.model?.[resource]?.icon) return acc; acc[resource] = options.model?.[resource]?.icon; return acc; }, {}); return { resources, resource, basePath, apiBasePath, customPages, resourcesTitles, isAppDir, title: isAppDir ? options?.title ?? "Admin" : null, sidebar: options?.sidebar, resourcesIcons, externalLinks: options?.externalLinks, options: extractSerializable(options, isAppDir), resourcesIdProperty: resourcesIdProperty, schema: getSchema() }; }; export { getMainLayoutProps, getPropsFromParams };