Use directus return types, fix project link nested type API response

This commit is contained in:
2024-12-28 21:01:25 -06:00
parent fe3d2aefbe
commit 55b491bd33
3 changed files with 74 additions and 34 deletions

View File

@@ -25,13 +25,18 @@ async function getURLs(
const urls = ["/technology"]; const urls = ["/technology"];
// Get all projects with the technology // Get all projects with the technology
const all_projects = await directus.request(readItems("project")); const all_projects = await directus.request(readItems("project", {
if (all_projects != null) { fields: ["id", {
for (const project of all_projects) { technologies: ["id"],
if (project.technologies?.some((t) => t.id === key)) }],
urls.push(`/projects/${project.id}`); }));
}
} // if (all_projects != null) {
// for (const project of all_projects) {
// if (project.technologies?.some((t) => t.id === key))
// urls.push(`/projects/${project.id}`);
// }
// }
return urls; return urls;
} }

View File

@@ -1,19 +1,26 @@
import AppWrapper from "@/components/AppWrapper"; import AppWrapper from "@/components/AppWrapper";
import directus, { Project } from "@/utils/directus"; import directus from "@/utils/directus";
import { cn } from "@/utils/helpers"; import { cn } from "@/utils/helpers";
import { readItems } from "@directus/sdk"; import { readItems } from "@directus/sdk";
import { GetStaticPropsResult, type NextPage } from "next"; import { GetStaticPropsResult, type NextPage } from "next";
import Link from "next/link"; import Link from "next/link";
type Props = { type Props = {
projects: Project[]; projects: Awaited<ReturnType<typeof getProjects>>;
}; };
export async function getStaticProps(): Promise<GetStaticPropsResult<Props>> { async function getProjects() {
const projects = await directus.request(readItems("project")); return await directus.request(readItems("project", {
fields: ["name", "shortDescription", "icon", {links: ["url"]}],
}));
}
export async function getStaticProps(): Promise<GetStaticPropsResult<Props>> {;
return { return {
props: {projects} props: {
projects: await getProjects(),
}
} }
} }
@@ -21,11 +28,12 @@ const ProjectsPage: NextPage<Props> = ({projects}) => {
return ( return (
<AppWrapper dotsClassName="animate-bg-fast"> <AppWrapper dotsClassName="animate-bg-fast">
<div className="max-w-500 mx-auto mt-20 grid h-full w-max grid-cols-1 gap-x-20 gap-y-7 py-2 md:grid-cols-2 lg:grid-cols-3"> <div className="max-w-500 mx-auto mt-20 grid h-full w-max grid-cols-1 gap-x-20 gap-y-7 py-2 md:grid-cols-2 lg:grid-cols-3">
{projects.map(({ name, shortDescription: description, links, icon }) => ( {projects.map(({ name, shortDescription: description, links, icon }) => {
return (
<Link <Link
key={name} key={name}
className="flex relative max-w-[30rem] flex-shrink items-center opacity-75 transition-opacity hover:opacity-100 bg-black/30 hover:bg-white/5 py-2 rounded" className="flex relative max-w-[30rem] flex-shrink items-center opacity-75 transition-opacity hover:opacity-100 bg-black/30 hover:bg-white/5 py-2 rounded"
href={links[0]?.url ?? "#"} href={links![0]?.url ?? "#"}
target="_blank" target="_blank"
rel="noreferrer" rel="noreferrer"
title={name} title={name}
@@ -41,7 +49,8 @@ const ProjectsPage: NextPage<Props> = ({projects}) => {
</div> </div>
</div> </div>
</Link> </Link>
))} );
})}
</div> </div>
</AppWrapper> </AppWrapper>
); );

View File

@@ -4,6 +4,9 @@ export interface Schema {
metadata: Metadata; metadata: Metadata;
project: Project[]; project: Project[];
technology: Technology[]; technology: Technology[];
link: Link[];
project_technology: ProjectTechnology[];
project_link: ProjectLink[];
} }
export interface Technology { export interface Technology {
@@ -12,19 +15,42 @@ export interface Technology {
url: string | null; url: string | null;
} }
export interface ProjectTechnology {
id: string;
project_id: string;
technology_id: string;
}
export interface Project { export interface Project {
id: string; id: string;
// One2Many
links: number[] | ProjectLink[];
// Many2Many
technologies: number[] | ProjectTechnology[];
icon: string | null; icon: string | null;
name: string; name: string;
description: string; description: string;
shortDescription: string; shortDescription: string;
links: Link[];
featured: boolean;
wakatimeOffset: number | null; wakatimeOffset: number | null;
technologies: Technology[] | null;
bannerImage: string; bannerImage: string;
} }
export interface Link { export interface Link {
id: string;
project_id: string;
icon: string;
url: string;
description: string | null;
}
export interface ProjectLink {
id: string;
project_id: string;
sort: number;
icon: string; icon: string;
url: string; url: string;
description: string | null; description: string | null;