Fix JSON parsing for tags and technologies arrays from Directus
Co-authored-by: denshooter <44590296+denshooter@users.noreply.github.com>
This commit is contained in:
@@ -549,6 +549,21 @@ export async function getProjects(
|
|||||||
proj.translations?.find((t: any) => t.languages_code?.code === directusLocale) ||
|
proj.translations?.find((t: any) => t.languages_code?.code === directusLocale) ||
|
||||||
proj.translations?.[0] ||
|
proj.translations?.[0] ||
|
||||||
{};
|
{};
|
||||||
|
|
||||||
|
// Parse JSON string fields if needed
|
||||||
|
const parseTags = (tags: any) => {
|
||||||
|
if (!tags) return [];
|
||||||
|
if (Array.isArray(tags)) return tags;
|
||||||
|
if (typeof tags === 'string') {
|
||||||
|
try {
|
||||||
|
return JSON.parse(tags);
|
||||||
|
} catch {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: proj.id,
|
id: proj.id,
|
||||||
slug: proj.slug,
|
slug: proj.slug,
|
||||||
@@ -557,8 +572,8 @@ export async function getProjects(
|
|||||||
content: trans.content,
|
content: trans.content,
|
||||||
category: proj.category,
|
category: proj.category,
|
||||||
difficulty: proj.difficulty,
|
difficulty: proj.difficulty,
|
||||||
tags: proj.tags || [],
|
tags: parseTags(proj.tags),
|
||||||
technologies: proj.technologies || [],
|
technologies: parseTags(proj.technologies),
|
||||||
challenges: proj.challenges,
|
challenges: proj.challenges,
|
||||||
lessons_learned: proj.lessons_learned,
|
lessons_learned: proj.lessons_learned,
|
||||||
future_improvements: proj.future_improvements,
|
future_improvements: proj.future_improvements,
|
||||||
@@ -567,7 +582,7 @@ export async function getProjects(
|
|||||||
image_url: proj.image_url,
|
image_url: proj.image_url,
|
||||||
demo_video_url: proj.demo_video,
|
demo_video_url: proj.demo_video,
|
||||||
performance_metrics: proj.performance_metrics,
|
performance_metrics: proj.performance_metrics,
|
||||||
screenshots: proj.screenshots || [],
|
screenshots: parseTags(proj.screenshots),
|
||||||
featured: proj.featured === 1 || proj.featured === true,
|
featured: proj.featured === 1 || proj.featured === true,
|
||||||
published: proj.status === 'published',
|
published: proj.status === 'published',
|
||||||
created_at: proj.date_created,
|
created_at: proj.date_created,
|
||||||
|
|||||||
Reference in New Issue
Block a user