Skip to content

Commit

Permalink
consumers to use new queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-justin committed Dec 20, 2023
1 parent dad4241 commit f543dc6
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 160 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 { useProfileQuery } from "services/aws/aws";
import { useEndowmentLinkQuery } from "services/aws/aws";
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 = useProfileQuery({ endowId });
const query = useEndowmentLinkQuery(endowId);
return (
<QueryLoader
queryState={query}
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 @@ -19,12 +19,7 @@ import { toProfileUpdate } from "./update";

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

const content =
isLoading || isFetching ? (
Expand Down
17 changes: 6 additions & 11 deletions src/pages/Admin/Charity/EditProfile/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,19 @@ export function toProfileUpdate(arg: Arg): EndowmentProfileUpdate {
return {
id: d.id,
active_in_countries: d.active_in_countries,
categories: { sdgs: d.sdgs ?? [], general: [] },
charity_navigator_rating: "",
contact_email: d.contact_email ?? "",
contributor_verification_required:
d.contributor_verification_required ?? false,
endow_designation: d.endow_designation ?? "",
contact_email: d.contact_email,
endow_designation: d.endow_designation,
hq_country: d.hq_country,
image: d.image,
kyc_donors_only: d.kyc_donors_only ?? false,
kyc_donors_only: d.kyc_donors_only,
logo: d.logo,
name: d.name,
overview: d.overview,
program: [], //program is updated in /create-program
program_id: "",
published: d.published ?? false,
registration_number: d.registration_number ?? "",
sdgs: d.sdgs ?? [],
published: d.published,
registration_number: d.registration_number,
sdgs: d.sdgs,
social_media_urls: {
facebook: d.social_media_urls.facebook,
instagram: d.social_media_urls.instagram,
Expand All @@ -48,7 +44,6 @@ export function toProfileUpdate(arg: Arg): EndowmentProfileUpdate {
},
street_address: d.street_address,
tagline: d.tagline ?? "",
tier: 1,
url: d.url ?? "",
};
}
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 { useProfileQuery } from "services/aws/aws";
import { useProgramsQuery } from "services/aws/aws";
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 = useProfileQuery({ endowId: id });
const queryState = useProgramsQuery(id);

return (
<QueryLoader
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 } = useProfileQuery({ endowId: id });
const { data: profile } = useProfileQuery(id);

return (
<CommonSEO
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Admin/Sidebar/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useAdminContext } from "../../Context";

export default function Header() {
const { id } = useAdminContext();
const { data: profile, isLoading } = useProfileQuery({ endowId: id });
const { data: profile, isLoading } = useProfileQuery(id);

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 @@ -9,7 +9,7 @@ import Content from "./Content";
export default function Donate() {
const { id } = useParams<{ id: string }>();
const numId = idParamToNum(id);
const queryState = useProfileQuery({ endowId: numId }, { skip: numId === 0 });
const queryState = useProfileQuery(numId, { skip: numId === 0 });

return (
<section className="grid content-start w-full font-work min-h-screen sm:min-h-[900px] pb-20">
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 @@ -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 = useProfileQuery(endowId, { skip: endowId === 0 });

/** Hide the Intercom chatbot */
useEffect(() => {
Expand Down
11 changes: 3 additions & 8 deletions src/pages/Profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,12 @@ import PageError from "./PageError";
import ProfileContext, { useProfileContext } from "./ProfileContext";
import Skeleton from "./Skeleton";

// import Unpublished from "./Unpublished";

export default function Profile({ legacy = false }) {
const { id } = useParams<{ id: string }>();
const numId = idParamToNum(id);
const { isLoading, isError, data } = useProfileQuery(
{ endowId: numId, isLegacy: legacy },
{
skip: numId === 0,
}
);
const { isLoading, isError, data } = useProfileQuery(numId, {
skip: numId === 0,
});

if (isLoading) return <Skeleton />;
if (isError || !data) return <PageError />;
Expand Down
72 changes: 16 additions & 56 deletions src/services/aws/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ import {
ApplicationsQueryParams,
EndowListPaginatedAWSQueryRes,
EndowmentCard,
EndowmentLink,
EndowmentOption,
EndowmentProfile,
EndowmentProfileUpdate,
EndowmentsQueryParams,
FileObject,
PaginatedAWSQueryRes,
Program,
WalletProfile,
} from "types/aws";
import { apiEnv } from "services/constants";
import { RootState } from "store/store";
import { logger } from "helpers";
import { TEMP_JWT } from "constants/auth";
import { APIs } from "constants/urls";
import { version as v } from "../helpers";
Expand Down Expand Up @@ -116,24 +114,19 @@ export const aws = createApi({
},
transformResponse: (response: { data: any }) => response,
}),
profile: builder.query<
EndowmentProfile,
{ endowId: number; isLegacy?: boolean }
>({
endowmentLink: builder.query<EndowmentLink, number>({
providesTags: ["profile"],
query: ({ endowId, isLegacy = false }) => ({
params: { legacy: isLegacy },
url: `/${v(1)}/profile/endowment/${endowId}`,
}),
transformResponse(r: EndowmentProfile) {
//transform cloudsearch placeholders
const tagline = r.tagline === " " ? "" : r.tagline;
return {
...r,
tagline,
};
},
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>({
providesTags: ["profile"],
query: (id) => `v6/endowments/${id}?env=${apiEnv}&format=programs`,
}),

program: builder.query<Program, { endowId: number; programId: string }>({
providesTags: ["profile", "program"],
query: ({ endowId, programId }) =>
Expand Down Expand Up @@ -186,32 +179,6 @@ export const aws = createApi({
};
},
}),
updateBankStatement: builder.mutation<
EndowmentProfile,
{ id: number; bank_statement_file: FileObject }
>({
query: (payload) => {
return {
url: `/${v(1)}/profile/endowment/bank-statement`,
method: "PUT",
headers: { authorization: TEMP_JWT },
body: payload,
};
},
/** pessimistic manual cache update so not to GET fresh data */
async onQueryStarted(args, { dispatch, queryFulfilled }) {
try {
const { data } = await queryFulfilled;
dispatch(
updateAWSQueryData("profile", { endowId: args.id }, (draft) => {
draft = { ...data };
})
);
} catch (err) {
logger.error(err);
}
},
}),
}),
});

Expand All @@ -221,12 +188,13 @@ export const {
useEndowmentCardsQuery,
useEndowmentOptionsQuery,
useProfileQuery,
useProgramsQuery,
useEndowmentLinkQuery,
useProgramQuery,
useEditProfileMutation,
useApplicationsQuery,
useApplicationQuery,
useReviewApplicationMutation,
useUpdateBankStatementMutation,

endpoints: {
endowmentCards: { useLazyQuery: useLazyEndowmentCardsQuery },
Expand All @@ -240,13 +208,9 @@ export const {
},
} = aws;

type EndowCardFields = keyof (Omit<EndowmentCard, "hq"> &
/** replace with cloudsearch specific field format */
Pick<EndowmentProfileUpdate, "hq_country">);

//object format first to avoid duplicates
const endowCardObj: {
[key in EndowCardFields]: any; //we care only for keys
[key in keyof EndowmentCard]: ""; //we care only for keys
} = {
hq_country: "",
endow_designation: "",
Expand All @@ -255,17 +219,13 @@ const endowCardObj: {
id: "",
logo: "",
kyc_donors_only: "",
contributor_verification_required: "",
name: "",
tagline: "",
endow_type: "",
published: false,
program: "",
};
const endowCardFields = Object.keys(endowCardObj).join(",");

const endowSelectorOptionObj: {
[key in Extract<EndowCardFields, "id" | "name">]: any;
[key in keyof EndowmentOption]: "";
} = {
id: "",
name: "",
Expand Down
Loading

0 comments on commit f543dc6

Please sign in to comment.