forked from UKSOURCE/cms.lams
feat(cms): enhance content editors with dynamic UI configs and validation
Implement a more flexible, configuration-driven approach for CMS editors across accreditation, admissions, and partnerships modules. - Move UI labels, help texts, and field limits from hardcoded views to `editorUi` configurations in config files. - Add server-side and client-side validation to prevent duplicate category tabs in accreditation and partnerships editors. - Refactor partnership and admission views to dynamically render tabs and fields based on the provided configuration. - Update field length constraints and default values across multiple content editors to better align with frontend requirements. - Improve the admissions calculator editor with dynamic field configurations and default value fallbacks.
This commit is contained in:
@@ -9,6 +9,38 @@ const diffObject = require("../audit/diffObject");
|
||||
const { ensureUniqueIds } = require("../utils/contentEditorIds");
|
||||
const { ICON_OPTIONS } = require("../utils/contentEditors/sharedFields");
|
||||
|
||||
function getAdmissionsEditorUi() {
|
||||
return admissionsConfig.editorUi || {};
|
||||
}
|
||||
|
||||
function getDefaultCalculatorOptionValues() {
|
||||
return getAdmissionsEditorUi().calculator?.defaultOption || {};
|
||||
}
|
||||
|
||||
function getCalculatorOptionFieldConfig(fieldKey) {
|
||||
const calculatorTab = (admissionsConfig.tabs || []).find((tab) => tab.key === "calculator");
|
||||
const calculatorFields = calculatorTab?.schema?.fields || [];
|
||||
const optionsField = calculatorFields.find((field) => field.key === "options");
|
||||
const optionItemFields = optionsField?.itemSchema?.fields || [];
|
||||
|
||||
return optionItemFields.find((field) => field.key === fieldKey) || {};
|
||||
}
|
||||
|
||||
function hasDuplicateCalculatorOptionLabel(options, currentOptionId, nextLabel) {
|
||||
const normalizedLabel = String(nextLabel || "").trim().toLowerCase();
|
||||
if (!normalizedLabel) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (options || []).some((option) => {
|
||||
if (String(option?.id || "") === String(currentOptionId || "")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return String(option?.label || "").trim().toLowerCase() === normalizedLabel;
|
||||
});
|
||||
}
|
||||
|
||||
function normalizePositiveAmount(value, fallback = "1") {
|
||||
const match = String(value || "").match(/\d[\d,]*/);
|
||||
const numericValue = Number((match ? match[0] : "").replace(/,/g, ""));
|
||||
@@ -17,18 +49,25 @@ function normalizePositiveAmount(value, fallback = "1") {
|
||||
|
||||
function normalizeCalculatorOption(option, index, calculator) {
|
||||
const source = typeof option === "string" ? { label: option } : { ...(option || {}) };
|
||||
const defaultOption = getDefaultCalculatorOptionValues();
|
||||
const labelMaxLength = getCalculatorOptionFieldConfig("label").maxLength || 12;
|
||||
|
||||
return {
|
||||
...source,
|
||||
label: String(source.label || source.title || `Option ${index + 1}`).slice(0, 12),
|
||||
paceLabel: String(source.paceLabel || calculator.paceLabel || "Target Pace"),
|
||||
minPaceLabel: String(source.minPaceLabel || calculator.minPaceLabel || "Relaxed"),
|
||||
maxPaceLabel: String(source.maxPaceLabel || calculator.maxPaceLabel || "Accelerated"),
|
||||
resultLabel: String(source.resultLabel || calculator.resultLabel || "Estimated Monthly Payment"),
|
||||
monthlyAmount: normalizePositiveAmount(source.monthlyAmount || calculator.monthlyAmount || "299", "299"),
|
||||
monthlySuffix: String(source.monthlySuffix || calculator.monthlySuffix || "/mo"),
|
||||
noteIcon: String(source.noteIcon || calculator.noteIcon || "fa-bolt"),
|
||||
note: String(source.note || calculator.note || ""),
|
||||
label: String(source.label || source.title || `Option ${index + 1}`).slice(0, labelMaxLength),
|
||||
paceLabel: String(source.paceLabel || calculator.paceLabel || defaultOption.paceLabel || "Target Pace"),
|
||||
minPaceLabel: String(source.minPaceLabel || calculator.minPaceLabel || defaultOption.minPaceLabel || "Relaxed"),
|
||||
maxPaceLabel: String(source.maxPaceLabel || calculator.maxPaceLabel || defaultOption.maxPaceLabel || "Accelerated"),
|
||||
resultLabel: String(
|
||||
source.resultLabel || calculator.resultLabel || defaultOption.resultLabel || "Estimated Monthly Payment",
|
||||
),
|
||||
monthlyAmount: normalizePositiveAmount(
|
||||
source.monthlyAmount || calculator.monthlyAmount || defaultOption.monthlyAmount || "299",
|
||||
String(defaultOption.monthlyAmount || "299"),
|
||||
),
|
||||
monthlySuffix: String(source.monthlySuffix || calculator.monthlySuffix || defaultOption.monthlySuffix || "/mo"),
|
||||
noteIcon: String(source.noteIcon || calculator.noteIcon || defaultOption.noteIcon || "fa-bolt"),
|
||||
note: String(source.note || calculator.note || defaultOption.note || ""),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -174,12 +213,41 @@ controller.editCalculatorOption = async function editCalculatorOption(req, res)
|
||||
const frontendUrl = process.env.FRONTEND_URL || "http://localhost:3000";
|
||||
const backendUrl =
|
||||
process.env.BACKEND_URL ?? `${req.protocol}://${req.get("host")}`;
|
||||
const fieldKeys = [
|
||||
"label",
|
||||
"paceLabel",
|
||||
"minPaceLabel",
|
||||
"maxPaceLabel",
|
||||
"resultLabel",
|
||||
"monthlyAmount",
|
||||
"monthlySuffix",
|
||||
"noteIcon",
|
||||
"note",
|
||||
];
|
||||
const fieldConfig = fieldKeys.reduce((acc, key) => {
|
||||
acc[key] = getCalculatorOptionFieldConfig(key);
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
return res.render("admin/admissions/calculator-option", {
|
||||
layout: "layouts/main",
|
||||
title: `Edit ${option.label}`,
|
||||
subtitle: "Update the calculator option details",
|
||||
option,
|
||||
existingOptionLabels: data.calculator.options
|
||||
.filter((item) => item.id !== optionId)
|
||||
.map((item) => item.label)
|
||||
.filter(Boolean),
|
||||
fieldLimits: {
|
||||
label: fieldConfig.label.maxLength || 12,
|
||||
paceLabel: fieldConfig.paceLabel.maxLength || 20,
|
||||
minPaceLabel: fieldConfig.minPaceLabel.maxLength || 7,
|
||||
maxPaceLabel: fieldConfig.maxPaceLabel.maxLength || 7,
|
||||
resultLabel: fieldConfig.resultLabel.maxLength || 40,
|
||||
monthlySuffix: fieldConfig.monthlySuffix.maxLength || 10,
|
||||
note: fieldConfig.note.maxLength || 60,
|
||||
},
|
||||
fieldConfig,
|
||||
iconOptions: ICON_OPTIONS,
|
||||
editorConfig: admissionsConfig,
|
||||
previewUrl: `${frontendUrl}${admissionsConfig.previewPath}`,
|
||||
@@ -207,9 +275,15 @@ controller.updateCalculatorOption = async function updateCalculatorOption(req, r
|
||||
return req.session.save(() => res.redirect("/admin/admissions?tab=calculator"));
|
||||
}
|
||||
|
||||
const nextLabel = String(req.body.label || "").trim().slice(0, getCalculatorOptionFieldConfig("label").maxLength || 12);
|
||||
if (hasDuplicateCalculatorOptionLabel(payload.calculator.options, optionId, nextLabel)) {
|
||||
req.flash("error_msg", `Option label "${nextLabel}" already exists. Please use a unique label.`);
|
||||
return req.session.save(() => res.redirect(`/admin/admissions/calculator/${optionId}`));
|
||||
}
|
||||
|
||||
payload.calculator.options[optionIndex] = {
|
||||
...payload.calculator.options[optionIndex],
|
||||
label: String(req.body.label || "").trim().slice(0, 12),
|
||||
label: nextLabel,
|
||||
paceLabel: String(req.body.paceLabel || "").trim(),
|
||||
minPaceLabel: String(req.body.minPaceLabel || "").trim(),
|
||||
maxPaceLabel: String(req.body.maxPaceLabel || "").trim(),
|
||||
|
||||
Reference in New Issue
Block a user