Skip to content

Commit

Permalink
settings loader
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-justin committed Oct 14, 2024
1 parent 032a224 commit 9ed2717
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 48 deletions.
8 changes: 1 addition & 7 deletions src/components/Header/SearchDropdown/SearchDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ interface Props {
export default function SearchDropdown({ classes = "", query }: Props) {
const [debouncedQuery, isDebouncing] = useDebouncer(query, 500);
const { currentData, isLoading, isFetching, isError, isUninitialized } =
useEndowmentCardsQuery(
{
query: debouncedQuery,
page: "1",
},
{ skip: isDebouncing }
);
useEndowmentCardsQuery({ query: debouncedQuery }, { skip: isDebouncing });

const isCategoriesShown =
!debouncedQuery && !isDebouncing && !isLoading && !isFetching;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Admin/Charity/Dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const loader: LoaderFunction = async ({ params }) => {
async function getAllocation(id: number) {
const url = new URL(APIs.aws);
url.searchParams.set("env", apiEnv);
url.searchParams.set("field", "allocation");
url.searchParams.set("fields", "allocation");
url.pathname = `v9/endowments/${id}`;

return cacheGet(url)
Expand Down
20 changes: 12 additions & 8 deletions src/pages/Admin/Charity/Settings/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CheckField, RhfForm } from "components/form";
import { BG_ID } from "constants/common";
import { useErrorContext } from "contexts/ErrorContext";
import { useController, useForm } from "react-hook-form";
import { useLoaderData } from "react-router-dom";
import { schema } from "schemas/shape";
import type { Endowment, EndowmentSettingsAttributes } from "types/aws";
import type { TDonateMethod } from "types/components";
Expand All @@ -15,9 +16,12 @@ import ReceiptMsg from "./ReceiptMsg";
import { MAX_RECEIPT_MSG_CHAR } from "./constants";
import type { FV } from "./types";

type Props = Pick<Endowment, "id" | EndowmentSettingsAttributes>;
export default function Form() {
const endow = useLoaderData() as Pick<
Endowment,
"id" | EndowmentSettingsAttributes
>;

export default function Form(props: Props) {
const updateEndow = useUpdateEndowment();
const { displayError } = useErrorContext();

Expand All @@ -35,10 +39,10 @@ export default function Form(props: Props) {
})
),
values: {
receiptMsg: props.receiptMsg ?? "",
hide_bg_tip: props.hide_bg_tip ?? false,
programDonateDisabled: !(props.progDonationsAllowed ?? true),
donateMethods: fill(props.donateMethods),
receiptMsg: endow.receiptMsg ?? "",
hide_bg_tip: endow.hide_bg_tip ?? false,
programDonateDisabled: !(endow.progDonationsAllowed ?? true),
donateMethods: fill(endow.donateMethods),
},
});

Expand All @@ -64,7 +68,7 @@ export default function Form(props: Props) {
}}
onSubmit={handleSubmit(
async ({ programDonateDisabled, donateMethods, ...fv }) => {
if (props.id === BG_ID && fv.hide_bg_tip === false) {
if (endow.id === BG_ID && fv.hide_bg_tip === false) {
return displayError(
"BG donation flow should not show BG tip screen"
);
Expand All @@ -73,7 +77,7 @@ export default function Form(props: Props) {
await updateEndow({
...fv,
progDonationsAllowed: !programDonateDisabled,
id: props.id,
id: endow.id,
donateMethods: donateMethods
.filter((m) => !m.disabled)
.map((m) => m.id),
Expand Down
28 changes: 0 additions & 28 deletions src/pages/Admin/Charity/Settings/Settings.tsx

This file was deleted.

24 changes: 23 additions & 1 deletion src/pages/Admin/Charity/Settings/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
export { default as Component } from "./Settings";
import { APIs } from "constants/urls";
import { cacheGet } from "helpers/cache-get";
import type { LoaderFunction } from "react-router-dom";
import { apiEnv } from "services/constants";
import type { EndowmentSettingsAttributes } from "types/aws";

export { default as Component } from "./Form";

const fields: EndowmentSettingsAttributes[] = [
"receiptMsg",
"hide_bg_tip",
"progDonationsAllowed",
"donateMethods",
];
export const loader: LoaderFunction = async ({ params }) => {
const url = new URL(APIs.aws);
url.searchParams.set("env", apiEnv);
url.searchParams.set("fields", fields.join(","));
url.pathname = `v9/endowments/${params.id}`;

//Pick<Endowment, "id" | EndowmentSettingsAttributes>;
return cacheGet(url);
};
1 change: 0 additions & 1 deletion src/pages/Widget/Configurer/EndowmentSelector/Options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default function Options({ searchText, isDebouncing = false }: Props) {
query: searchText,
sdgs: Object.keys(unsdgs).join(","),
kyc_only: "true,false",
page: "1",
});

return (
Expand Down
4 changes: 2 additions & 2 deletions src/services/aws/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ export const aws = createApi({
}),
endowmentOptions: builder.query<EndowmentOption[], EndowQParams>({
providesTags: ["endowments"],
query: ({ page = "1", ...p }) => {
query: (p) => {
return {
url: `${v(1)}/cloudsearch-nonprofits`,
params: { ...p, page, fields: endowSelectorOptionFields },
params: { ...p, page: 1, fields: endowSelectorOptionFields },
};
},
transformResponse(res: EndowListPaginatedAWSQueryRes<EndowmentOption[]>) {
Expand Down

0 comments on commit 9ed2717

Please sign in to comment.