Skip to content

Commit

Permalink
apply endpoint changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-justin committed Dec 21, 2023
1 parent f543dc6 commit 38a3266
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/App/Header/UserMenu/EndowmentLink.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Link } from "react-router-dom";
import { useEndowmentLinkQuery } from "services/aws/aws";
import { useEndowment } from "services/aws/useEndowment";
import ContentLoader from "components/ContentLoader";
import Image from "components/Image";
import QueryLoader from "components/QueryLoader";
import { appRoutes } from "constants/routes";

type Props = { endowId: number };
export default function EndowmentLink({ endowId }: Props) {
const query = useEndowmentLinkQuery(endowId);
const query = useEndowment(endowId, ["name", "logo"]);
return (
<QueryLoader
queryState={query}
Expand Down
9 changes: 1 addition & 8 deletions src/components/QueryLoader.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { BaseQueryFn } from "@reduxjs/toolkit/dist/query";
import { TypedUseQueryHookResult } from "@reduxjs/toolkit/dist/query/react/buildHooks";
import { ReactElement } from "react";
import { QueryState } from "types/third-party/redux";
import Status, { ErrorStatus, LoadingStatus } from "components/Status";
import { isEmpty } from "helpers";

type Base = BaseQueryFn<any, unknown, unknown, {}, {}>;
type QueryState<T> = Pick<
TypedUseQueryHookResult<T, any, Base>,
"isLoading" | "isError" | "data"
>;

type Props<T> = {
queryState: QueryState<T>;
messages: {
Expand Down
19 changes: 9 additions & 10 deletions src/pages/Admin/Charity/EditProfile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
EndowmentProfileUpdate,
EndowmentProfile as TProfile,
} from "types/aws";
import { useProfileQuery } from "services/aws/aws";
import { useEndowment } from "services/aws/useEndowment";
import { country } from "components/CountrySelector";
import { FormError, FormSkeleton } from "components/admin";
import { adminRoutes } from "constants/routes";
Expand All @@ -19,16 +19,15 @@ import { toProfileUpdate } from "./update";

export default function EditProfile() {
const { id } = useAdminContext();
const { data: profile, isLoading, isFetching, isError } = useProfileQuery(id);
const { data: profile, isLoading, isError } = useEndowment(id, "all");

const content =
isLoading || isFetching ? (
<FormSkeleton classes="max-w-4xl justify-self-center mt-6" />
) : isError || !profile ? (
<FormError errorMessage="Failed to load profile" />
) : (
<FormWithContext {...profile} />
);
const content = isLoading ? (
<FormSkeleton classes="max-w-4xl justify-self-center mt-6" />
) : isError || !profile ? (
<FormError errorMessage="Failed to load profile" />
) : (
<FormWithContext {...profile} />
);

return (
<>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Admin/Charity/Programs/List.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useAdminContext } from "pages/Admin/Context";
import { useProgramsQuery } from "services/aws/aws";
import { useEndowment } from "services/aws/useEndowment";
import ContentLoader from "components/ContentLoader";
import QueryLoader from "components/QueryLoader";
import { ErrorStatus, Info } from "components/Status";
import { Program } from "./Program";

export default function List() {
const { id } = useAdminContext();
const queryState = useProgramsQuery(id);
const queryState = useEndowment(id, ["program"]);

return (
<QueryLoader
Expand Down
9 changes: 7 additions & 2 deletions src/pages/Admin/Charity/Seo.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useProfileQuery } from "services/aws/aws";
import { useEndowment } from "services/aws/useEndowment";
import CommonSEO from "components/Seo";
import { APP_NAME, DAPP_URL } from "constants/env";
import { appRoutes } from "constants/routes";
Expand All @@ -12,7 +12,12 @@ export default function Seo({
url?: string;
}) {
const { id } = useAdminContext();
const { data: profile } = useProfileQuery(id);
const { data: profile } = useEndowment(id, [
"name",
"overview",
"name",
"logo",
]);

return (
<CommonSEO
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Admin/Sidebar/Header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useProfileQuery } from "services/aws/aws";
import { useEndowment } from "services/aws/useEndowment";
import Image from "components/Image";
import { useAdminContext } from "../../Context";

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

return (
<div
Expand Down
12 changes: 10 additions & 2 deletions src/pages/Donate/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useParams } from "react-router-dom";
import { useProfileQuery } from "services/aws/aws";
import { useEndowment } from "services/aws/useEndowment";
import QueryLoader from "components/QueryLoader";
import Seo from "components/Seo";
import { idParamToNum } from "helpers";
Expand All @@ -9,7 +9,15 @@ import Content from "./Content";
export default function Donate() {
const { id } = useParams<{ id: string }>();
const numId = idParamToNum(id);
const queryState = useProfileQuery(numId, { skip: numId === 0 });
const queryState = useEndowment(numId, [
"name",
"image",
"logo",
"overview",
"kyc_donors_only",
"fiscal_sponsored",
"id",
]);

return (
<section className="grid content-start w-full font-work min-h-screen sm:min-h-[900px] pb-20">
Expand Down
4 changes: 2 additions & 2 deletions src/pages/DonateWidget/DonateWidget.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect } from "react";
import { useParams, useSearchParams } from "react-router-dom";
import { useProfileQuery } from "services/aws/aws";
import { useEndowment } from "services/aws/useEndowment";
import { DappLogo } from "components/Image";
import LoaderRing from "components/LoaderRing";
import QueryLoader from "components/QueryLoader";
Expand All @@ -14,7 +14,7 @@ export default function DonateWidget() {
const routeParams = useParams();
const [searchParams] = useSearchParams();
const endowId = idParamToNum(routeParams.id);
const queryState = useProfileQuery(endowId, { skip: endowId === 0 });
const queryState = useEndowment(endowId, "all");

/** Hide the Intercom chatbot */
useEffect(() => {
Expand Down
6 changes: 2 additions & 4 deletions src/pages/Profile/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Navigate, useParams } from "react-router-dom";
import { useProfileQuery } from "services/aws/aws";
import { useEndowment } from "services/aws/useEndowment";
import Image from "components/Image";
import Seo from "components/Seo";
import { idParamToNum } from "helpers";
Expand All @@ -13,9 +13,7 @@ import Skeleton from "./Skeleton";
export default function Profile({ legacy = false }) {
const { id } = useParams<{ id: string }>();
const numId = idParamToNum(id);
const { isLoading, isError, data } = useProfileQuery(numId, {
skip: numId === 0,
});
const { isLoading, isError, data } = useEndowment(numId, "all");

if (isLoading) return <Skeleton />;
if (isError || !data) return <PageError />;
Expand Down
30 changes: 14 additions & 16 deletions src/services/aws/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
ApplicationVerdict,
ApplicationsQueryParams,
EndowListPaginatedAWSQueryRes,
Endowment,
EndowmentCard,
EndowmentLink,
EndowmentOption,
EndowmentProfile,
EndowmentsQueryParams,
Expand Down Expand Up @@ -114,19 +114,19 @@ export const aws = createApi({
},
transformResponse: (response: { data: any }) => response,
}),
endowmentLink: builder.query<EndowmentLink, number>({
providesTags: ["profile"],
query: (id) => `v6/endowments/${id}?env=${apiEnv}&format=link`,
}),
profile: builder.query<EndowmentProfile, number>({
providesTags: ["profile"],
query: (id) => `v6/endowments/${id}?env=${apiEnv}&format=profile`,
}),
programs: builder.query<EndowmentProfile, number>({
endowment: builder.query<
Endowment,
{ id: number; fields?: (keyof Endowment)[] }
>({
providesTags: ["profile"],
query: (id) => `v6/endowments/${id}?env=${apiEnv}&format=programs`,
query: ({ id, fields }) => ({
url: `v6/endowments/${id}`,
params: {
env: apiEnv,
...(fields ? { fields: fields.join(",") } : {}),
},
}),
}),

program: builder.query<Program, { endowId: number; programId: string }>({
providesTags: ["profile", "program"],
query: ({ endowId, programId }) =>
Expand Down Expand Up @@ -185,11 +185,9 @@ export const aws = createApi({
export const {
useWalletProfileQuery,
useToggleBookmarkMutation,
useEndowmentQuery,
useEndowmentCardsQuery,
useEndowmentOptionsQuery,
useProfileQuery,
useProgramsQuery,
useEndowmentLinkQuery,
useProgramQuery,
useEditProfileMutation,
useApplicationsQuery,
Expand All @@ -199,7 +197,7 @@ export const {
endpoints: {
endowmentCards: { useLazyQuery: useLazyEndowmentCardsQuery },
endowmentOptions: { useLazyQuery: useLazyEndowmentOptionsQuery },
profile: { useLazyQuery: useLazyProfileQuery },
endowment: { useLazyQuery: useLazyProfileQuery },
applications: { useLazyQuery: useLazyApplicationsQuery },
},
util: {
Expand Down
17 changes: 17 additions & 0 deletions src/services/aws/useEndowment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Endowment } from "types/aws";
import { QueryState } from "types/third-party/redux";
import { useEndowmentQuery } from "./aws";

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

type K = keyof Endowment;
export function useEndowment<T extends K[] | "all">(id: number, fields: T) {
const query = useEndowmentQuery({
id,
...(fields === "all" ? {} : { fields }),
});

return query as QueryState<
T extends K[] ? Pick<Endowment, ArrayValues<T>> : Endowment
>;
}
14 changes: 5 additions & 9 deletions src/types/aws/ap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type SocialMediaURLs = {
tiktok: string; //or empty
};

type EndowmentItem = {
export type Endowment = {
id: number;
active_in_countries: string[];
contact_email: string;
Expand All @@ -57,11 +57,10 @@ type EndowmentItem = {
url: string;
};

export type EndowmentProfile = EndowmentItem;
export type EndowmentPrograms = Pick<EndowmentItem, "program">;
export type EndowmentProfile = Endowment;

export type EndowmentCard = Pick<
EndowmentItem,
Endowment,
| "id"
| "active_in_countries"
| "endow_designation"
Expand All @@ -72,14 +71,11 @@ export type EndowmentCard = Pick<
| "sdgs"
| "tagline"
>;

export type EndowmentLink = Pick<EndowmentItem, "name" | "logo">;

export type EndowmentOption = Pick<EndowmentItem, "id" | "name">;
export type EndowmentOption = Pick<Endowment, "id" | "name">;

//most are optional except id, but typed as required to force setting of default values - "", [], etc ..
export type EndowmentProfileUpdate = Except<
EndowmentItem,
Endowment,
"endow_designation" | "fiscal_sponsored"
> & {
endow_designation: EndowDesignation | "";
Expand Down
7 changes: 7 additions & 0 deletions src/types/third-party/redux.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { PayloadAction } from "@reduxjs/toolkit";
import { BaseQueryFn } from "@reduxjs/toolkit/dist/query";
import { TagDescription } from "@reduxjs/toolkit/dist/query/endpointDefinitions";
import { TypedUseQueryHookResult } from "@reduxjs/toolkit/dist/query/react/buildHooks";

type Tag = TagDescription<string>;
export type TagPayload = PayloadAction<Tag[], string>;
type Base = BaseQueryFn<any, unknown, unknown, {}, {}>;
export type QueryState<T> = Pick<
TypedUseQueryHookResult<T, any, Base>,
"isLoading" | "isError" | "data"
>;

0 comments on commit 38a3266

Please sign in to comment.