From edee8c2b75c2d62f908f13b2fd8548594ba7e4d7 Mon Sep 17 00:00:00 2001 From: Vishva Date: Tue, 4 Aug 2026 13:46:05 +0530 Subject: [PATCH] feat: add authorName and editorName attributes to Strapi blog schema for dynamic author display --- server/strapi.js | 12 ++++++++++-- src/components/BlogDetailPage.jsx | 6 +++++- .../api/blog/content-types/blog/lifecycles.ts | 16 +++++++++------- .../src/api/blog/content-types/blog/schema.json | 6 ++++++ 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/server/strapi.js b/server/strapi.js index 04a0552..33278b9 100644 --- a/server/strapi.js +++ b/server/strapi.js @@ -81,9 +81,17 @@ export async function getStrapiBlogs() { date: dateStr, image: coverUrl, slug: b.slug, - author: typeof b.author === 'string' ? b.author : (b.author?.firstname ? `${b.author.firstname} ${b.author.lastname || ''}`.trim() : (b.author?.username || 'Dr. Arvindh Ramachandran')), + author: (typeof b.authorName === 'string' && b.authorName.trim()) + ? b.authorName.trim() + : (typeof b.author === 'string' && b.author.trim() + ? b.author.trim() + : (b.author?.firstname ? `${b.author.firstname} ${b.author.lastname || ''}`.trim() : (b.author?.username || 'Cavin Editorial Team'))), authorDesignation: b.authorDesignation || 'Chief AI Architect & Head of Data Engineering', - editor: typeof b.editor === 'string' ? b.editor : (b.editor?.firstname ? `${b.editor.firstname} ${b.editor.lastname || ''}`.trim() : (b.editor?.username || null)), + editor: (typeof b.editorName === 'string' && b.editorName.trim()) + ? b.editorName.trim() + : (typeof b.editor === 'string' && b.editor.trim() + ? b.editor.trim() + : (b.editor?.firstname ? `${b.editor.firstname} ${b.editor.lastname || ''}`.trim() : (b.editor?.username || null))), editorDesignation: b.editorDesignation || null, tags: b.tags || [], metaTitle: b.metaTitle || null, diff --git a/src/components/BlogDetailPage.jsx b/src/components/BlogDetailPage.jsx index ccd666e..20746e8 100644 --- a/src/components/BlogDetailPage.jsx +++ b/src/components/BlogDetailPage.jsx @@ -145,7 +145,11 @@ export default function BlogDetailPage({ blog, allBlogs = [], isMobile, onNaviga const bgPatternUrl = assetUrl('/assets/Background pattern About Us Hero section.svg'); const spotlightUrl = assetUrl('/assets/Spotlight vector.svg'); - const authorName = typeof blog.author === 'string' ? blog.author : (blog.author?.firstname ? `${blog.author.firstname} ${blog.author.lastname || ''}`.trim() : 'Purushothaman Gopalan'); + const authorName = (typeof blog.authorName === 'string' && blog.authorName.trim()) + ? blog.authorName.trim() + : (typeof blog.author === 'string' && blog.author.trim() + ? blog.author.trim() + : (blog.author?.firstname ? `${blog.author.firstname} ${blog.author.lastname || ''}`.trim() : 'Cavin Editorial Team')); const authorRole = typeof blog.authorDesignation === 'string' ? blog.authorDesignation : 'Chief AI Architect & Head of Data Engineering'; const authorAvatar = '/assets/arvindh_agentic_ai.jpg'; const relatedArticles = allBlogs.filter(b => b.id !== blog.id).slice(0, 2); diff --git a/strapi-cms/src/api/blog/content-types/blog/lifecycles.ts b/strapi-cms/src/api/blog/content-types/blog/lifecycles.ts index f2dd648..ae7add8 100644 --- a/strapi-cms/src/api/blog/content-types/blog/lifecycles.ts +++ b/strapi-cms/src/api/blog/content-types/blog/lifecycles.ts @@ -14,12 +14,12 @@ export default { const user = ctx?.state?.user; if (user) { - // Auto-fill author relation if not explicitly set by user if (!data.author) { data.author = user.id; } - - // Auto-fill author designation based on logged-in account + if (!data.authorName) { + data.authorName = `${user.firstname || ''} ${user.lastname || ''}`.trim() || user.username || user.email; + } if (!data.authorDesignation && user.email) { data.authorDesignation = designationMap[user.email] || `${user.firstname || ''} ${user.lastname || ''}`.trim(); } @@ -32,22 +32,24 @@ export default { const user = ctx?.state?.user; if (user && data) { - // Auto-fill author relation if empty if (!data.author) { data.author = user.id; } - - // Auto-fill author designation if empty + if (!data.authorName && user) { + data.authorName = `${user.firstname || ''} ${user.lastname || ''}`.trim() || user.username || user.email; + } if (!data.authorDesignation && user.email) { data.authorDesignation = designationMap[user.email] || `${user.firstname || ''} ${user.lastname || ''}`.trim(); } - // Auto-fill editor details if an Editor/Admin edits or approves const isEditor = user.roles?.some((r: any) => r.code === 'strapi-editor' || r.code === 'strapi-super-admin'); if (isEditor) { if (!data.editor) { data.editor = user.id; } + if (!data.editorName && user) { + data.editorName = `${user.firstname || ''} ${user.lastname || ''}`.trim() || user.username; + } if (!data.editorDesignation && user.email) { data.editorDesignation = designationMap[user.email] || 'Senior Editor'; } diff --git a/strapi-cms/src/api/blog/content-types/blog/schema.json b/strapi-cms/src/api/blog/content-types/blog/schema.json index cf13c7d..77f7da9 100644 --- a/strapi-cms/src/api/blog/content-types/blog/schema.json +++ b/strapi-cms/src/api/blog/content-types/blog/schema.json @@ -60,6 +60,9 @@ "default": "Draft", "required": true }, + "authorName": { + "type": "string" + }, "author": { "type": "relation", "relation": "oneToOne", @@ -68,6 +71,9 @@ "authorDesignation": { "type": "string" }, + "editorName": { + "type": "string" + }, "editor": { "type": "relation", "relation": "oneToOne",