Skip to content

Commit

Permalink
bugfix: Added null checking to mapToProjects
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisv09 committed Jul 3, 2024
1 parent 27ba9bd commit e677d89
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions web/src/utils/mapToProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@ import type { Project } from "@/types/types";
const STRAPI_URL = import.meta.env.STRAPI_URL;

export const extractProjectData = (json: any): Project[] => {
// Check if json is null or undefined before proceeding
if (!json) return [];

return json.map((projectData: any) => {
const attributes = projectData.attributes;
const attributes = projectData?.attributes;

// Provide default values or checks for potentially null properties
const project: Project = {
id: projectData.id,
title: attributes.Name,
description: attributes.Description.map(
id: projectData?.id ?? '',
title: attributes?.Name ?? 'No Title',
description: attributes?.Description?.map(
(desc: { children: { text: any }[] }) =>
desc.children.map((child: { text: any }) => child.text).join(""),
).join("\n"),
images: attributes.Images.data.map(
).join("\n") ?? 'No Description',
images: attributes?.Images?.data?.map(
(img: { attributes: { formats: { small: { url: string } } } }) =>
STRAPI_URL + img.attributes.formats.small.url,
),
technologies: attributes.technologies.data.map(
) ?? [],
technologies: attributes?.technologies?.data?.map(
(tech: { attributes: { Name: string } }) => tech.attributes.Name,
),
demoLink: attributes.Demo[0].href,
viewDemoLink: attributes.Demo[0].href,
) ?? [],
demoLink: attributes?.Demo?.[0]?.href ?? '',
viewDemoLink: attributes?.Demo?.[0]?.href ?? '',
};
return project;
});
};
};

0 comments on commit e677d89

Please sign in to comment.