From b950348f0676c28bea38ec3d82d7bc458511c19d Mon Sep 17 00:00:00 2001 From: Dominik Stumpf Date: Fri, 9 Aug 2024 04:51:57 +0200 Subject: [PATCH] refactor: redirect to error and fetch per guild --- .../(marketing)/profile/[username]/page.tsx | 9 +-- .../profile/_components/EditContributions.tsx | 65 ++++++++----------- 2 files changed, 31 insertions(+), 43 deletions(-) diff --git a/src/app/(marketing)/profile/[username]/page.tsx b/src/app/(marketing)/profile/[username]/page.tsx index 0d19f6e673..c06199516e 100644 --- a/src/app/(marketing)/profile/[username]/page.tsx +++ b/src/app/(marketing)/profile/[username]/page.tsx @@ -10,7 +10,7 @@ import { SWRProvider } from "@/components/SWRProvider" import { Anchor } from "@/components/ui/Anchor" import { Guild, Role, Schemas } from "@guildxyz/types" import { ArrowRight } from "@phosphor-icons/react/dist/ssr" -import { notFound } from "next/navigation" +import { notFound, redirect } from "next/navigation" import { Profile } from "../_components/Profile" // TODO: use env var for this url when it is changed to this value. @@ -30,9 +30,10 @@ const fetchPublicProfileData = async ({ username }: { username: string }) => { revalidate: 600, }, }) - if (profileResponse.status === 404) { - notFound() - } + + if (profileResponse.status === 404) notFound() + if (!profileResponse.ok) redirect("/error") + const profile = (await profileResponse.json()) as Schemas["Profile"] const contributions = await ssrFetcher( contributionsRequest, diff --git a/src/app/(marketing)/profile/_components/EditContributions.tsx b/src/app/(marketing)/profile/_components/EditContributions.tsx index 1a5b7b8a79..cbc1617196 100644 --- a/src/app/(marketing)/profile/_components/EditContributions.tsx +++ b/src/app/(marketing)/profile/_components/EditContributions.tsx @@ -29,7 +29,6 @@ import { AvatarFallback } from "@radix-ui/react-avatar" import { DialogDescription } from "@radix-ui/react-dialog" import { useState } from "react" import useSWRImmutable from "swr/immutable" -import fetcher from "utils/fetcher" import { useContribution } from "../_hooks/useContribution" import { useCreateContribution } from "../_hooks/useCreateContribution" import { useDeleteContribution } from "../_hooks/useDeleteContribution" @@ -37,23 +36,11 @@ import { useMemberships } from "../_hooks/useMemberships" import { useUpdateContribution } from "../_hooks/useUpdateContribution" import { CardWithGuildLabel } from "./CardWithGuildLabel" -const guildFetcher = (urls: string[]) => { - return Promise.all(urls.map((url) => fetcher(url) as Promise)) -} -const useYourVerifiedGuild = () => { - const yourGuilds = useYourGuilds() - const requests = yourGuilds.data - ? yourGuilds.data.map((guild) => `/v2/guilds/${guild.id}`) - : null - return { guilds: useSWRImmutable(requests, guildFetcher), baseGuilds: yourGuilds } -} - const EditContributionCard = ({ contribution, }: { contribution: Schemas["Contribution"] }) => { const { data: guild } = useSWRImmutable( - `/v2/guilds/${contribution.guildId}`, - fetcher + `/v2/guilds/${contribution.guildId}` ) const memberships = useMemberships() const editContribution = useUpdateContribution({ contributionId: contribution.id }) @@ -107,17 +94,8 @@ export const EditContributions = () => { const [roleId, setRoleId] = useState("") const { toast } = useToast() - const { - guilds: { data: guildData }, - baseGuilds, - } = useYourVerifiedGuild() - const guilds = - guildData && - baseGuilds.data?.reduce( - (acc, curr, i) => - curr.tags.includes("VERIFIED") ? [...acc, guildData[i]] : acc, - [] - ) + const { data: baseGuilds } = useYourGuilds() + const guilds = baseGuilds?.filter(({ tags }) => tags.includes("VERIFIED")) const roleIds = memberships.data?.find( (membership) => membership.guildId.toString() === guildId @@ -165,20 +143,7 @@ export const EditContributions = () => { {guilds?.map((data) => ( - -
- - - - - {data.name} -
-
+ ))}
@@ -233,6 +198,28 @@ export const EditContributions = () => { ) } + +const GuildSelectItem = ({ guildId }: Pick) => { + const { data } = useSWRImmutable(`/v2/guilds/${guildId}`) + if (!data) return + return ( + +
+ + + + + {data.name} +
+
+ ) +} + const RoleSelectItem = ({ roleId, guildId,