From b69c1e46d18003f22dea9f56f7d2152fbb768eab Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Date: Thu, 12 May 2022 13:47:11 -0400 Subject: [PATCH] Merge pull request #2745 from calcom/apps/multiple-categories Allow apps to belong to multiple categories --- apps/web/pages/apps/categories/[category].tsx | 49 +++++++++++++------ apps/web/pages/apps/index.tsx | 14 ++++-- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/apps/web/pages/apps/categories/[category].tsx b/apps/web/pages/apps/categories/[category].tsx index 91e0db6c0c..aa8d679adc 100644 --- a/apps/web/pages/apps/categories/[category].tsx +++ b/apps/web/pages/apps/categories/[category].tsx @@ -1,15 +1,18 @@ import { ChevronLeftIcon } from "@heroicons/react/solid"; -import { InferGetStaticPropsType } from "next"; +import { InferGetStaticPropsType, GetStaticPropsContext } from "next"; import Link from "next/link"; import { useRouter } from "next/router"; import { getAppRegistry } from "@calcom/app-store/_appRegistry"; import { useLocale } from "@calcom/lib/hooks/useLocale"; +import prisma from "@calcom/prisma"; import Shell from "@components/Shell"; import AppCard from "@components/apps/AppCard"; -export default function Apps({ appStore }: InferGetStaticPropsType) { +import { AppCategories } from ".prisma/client"; + +export default function Apps({ apps: apps }: InferGetStaticPropsType) { const { t } = useLocale(); const router = useRouter(); @@ -28,18 +31,16 @@ export default function Apps({ appStore }: InferGetStaticPropsType

All {router.query.category} apps

- {appStore.map((app) => { + {apps.map((app) => { return ( - app.category === router.query.category && ( - - ) + ); })}
@@ -64,10 +65,28 @@ export const getStaticPaths = async () => { }; }; -export const getStaticProps = async () => { +export const getStaticProps = async (context: GetStaticPropsContext) => { + const category = context.params?.category as AppCategories; + + const appQuery = await prisma.app.findMany({ + where: { + categories: { + has: category, + }, + }, + select: { + slug: true, + }, + }); + const appSlugs = appQuery.map((category) => category.slug); + + const appStore = await getAppRegistry(); + + const apps = appStore.filter((app) => appSlugs.includes(app.slug)); + return { props: { - appStore: await getAppRegistry(), + apps: apps, }, }; }; diff --git a/apps/web/pages/apps/index.tsx b/apps/web/pages/apps/index.tsx index 94b61b98fe..501ee0a9b6 100644 --- a/apps/web/pages/apps/index.tsx +++ b/apps/web/pages/apps/index.tsx @@ -2,6 +2,7 @@ import { InferGetStaticPropsType } from "next"; import { getAppRegistry } from "@calcom/app-store/_appRegistry"; import { useLocale } from "@calcom/lib/hooks/useLocale"; +import prisma from "@calcom/prisma"; import AppsShell from "@components/AppsShell"; import Shell from "@components/Shell"; @@ -25,11 +26,18 @@ export default function Apps({ appStore, categories }: InferGetStaticPropsType { const appStore = await getAppRegistry(); - const categories = appStore.reduce((c, app) => { - c[app.category] = c[app.category] ? c[app.category] + 1 : 1; + + const categoryQuery = await prisma.app.findMany({ + select: { + categories: true, + }, + }); + const categories = categoryQuery.reduce((c, app) => { + for (const category of app.categories) { + c[category] = c[category] ? c[category] + 1 : 1; + } return c; }, {} as Record); - return { props: { categories: Object.entries(categories).map(([name, count]) => ({ name, count })),