Skip to content

Commit

Permalink
chore: migrate from role-group to page, rename route params
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-stumpf committed Dec 2, 2024
1 parent 811c65e commit e6aac61
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 81 deletions.
28 changes: 0 additions & 28 deletions src/app/(dashboard)/[guildId]/fetchers.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/app/(dashboard)/[guildId]/page.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ import type { Guild } from "@/lib/schemas/guild";
import type { Role } from "@/lib/schemas/role";
import type { RoleGroup } from "@/lib/schemas/roleGroup";
import type { DynamicRoute, PaginatedResponse } from "@/lib/types";
import type { Schemas } from "@guildxyz/types";
import { Lock } from "@phosphor-icons/react/dist/ssr";

const RoleGroupPage = async ({
params,
}: DynamicRoute<{ roleGroupId: string; guildId: string }>) => {
const { roleGroupId: roleGroupIdParam, guildId: guildIdParam } = await params;
}: DynamicRoute<{ roleGroupUrlName: string; guildUrlName: string }>) => {
const { roleGroupUrlName, guildUrlName } = await params;
const guild = (await (
await fetch(`${env.NEXT_PUBLIC_API}/guild/urlName/${guildIdParam}`)
await fetch(`${env.NEXT_PUBLIC_API}/guild/urlName/${guildUrlName}`)
).json()) as Guild;
const paginatedRoleGroup = (await (
await fetch(
`${env.NEXT_PUBLIC_API}/role-group/search?customQuery=@guildId:{${guild.id}}&pageSize=${Number.MAX_SAFE_INTEGER}`,
`${env.NEXT_PUBLIC_API}/page/search?customQuery=@guildId:{${guild.id}}&pageSize=${Number.MAX_SAFE_INTEGER}`,
)
).json()) as PaginatedResponse<RoleGroup>;
const roleGroups = paginatedRoleGroup.items;
const roleGroup = roleGroups.find(
// @ts-expect-error
(rg) => rg.urlName === roleGroupIdParam || rg.id === guild.homeRoleGroupId,
(rg) => rg.urlName === roleGroupUrlName || rg.id === guild.homeRoleGroupId,
)!;

Check warning on line 29 in src/app/(dashboard)/[guildUrlName]/[roleGroupUrlName]/page.tsx

View workflow job for this annotation

GitHub Actions / quality-assurance

lint/style/noNonNullAssertion

Forbidden non-null assertion.
const paginatedRole = await fetcher<PaginatedResponse<Role>>(
`${env.NEXT_PUBLIC_API}/role/search?customQuery=@guildId:{${guild.id}}&pageSize=${Number.MAX_SAFE_INTEGER}`,
Expand All @@ -40,38 +41,18 @@ const RoleGroupPage = async ({
);
};

const RE = {
id: "aca776be-5a7b-4618-bb7c-1130de066257",
createdAt: 1732825819745,
updatedAt: 1732825819745,
name: "Home - delete",
guildId: "2b3330e4-05fa-4949-a80c-2deeb99d100e",
urlName: "stars-guild-delete",
foreignEntity: "role-group",
foreignIdentifier: "1b06e1e7-5bf8-4f55-8e7b-16c5542d10c3",
description:
'Grants delete access to the "Home" role group in the "Stars Guild" guild',
type: "GUILD",
permissions: {
read: "b62cda4f-27d6-40dc-9625-34a6ce74912a",
update: "78ac4c72-cc44-4336-b5cd-e73e93fd86e7",
delete: "d713b547-135d-49ab-a9b1-e670ad1871a7",
},
};
type Reward = typeof RE;

const RoleCard = async ({ role }: { role: Role }) => {
const rewards = (await Promise.all(
// @ts-ignore
role.rewards?.map(({ rewardId }) => {
const req = `${env.NEXT_PUBLIC_API}/reward/id/${rewardId}`;
try {
return fetcher<Reward>(req);
return fetcher<Schemas["RewardFull"]>(req);
} catch {
console.error({ rewardId, req });
}
}) ?? [],
)) as Reward[];
)) as Schemas["RewardFull"][];

return (
<Card className="flex flex-col md:flex-row" key={role.id}>
Expand Down Expand Up @@ -114,7 +95,7 @@ const RoleCard = async ({ role }: { role: Role }) => {
);
};

const Reward = ({ reward }: { reward: Reward }) => {
const Reward = ({ reward }: { reward: Schemas["RewardFull"] }) => {
return (
<div className="border-b p-4 last:border-b-0">
<div className="mb-2 font-medium">{reward.name}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { env } from "@/lib/env";
import { revalidateTag } from "next/cache";

export const revalidateRoleGroups = async (guildId: string) => {
revalidateTag(`role-groups-${guildId}`);
revalidateTag(`page-${guildId}`);
};

export const joinGuild = async ({ guildId }: { guildId: string }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const CreateRoleGroupDialogForm = ({

if (!token) throw new Error("Unauthorized"); // TODO: custom errors?

return fetcher<RoleGroup>(`${env.NEXT_PUBLIC_API}/role-group`, {
return fetcher<RoleGroup>(`${env.NEXT_PUBLIC_API}/page`, {
method: "POST",
headers: {
"X-Auth-Token": token,
Expand Down
29 changes: 29 additions & 0 deletions src/app/(dashboard)/[guildUrlName]/fetchers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { env } from "@/lib/env";
import { fetcher } from "@/lib/fetcher";
import type { PaginatedResponse } from "@/lib/types";
import type { Schemas } from "@guildxyz/types";

export const getGuild = async (urlName: string) => {
return await fetcher<Schemas["GuildFull"]>(
`${env.NEXT_PUBLIC_API}/guild/urlName/${urlName}`,
{
next: {
tags: [`guild-${urlName}`],
},
},
);
};

export const getRoleGroups = async (guildId: string) => {
return (
await fetcher<PaginatedResponse<Schemas["RoleGroupFull"]>>(
`${env.NEXT_PUBLIC_API}/page/search?customQuery=@guildId:{${guildId}}&pageSize=${Number.MAX_SAFE_INTEGER}`,
{
next: {
tags: [`page-${guildId}`],
revalidate: 3600,
},
},
)
).items;
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ import { JoinButton } from "./components/JoinButton";
const GuildPage = async ({
params,
children,
}: PropsWithChildren<DynamicRoute<{ guildId: string }>>) => {
const { guildId: guildIdParam } = await params;
}: PropsWithChildren<DynamicRoute<{ guildUrlName: string }>>) => {
const { guildUrlName } = await params;
const guild = await fetcher<Guild>(
`${env.NEXT_PUBLIC_API}/guild/urlName/${guildIdParam}`,
`${env.NEXT_PUBLIC_API}/guild/urlName/${guildUrlName}`,
);
const token = await getParsedToken();
if (!token) {
throw new Error("Failed to authenticate");
}
const user = await fetcher<Schemas["UserFull"]>(
`${env.NEXT_PUBLIC_API}/user/id/${token.userId}`,
);
const user =
token &&
(await fetcher<Schemas["UserFull"]>(
`${env.NEXT_PUBLIC_API}/user/id/${token.userId}`,
));

return (
<main className="py-16">
Expand All @@ -43,7 +42,7 @@ const GuildPage = async ({
</h1>
</div>
<AuthBoundary fallback={<SignInButton />}>
<JoinButton guild={guild} user={user} />
{user && <JoinButton guild={guild} user={user} />}
</AuthBoundary>
</div>
<p className="line-clamp-3 max-w-prose text-balance text-lg leading-relaxed">
Expand Down
10 changes: 10 additions & 0 deletions src/app/(dashboard)/[guildUrlName]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { DynamicRoute } from "@/lib/types";
import GuildPage from "./[roleGroupUrlName]/page";

const DefaultGuildPage = async ({
params,
}: DynamicRoute<{ guildUrlName: string }>) => {
return <GuildPage params={{ ...(await params), roleGroupUrlName: "" }} />;
};

export default DefaultGuildPage;
7 changes: 7 additions & 0 deletions src/lib/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export const fetcher = async <Data = unknown, Error = unknown>(
? await response.json()
: await response.text();

if (process.env.NODE_ENV === "development") {
console.info(
`[${new Date().toLocaleTimeString()} - fetcher]: ${resource}`,
res,
);
}

if (!response.ok) {
if (resource.includes(env.NEXT_PUBLIC_API)) {
return Promise.reject({
Expand Down

0 comments on commit e6aac61

Please sign in to comment.