Skip to content

Commit

Permalink
SonarCloud fixes (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
samwisekind authored Dec 24, 2024
1 parent 8011f1e commit c0ff12c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/components/Timeline/Timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Timeline = ({
classes.push(styles['timeline-single']);
}

const sortedItems = items.sort((a, b) => a.localeCompare(b));
const sortedItems = items.toSorted((a, b) => a.localeCompare(b));

return (
<ul className={classes.join(' ')}>
Expand Down
12 changes: 6 additions & 6 deletions src/pages/api/catalogues/[catalogue].spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ it.each([
method: 'GET',
url: `/api/catalogues/${value}`,
query: {
catalogue: value, // TODO: Fix path parameter not working correctly
catalogue: value,
page: 2,
},
}) as NextApiRequest;
Expand All @@ -48,7 +48,7 @@ it.each([
method: 'GET',
url: `/api/catalogues/${value}`,
query: {
catalogue: value, // TODO: Fix path parameter not working correctly
catalogue: value,
page: 1,
search: 'hello',
},
Expand All @@ -70,7 +70,7 @@ it('Handles incorrect catalogue', async () => {
method: 'GET',
url: '/api/catalogues/kangaroo',
query: {
catalogue: 'kangaroo', // TODO: Fix path parameter not working correctly
catalogue: 'kangaroo',
page: 1,
},
}) as NextApiRequest;
Expand All @@ -90,7 +90,7 @@ it('Handles incorrect method', async () => {
method: 'POST',
url: '/api/catalogues/bottlenose-dolphin',
query: {
catalogue: 'bottlenose-dolphin', // TODO: Fix path parameter not working correctly
catalogue: 'bottlenose-dolphin',
page: 1,
},
}) as NextApiRequest;
Expand All @@ -110,7 +110,7 @@ it('Handles missing page query', async () => {
method: 'GET',
url: '/api/catalogues/bottlenose-dolphin',
query: {
catalogue: 'bottlenose-dolphin', // TODO: Fix path parameter not working correctly
catalogue: 'bottlenose-dolphin',
},
}) as NextApiRequest;

Expand All @@ -133,7 +133,7 @@ it.each([
method: 'GET',
url: '/api/catalogues/bottlenose-dolphin',
query: {
catalogue: 'bottlenose-dolphin', // TODO: Fix path parameter not working correctly
catalogue: 'bottlenose-dolphin',
page: value,
},
}) as NextApiRequest;
Expand Down
31 changes: 19 additions & 12 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import type { NextPage, GetServerSideProps } from 'next';
import type { Asset } from 'contentful';
import type { Asset, Entry } from 'contentful';

import React, { useRef, useState, useEffect } from 'react';
import { OrganizationJsonLd } from 'next-seo';
Expand All @@ -14,6 +14,9 @@ import type {
FlattenedImage,
FlattenedVideo,
NewsArticle,
ContentTypeNews,
ContentTypePageContent,
ContentTypeSpeciesPage,
} from '@/helpers/types';

import sitemap from '@/data/sitemap.json';
Expand All @@ -24,7 +27,6 @@ import getNews from '@/helpers/getNews';

import {
ContentTypes,
InlineContentEntries,
DEFAULT_SITE_NAME,
DEFAULT_SITE_ALTERNATE_NAME,
DEFAULT_SITE_DESCRIPTION,
Expand Down Expand Up @@ -223,20 +225,25 @@ export const getServerSideProps: GetServerSideProps<PageProps> = async (ctx) =>

const [{ fields }] = homepageData.items;

// TODO: Type this properly
const flattenAssetReference = (data: any) => {
const flattenReference = (
data: Entry<ContentTypeNews | ContentTypePageContent | ContentTypeSpeciesPage> | undefined,
) => {
if (!data) {
return '';
}

const contentTypeID = data?.sys?.contentType?.sys?.id;

if (contentTypeID === InlineContentEntries.Page) {
return data.fields.path;
if (contentTypeID === ContentTypes.NewsArticle) {
return `/news/${(data as Entry<ContentTypeNews>).fields.slug}`;
}

if (contentTypeID === InlineContentEntries.Species) {
return `/education/species/${data.fields.slug}`;
if (contentTypeID === ContentTypes.PageContent) {
return `${(data as Entry<ContentTypePageContent>).fields.path}`;
}

if (contentTypeID === InlineContentEntries.News) {
return `/news/${data.fields.slug}`;
if (contentTypeID === ContentTypes.SpeciesPage) {
return `/education/species/${(data as Entry<ContentTypeSpeciesPage>).fields.slug}`;
}

return '';
Expand All @@ -251,11 +258,11 @@ export const getServerSideProps: GetServerSideProps<PageProps> = async (ctx) =>
highlightLeftTitle: fields.highlightLeftTitle,
highlightLeftSubtitle: fields.highlightLeftSubtitle,
highlightLeftImage: flattenImageAssetFields(fields.highlightLeftImage as Asset),
highlightLeftLink: flattenAssetReference(fields.highlightLeftLink),
highlightLeftLink: flattenReference(fields.highlightLeftLink),
highlightRightTitle: fields.highlightRightTitle,
highlightRightSubtitle: fields.highlightRightSubtitle,
highlightRightImage: flattenImageAssetFields(fields.highlightRightImage as Asset),
highlightRightLink: flattenAssetReference(fields.highlightRightLink),
highlightRightLink: flattenReference(fields.highlightRightLink),
},
newsArticles,
},
Expand Down
4 changes: 2 additions & 2 deletions src/pages/research/catalogues/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Card, Filters, Loading } from '@/components';

import styles from './index.module.scss';

interface updateURLProps {
interface UpdateURLProps {
catalogue: Catalogues,
page: number,
search: string,
Expand All @@ -28,7 +28,7 @@ const updateURL = ({
catalogue,
page,
search,
}: updateURLProps) => {
}: UpdateURLProps) => {
let newURL: URL | string = new URL(location.toString());

newURL.searchParams.set('catalogue', catalogue);
Expand Down

0 comments on commit c0ff12c

Please sign in to comment.