Skip to content

Commit

Permalink
chore: use Set for request method
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jan 10, 2024
1 parent b38c609 commit b6c7be1
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions frontend/src/screens/apps/NewApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,22 @@ const NewApp = () => {
? budgetRenewalParam
: "monthly";

const reqMethodsParam = queryParams.get("request_methods");
const [requestMethods, setRequestMethods] = useState(
reqMethodsParam ?? Object.keys(nip47MethodDescriptions).join(" ")
// returns RequestMethod Set
const parseRequestMethods = (reqParam: string): Set<RequestMethodType> => {
const methods = reqParam
? reqParam.split(" ")
: Object.keys(nip47MethodDescriptions);
// Create a Set of RequestMethodType from the array
const requestMethodsSet = new Set<RequestMethodType>(
methods as RequestMethodType[]
);
return requestMethodsSet;
};

const reqMethodsParam = parseRequestMethods(
queryParams.get("request_methods") ?? ""
);
const [requestMethods, setRequestMethods] = useState(reqMethodsParam);

const maxAmountParam = queryParams.get("max_amount") ?? "";
const [maxAmount, setMaxAmount] = useState(
Expand All @@ -78,7 +90,7 @@ const NewApp = () => {

// Only timestamp in seconds or ISO string is expected
const expiresAtParam = parseExpiresParam(queryParams.get("expires_at") ?? "");
const [expiresAt, setExpiresAt] = useState(expiresAtParam ?? "");
const [expiresAt, setExpiresAt] = useState(expiresAtParam);
const [days, setDays] = useState(0);
const [expireOptions, setExpireOptions] = useState(false);

Expand All @@ -96,17 +108,14 @@ const NewApp = () => {
const handleRequestMethodChange = (
event: React.ChangeEvent<HTMLInputElement>
) => {
const rm = event.target.value;
if (requestMethods.includes(rm)) {
const rm = event.target.value as RequestMethodType;
if (requestMethods.has(rm)) {
// If checked and item is already in the list, remove it
const newMethods = requestMethods
.split(" ")
.filter((reqMethod) => reqMethod !== rm)
.join(" ");
setRequestMethods(newMethods);
requestMethods.delete(rm);
} else {
setRequestMethods(`${requestMethods} ${rm}`);
requestMethods.add(rm);
}
setRequestMethods(requestMethods);
};

const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
Expand All @@ -128,7 +137,7 @@ const NewApp = () => {
maxAmount: maxAmount.toString(),
budgetRenewal: budgetRenewal,
expiresAt: expiresAt,
requestMethods: requestMethods,
requestMethods: [...requestMethods].join(" "),
returnTo: returnTo,
}),
});
Expand Down Expand Up @@ -219,7 +228,11 @@ const NewApp = () => {
key={index}
className={`w-full ${
rm == "pay_invoice" ? "order-last" : ""
} ${!edit && !requestMethods.includes(rm) ? "hidden" : ""}`}
} ${
!edit && !requestMethods.has(rm as RequestMethodType)
? "hidden"
: ""
}`}
>
<div className="flex items-center mb-2">
{RequestMethodIcon && (
Expand All @@ -233,7 +246,7 @@ const NewApp = () => {
type="checkbox"
id={rm}
value={rm}
checked={requestMethods.includes(rm)}
checked={requestMethods.has(rm as RequestMethodType)}
onChange={handleRequestMethodChange}
className={` ${
!edit ? "hidden" : ""
Expand All @@ -249,7 +262,7 @@ const NewApp = () => {
{rm == "pay_invoice" && (
<div
className={`pt-2 pb-2 pl-5 ml-2.5 border-l-2 border-l-gray-200 dark:border-l-gray-400 ${
!requestMethods.includes(rm)
!requestMethods.has(rm)
? edit
? "pointer-events-none opacity-30"
: "hidden"
Expand Down

0 comments on commit b6c7be1

Please sign in to comment.