Skip to content

Commit

Permalink
profile slug error (#3555)
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-justin authored Jan 30, 2025
1 parent 3cc06a5 commit 103eb00
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/pages/profile/body/body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import { useRootData } from "hooks/use-root-data";
import { Globe, MapPin } from "lucide-react";
import type { EndowmentBalances } from "types/aws";
import { useProfileContext } from "../profile-context";
import { npoId } from "./common/npo-id";

export const loader: LoaderFunction = async ({ params }) =>
getEndowBalance(params.id);
export const loader: LoaderFunction = async ({ params }) => {
const id = await npoId(params.id);
if (typeof id !== "number") return id;
return getEndowBalance(id.toString());
};

export default function Body() {
const p = useProfileContext();
Expand Down
16 changes: 16 additions & 0 deletions src/pages/profile/body/common/npo-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { endowIdParam, segment } from "@better-giving/endowment/schema";
import { safeParse, union } from "valibot";
import { getNpoByIdOrSlug } from ".server/npo";

export const npoId = async (
idOrSlugParam: string | undefined
): Promise<number | Response> => {
const id = safeParse(union([segment, endowIdParam]), idOrSlugParam);
if (id.issues) throw new Response(id.issues[0].message, { status: 400 });

if (typeof id.output === "number") return id.output;

const npo = await getNpoByIdOrSlug(id.output, ["id"]);
if (!npo) throw new Response(null, { status: 404 });
return npo.id;
};
11 changes: 6 additions & 5 deletions src/pages/profile/body/general-info/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type { IMedia, Program } from "@better-giving/endowment";
import type { FundItem } from "@better-giving/fundraiser";
import type { LoaderFunction } from "@vercel/remix";
import { getPrograms } from "api/get/programs";
import { plusInt } from "api/schema/endow-id";
import { parse } from "valibot";
import { npoId } from "../common/npo-id";
import { featuredMedia } from "../featured-media";
import { getFundsNpoMemberOf } from ".server/funds";

Expand All @@ -14,10 +13,12 @@ export interface LoaderData {
}

export const loader: LoaderFunction = async ({ params }) => {
const id = parse(plusInt, params.id);
const id = await npoId(params.id);
if (typeof id !== "number") return id;

return {
programs: await getPrograms(params.id),
media: await featuredMedia(params.id),
programs: await getPrograms(id),
media: await featuredMedia(id.toString()),
funds: await getFundsNpoMemberOf(id, {
npoProfileFeatured: true,
}),
Expand Down
8 changes: 6 additions & 2 deletions src/pages/profile/body/program/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { LinksFunction, LoaderFunction } from "@vercel/remix";
import { getProgram } from "api/get/program";
import { richTextStyles } from "components/rich-text";
import { npoId } from "../common/npo-id";
export { default } from "./program";
export { ErrorBoundary } from "components/error";
export const loader: LoaderFunction = ({ params }) =>
getProgram(params.id, params.programId);
export const loader: LoaderFunction = async ({ params }) => {
const id = await npoId(params.id);
if (typeof id !== "number") return id;

return getProgram(id, params.programId);
};
export const links: LinksFunction = () => [...richTextStyles];

0 comments on commit 103eb00

Please sign in to comment.