Skip to content

Commit

Permalink
remove ordering (#3283)
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-justin authored Sep 3, 2024
1 parent 618f912 commit e0b5116
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 137 deletions.
84 changes: 0 additions & 84 deletions src/components/DonateMethods/helpers.test.ts

This file was deleted.

22 changes: 1 addition & 21 deletions src/components/DonateMethods/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const methodDetails: {
[K in DonateMethodId]: Pick<TDonateMethod, "name" | "tooltip">;
} = {
crypto: { name: "Crypto" },
daf: { name: "DAF", tooltip: "requires card payment and must be next to it" },
daf: { name: "DAF", tooltip: "requires card payment" },
stocks: { name: "Stocks" },
stripe: { name: "Card/Bank" },
};
Expand All @@ -30,23 +30,3 @@ export function fill(sub = all): TDonateMethod[] {
const missing = all.filter((x) => !sub.includes(x));
return toMethods(existing).concat(toMethods(missing, true));
}

export function order<T extends DonateMethodId | TDonateMethod>(arr: T[]): T[] {
const getId = (item: T): DonateMethodId =>
typeof item === "object" ? item.id : item;

const result = [...arr];
const stripeIndex = result.findIndex((item) => getId(item) === "stripe");
const dafIndex = result.findIndex((item) => getId(item) === "daf");

if (stripeIndex !== -1 && dafIndex !== -1) {
// Remove 'daf' from its current position
const [daf] = result.splice(dafIndex, 1);

// Insert 'daf' next to 'stripe'
const newStripeIndex = result.findIndex((item) => getId(item) === "stripe");
result.splice(newStripeIndex + 1, 0, daf);
}

return result;
}
19 changes: 5 additions & 14 deletions src/pages/Admin/Charity/Settings/Form.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ErrorMessage } from "@hookform/error-message";
import { yupResolver } from "@hookform/resolvers/yup";
import { DonateMethods, fill, order } from "components/DonateMethods";
import { DonateMethods, fill } from "components/DonateMethods";
import { LockedSplitSlider } from "components/donation";
import { CheckField, Field, Form as _Form } from "components/form";
import { useController, useForm } from "react-hook-form";
Expand Down Expand Up @@ -80,28 +80,19 @@ export default function Form(props: Props) {
programDonateDisabled,
splitLockPct,
payout_minimum,
donateMethods: dms,
donateMethods,
...fv
}) => {
const ordered = order(dms);
const ids = ordered.filter((m) => !m.disabled).map((m) => m.id);

await updateEndow({
...fv,
progDonationsAllowed: !programDonateDisabled,
splitLiqPct: 100 - splitLockPct,
id: props.id,
payout_minimum: +payout_minimum,
donateMethods: order(ids),
donateMethods: donateMethods
.filter((m) => !m.disabled)
.map((m) => m.id),
});

/** manually re-set the `methods` to trigger animation which doesnt' trigger in ff scenario
* 1. init order-a: [ stripe, daf, crypto, stocks ]
* 2. reordered [stripe, crypto, stocks, daf]
* 3. submit: becomes order-a (no change - animation doesn't run)
*/
await new Promise((r) => setTimeout(r, 1000));
resetField("donateMethods", { defaultValue: ordered });
}
)}
className="w-full max-w-4xl justify-self-center grid content-start gap-6 mt-6"
Expand Down
5 changes: 1 addition & 4 deletions src/pages/Donate/Content.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import flying_character from "assets/images/flying-character.png";
import { order } from "components/DonateMethods";
import ExtLink from "components/ExtLink";
import { DappLogo } from "components/Image";
import { Steps } from "components/donation";
Expand Down Expand Up @@ -53,9 +52,7 @@ function Content({ intent, endowment }: Props) {
config={{
splitDisabled: endowment.splitFixed ?? false,
liquidSplitPct: endowment.splitLiqPct ?? 50,
methodIds: endowment.donateMethods
? order(endowment.donateMethods)
: undefined,
methodIds: endowment.donateMethods,
}}
className="md:border border-gray-l4 rounded-lg row-start-2"
/>
Expand Down
15 changes: 3 additions & 12 deletions src/pages/Widget/Configurer/Configurer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ErrorMessage } from "@hookform/error-message";
import { yupResolver } from "@hookform/resolvers/yup";
import { DonateMethods, order } from "components/DonateMethods";
import { DonateMethods } from "components/DonateMethods";
import { LockedSplitSlider, ProgramSelector } from "components/donation";
import { CheckField, Field, Form } from "components/form";
import type { Dispatch, SetStateAction } from "react";
Expand Down Expand Up @@ -58,17 +58,8 @@ export default function Configurer({
const isDescriptionTextShown = watch("isDescriptionTextShown");
const isTitleShown = watch("isTitleShown");

const submit: SubmitHandler<FormValues> = async ({ methods, ...fv }) => {
const ordered = order(methods);
setConfig({ ...fv, methods: ordered });

/** manually re-set the `methods` to trigger animation which doesnt' trigger in ff scenario
* 1. init order-a: [ stripe, daf, crypto, stocks ]
* 2. reordered [stripe, crypto, stocks, daf]
* 3. submit: becomes order-a (no change - animation doesn't run)
*/
await new Promise((r) => setTimeout(r, 1000));
resetField("methods", { defaultValue: ordered });
const submit: SubmitHandler<FormValues> = async (fv) => {
setConfig(fv);
};

return (
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Widget/Widget.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fill, order } from "components/DonateMethods";
import { fill } from "components/DonateMethods";
import QueryLoader from "components/QueryLoader";
import Seo from "components/Seo";
import { DEFAULT_PROGRAM } from "components/donation";
Expand Down Expand Up @@ -44,7 +44,7 @@ function Content({ endowment }: { endowment?: Endowment }) {
isTitleShown: true,
liquidSplitPct: endowment?.splitLiqPct ?? 50,
splitDisabled: endowment?.splitFixed ?? false,
methods: order(filled),
methods: filled,
accentPrimary: "#2D89C8",
accentSecondary: "#E6F1F9",
program: DEFAULT_PROGRAM,
Expand Down

0 comments on commit e0b5116

Please sign in to comment.