forked from UKSOURCE/cms.lams
feat(cms): refactor page content management and implement detailed editors
Refactor the CMS page content system to use a more modular configuration-driven approach. This replaces the monolithic `pageContentConfig.js` with individual configuration files for each page and introduces a shared field utility to standardize editor components. Key changes: - Implement a generic `_renderSingletonPageView` helper to reduce duplication across controllers. - Enhance `_createPageContentController` with `normalizeForEditor`, `normalizeForApi`, and `preparePayload` hooks for custom data transformation. - Create dedicated configuration and view structures for Partnerships, History, Accreditation, Admissions, and Policies pages. - Add a specialized section editor for Policies to manage complex nested content. - Improve the frontend `page-content-editor.js` with support for visibility logic, auto-sequencing for arrays, and URL synchronization for active tabs. - Update data JSON files to align with the new schema.
This commit is contained in:
@@ -8,12 +8,16 @@ function createPageContentController({
|
||||
modelName,
|
||||
auditAction,
|
||||
editorConfig,
|
||||
normalizeForEditor,
|
||||
normalizeForApi,
|
||||
preparePayload,
|
||||
}) {
|
||||
return {
|
||||
async index(req, res) {
|
||||
try {
|
||||
const doc = await model.getSingle();
|
||||
const data = doc.toObject();
|
||||
const rawData = doc.toObject();
|
||||
const data = normalizeForEditor ? normalizeForEditor(rawData, req) : rawData;
|
||||
const frontendUrl = process.env.FRONTEND_URL || "http://localhost:3000";
|
||||
const backendUrl =
|
||||
process.env.BACKEND_URL ?? `${req.protocol}://${req.get("host")}`;
|
||||
@@ -40,7 +44,7 @@ function createPageContentController({
|
||||
|
||||
async update(req, res) {
|
||||
try {
|
||||
const payload =
|
||||
const rawPayload =
|
||||
typeof req.body.pageJson === "string"
|
||||
? JSON.parse(req.body.pageJson)
|
||||
: req.body.pageJson || {};
|
||||
@@ -48,6 +52,9 @@ function createPageContentController({
|
||||
const activeTab = req.body.activeTab || editorConfig.tabs[0].key;
|
||||
const doc = await model.getSingle();
|
||||
const beforeData = JSON.parse(JSON.stringify(doc.toObject()));
|
||||
const payload = preparePayload
|
||||
? preparePayload(rawPayload, { req, doc, beforeData })
|
||||
: rawPayload;
|
||||
|
||||
doc.set(payload);
|
||||
Object.keys(payload).forEach((key) => doc.markModified(key));
|
||||
@@ -98,7 +105,8 @@ function createPageContentController({
|
||||
const rawData = doc.toObject();
|
||||
const backendUrl =
|
||||
process.env.BACKEND_URL ?? `${req.protocol}://${req.get("host")}`;
|
||||
const processed = addBaseUrlToImages(rawData, backendUrl);
|
||||
const normalized = normalizeForApi ? normalizeForApi(rawData, req) : rawData;
|
||||
const processed = addBaseUrlToImages(normalized, backendUrl);
|
||||
return res.json(processed);
|
||||
} catch (error) {
|
||||
console.error(`${editorConfig.key} api error:`, error);
|
||||
|
||||
Reference in New Issue
Block a user