Skip to content

Commit

Permalink
new single endow endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-justin committed Nov 6, 2024
1 parent d48f83b commit 9f6ce36
Show file tree
Hide file tree
Showing 23 changed files with 52 additions and 70 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"dependencies": {
"@better-giving/assets": "1.0.18",
"@better-giving/endowment": "1.0.21",
"@better-giving/endowment": "1.0.26",
"@better-giving/registration": "1.0.24",
"@better-giving/schemas": "1.0.1",
"@better-giving/types": "1.0.1",
Expand All @@ -43,7 +43,7 @@
"@paypal/react-paypal-js": "8.3.0",
"@radix-ui/react-slider": "1.1.2",
"@radix-ui/react-tooltip": "1.1.2",
"@reduxjs/toolkit": "2.2.3",
"@reduxjs/toolkit": "2.3.0",
"@sentry/react": "8.22.0",
"@stripe/react-stripe-js": "2.7.1",
"@stripe/stripe-js": "3.5.0",
Expand Down
1 change: 0 additions & 1 deletion src/App/Header/SearchDropdown/SearchDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ interface Props {
export default function SearchDropdown({ classes = "", query }: Props) {
const [debouncedQuery, isDebouncing] = useDebouncer(query, 500);
const { currentData, isLoading, isFetching, isError, isUninitialized } =
/** @ts-ignore: temp */
useEndowmentCardsQuery(
{
query: debouncedQuery,
Expand Down
2 changes: 1 addition & 1 deletion src/App/Header/UserMenu/EndowmentLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface IBookmarkLink {
endowId: number;
}
export function BookmarkLink({ endowId }: IBookmarkLink) {
const query = useEndowment({ id: endowId }, ["logo", "name"]);
const query = useEndowment(endowId, ["logo", "name"]);
return (
<QueryLoader
queryState={query}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default function RecipientDetailsForm({
details,
});

if ("data" in res) {
if (res.data) {
const file = (bankStatement as FileList).item(0)!;
return await onSubmit(res.data, file);
}
Expand Down
1 change: 0 additions & 1 deletion src/pages/@sections/HeroBottom/HeroBottom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import mappng from "./map.webp";
import s from "./styles.module.css";

const HeroBottom = ({ className = "" }) => {
/** @ts-ignore: temp */
const { data } = useEndowmentCardsQuery({
claimed: "true",
page: "1",
Expand Down
7 changes: 1 addition & 6 deletions src/pages/Admin/Charity/EditProfile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ import type { FV } from "./schema";

export function Component() {
const { id } = useAdminContext();
const {
data: profile,
isLoading,
isError,
isFetching,
} = useEndowment({ id });
const { data: profile, isLoading, isError, isFetching } = useEndowment(id);

const content =
isLoading || isFetching ? (
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Admin/Charity/EditProfile/useEditProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function useEditProfile(id: number, df: DirtyFields) {
}

if (df.slug) {
const result = await endowment({ slug: fv.slug });
const result = await endowment({ id: fv.slug });
//endow is found with update.slug
if (result.isSuccess) {
return displayError(`Slug "${fv.slug}" is already taken`);
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Admin/Charity/Media/VideoPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TMedia } from "@better-giving/endowment";
import type { IMedia } from "@better-giving/endowment";
import { useErrorContext } from "contexts/ErrorContext";
import { useModalContext } from "contexts/ModalContext";
import { LoaderCircle, Minus, Pencil, Star } from "lucide-react";
Expand All @@ -11,7 +11,7 @@ import {
} from "services/aws/media";
import VideoEditor from "./VideoEditor";

export default function VideoPreview(props: TMedia) {
export default function VideoPreview(props: IMedia) {
const { id } = useAdminContext();
const { handleError } = useErrorContext();
const { showModal } = useModalContext();
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Admin/Charity/Seo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function Seo({
url?: string;
}) {
const { id } = useAdminContext();
const { data: profile } = useEndowment({ id }, ["logo", "name", "overview"]);
const { data: profile } = useEndowment(id, ["logo", "name", "overview"]);

return (
<CommonSEO
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Admin/Charity/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function Settings() {
data: endow,
isLoading,
isError,
} = useEndowment({ id }, [
} = useEndowment(id, [
"receiptMsg",
"hide_bg_tip",
"progDonationsAllowed",
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Admin/SidebarHeader/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useAdminContext } from "../Context";

export default function Header() {
const { id } = useAdminContext();
const { data: profile, isLoading } = useEndowment({ id }, ["logo", "name"]);
const { data: profile, isLoading } = useEndowment(id, ["logo", "name"]);

return (
<div
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Donate/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function Component() {
const { id } = useParams<{ id: string }>();
const numId = idParamToNum(id);

const queryState = useEndowment({ id: numId });
const queryState = useEndowment(numId);

return (
<QueryLoader
Expand Down
2 changes: 1 addition & 1 deletion src/pages/DonateWidget/DonateWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function DonateWidget() {
const routeParams = useParams();
const [searchParams] = useSearchParams();
const endowId = idParamToNum(routeParams.id);
const queryState = useEndowment({ id: endowId });
const queryState = useEndowment(endowId);

/** Hide the Intercom chatbot */
useEffect(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Marketplace/Cards/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import QueryLoader from "components/QueryLoader";
import type { EndowmentCard } from "types/aws";
import Card from "./Card";
import useCards from "./useCards";

Expand Down Expand Up @@ -33,7 +34,7 @@ export default function Cards({ classes = "" }: { classes?: string }) {
<div
className={`${classes} w-full grid sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4 content-start`}
>
{endowments.map((endow) => (
{endowments.map((endow: EndowmentCard) => (
<Card {...endow} key={endow.id} />
))}

Expand Down
3 changes: 1 addition & 2 deletions src/pages/Marketplace/Cards/useCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default function useCards() {
}),
{}
);

const {
isLoading,
isFetching,
Expand All @@ -41,7 +40,7 @@ export default function useCards() {
/** search for both verified/unverified if user didn't explicitly narrow verified status */
searchText && isEmpty(verified) ? "true,false" : verified.join(","),
..._params,
},
} as any,
{ skip: isDebouncing }
);

Expand Down
6 changes: 3 additions & 3 deletions src/pages/Profile/Body/GeneralInfo/Media.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TMedia } from "@better-giving/endowment";
import type { IMedia } from "@better-giving/endowment";
import ReactPlayer from "react-player";

export default function Media({ media }: { media: TMedia[] }) {
export default function Media({ media }: { media: IMedia[] }) {
return (
<div className="w-full h-full px-8 py-10 grid sm:grid-cols-[repeat(auto-fill,minmax(373px,1fr))] gap-8">
{media.map((m) => (
Expand All @@ -11,7 +11,7 @@ export default function Media({ media }: { media: TMedia[] }) {
);
}

function Medium(props: TMedia) {
function Medium(props: IMedia) {
return (
/** @see https://github.com/CookPete/react-player/issues/145 */
<div className="relative pt-[56.25%] aspect-[16/9] rounded-lg overflow-clip">
Expand Down
10 changes: 5 additions & 5 deletions src/pages/Profile/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { endowIdParam, segment } from "@better-giving/endowment/schema";
import fallback_banner from "assets/images/fallback-banner.png";
import flying_character from "assets/images/flying-character.png";
import Image from "components/Image";
Expand All @@ -12,20 +13,19 @@ import {
useOutletContext,
useParams,
} from "react-router-dom";
import { segment } from "schemas/string";
import { useEndowment } from "services/aws/useEndowment";
import { parse, union } from "valibot";
import { bodyRoute } from "./Body";
import PageError from "./PageError";
import ProfileContext, { useProfileContext } from "./ProfileContext";
import Skeleton from "./Skeleton";

function Profile() {
const legacy = useOutletContext<true | undefined>();
const { id = "" } = useParams<{ id: string }>();
const params = useParams();
const id = parse(union([segment, endowIdParam]), params.id);

const { isLoading, isError, data } = useEndowment(
segment.isValidSync(id) ? { slug: id } : { id: Number(id) }
);
const { isLoading, isError, data } = useEndowment(id);

if (isLoading) return <Skeleton />;
if (isError || !data) return <PageError />;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Widget/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function Preview({ classes = "", config }: Props) {
const { endowment, methods, increments, ...restConfig } = config;
const endowName = config.endowment.name;

const { data } = useEndowment({ id: endowment.id }, [
const { data } = useEndowment(endowment.id, [
"hide_bg_tip",
"progDonationsAllowed",
]);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Widget/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Preview from "./Preview";
import Snippet from "./Snippet";

export function Widget({ endowId = 0 }: { endowId?: number }) {
const queryState = useEndowment({ id: endowId }, undefined, {
const queryState = useEndowment(endowId, undefined, {
skip: !endowId,
});

Expand Down
21 changes: 8 additions & 13 deletions src/services/aws/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { createApi, fetchBaseQuery, retry } from "@reduxjs/toolkit/query/react";
import { fetchAuthSession } from "aws-amplify/auth";
import { TEMP_JWT } from "constants/auth";
import { APIs } from "constants/urls";
import { apiEnv } from "services/constants";
import type { RootState } from "store/store";
import { userIsSignedIn } from "types/auth";
import type {
Expand All @@ -24,7 +23,6 @@ import type {
EndowmentOption,
} from "types/aws";
import { version as v } from "../helpers";
import type { IdOrSlug } from "../types";

const awsBaseQuery = retry(
fetchBaseQuery({
Expand Down Expand Up @@ -158,15 +156,14 @@ export const aws = createApi({
},
}),

endowment: builder.query<Endow, IdOrSlug & { fields?: (keyof Endow)[] }>({
endowment: builder.query<
Endow,
{ id: string | number; fields?: (keyof Endow)[] }
>({
providesTags: ["endowment"],
query: ({ fields, ...args }) => ({
url: "id" in args ? `v10/endowments/${args.id}` : "v10/endowments",
params: {
env: apiEnv,
slug: args.slug,
...(fields ? { fields: fields.join(",") } : {}),
},
query: ({ fields, id }) => ({
url: `${v(1)}/endowments/${id}`,
params: fields ? { fields: fields.join(",") } : {},
}),
}),

Expand Down Expand Up @@ -276,9 +273,7 @@ export const endowByEin = async (
): Promise<
Pick<Endow, "id" | "name" | "claimed" | "registration_number"> | undefined
> => {
const res = await fetch(
`${APIs.aws}/v10/endowments?ein=${ein}&env=${apiEnv}`
);
const res = await fetch(`${APIs.aws}/${v(1)}/endowments/ein/${ein}`);
if (res.status === 404) return undefined;
if (!res.ok) throw res;
return res.json() as Promise<Endow>;
Expand Down
6 changes: 3 additions & 3 deletions src/services/aws/media.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {
IMedia,
MediaPage,
MediaQueryParamsObj,
MediaUpdate,
TMedia,
} from "@better-giving/endowment";
import { TEMP_JWT } from "constants/auth";
import { version as v } from "../helpers";
Expand All @@ -16,7 +16,7 @@ const media = aws.injectEndpoints({
return { url: `/${v(1)}/endowments/${endowId}/media`, params };
},
}),
medium: builder.query<TMedia, { endowId: number; mediaId: string }>({
medium: builder.query<IMedia, { endowId: number; mediaId: string }>({
providesTags: ["program"],
query: ({ endowId, mediaId }) =>
`/${v(1)}/endowments/${endowId}/media/${mediaId}`,
Expand All @@ -36,7 +36,7 @@ const media = aws.injectEndpoints({
},
}),
editMedium: builder.mutation<
TMedia,
IMedia,
MediaUpdate & { endowId: number; mediaId: string }
>({
invalidatesTags: (_, error) => (error ? [] : ["medium", "media"]),
Expand Down
11 changes: 2 additions & 9 deletions src/services/aws/useEndowment.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import type { Endow } from "@better-giving/endowment";
import type { IdOrSlug } from "services/types";
import type { QueryState } from "types/third-party/redux";
import { useEndowmentQuery } from "./aws";

type ArrayValues<T extends readonly unknown[]> = T[number];

type K = keyof Endow;
export function useEndowment<T extends K[]>(
idOrSlug: IdOrSlug,
id: string | number,
fields?: T,
options?: { skip: boolean }
) {
const query = useEndowmentQuery(
{
...idOrSlug,
fields,
},
options
);
const query = useEndowmentQuery({ id, fields }, options);

return query as QueryState<
T extends K[] ? Pick<Endow, ArrayValues<T>> : Endow
Expand Down
Loading

0 comments on commit 9f6ce36

Please sign in to comment.