Skip to content

Commit 224becc

Browse files
committed
refactor: allowlist parsing and error handling
changes: - split parseAllowList and forocused it to parse only. - Add to throw errors.
1 parent 3234cec commit 224becc

File tree

1 file changed

+82
-75
lines changed

1 file changed

+82
-75
lines changed

components/hypercert/hypercert-minting-form/form-steps.tsx

Lines changed: 82 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -423,66 +423,47 @@ const DatesAndPeople = ({ form }: FormStepsProps) => {
423423
);
424424
};
425425

426-
const parseAllowList = async (value: string) => {
427-
let data;
428-
let allowList: AllowlistEntry[] = [];
429-
const url = value.startsWith("ipfs://")
430-
? `https://ipfs.io/ipfs/${value.replace("ipfs://", "")}`
431-
: value.startsWith("https://")
432-
? value
433-
: null;
426+
export const parseAllowList = async (data: Response | string) => {
427+
let allowList: AllowlistEntry[];
428+
try {
429+
const text = typeof data === "string" ? data : await data.text();
430+
const parsedData = Papa.parse<AllowlistEntry>(text, {
431+
header: true,
432+
skipEmptyLines: true,
433+
});
434434

435-
if (!url) {
436-
errorToast("Invalid URL. URL must start with 'https://' or 'ipfs://'");
437-
return;
438-
}
439-
data = await fetch(url);
440-
441-
const contentType = data.headers.get("content-type");
442-
443-
if (
444-
contentType?.includes("text/csv") ||
445-
contentType?.includes("text/plain") ||
446-
value.endsWith(".csv")
447-
) {
448-
try {
449-
const text = await data.text();
450-
const parsedData = Papa.parse<AllowlistEntry>(text, {
451-
header: true,
452-
skipEmptyLines: true,
453-
});
454-
455-
const validEntries = parsedData.data.filter(
456-
(entry) => entry.address && entry.units,
457-
);
435+
const validEntries = parsedData.data.filter(
436+
(entry) => entry.address && entry.units,
437+
);
458438

459-
// Calculate total units
460-
const total = validEntries.reduce(
461-
(sum, entry) => sum + BigInt(entry.units),
462-
BigInt(0),
463-
);
439+
// Calculate total units
440+
const total = validEntries.reduce(
441+
(sum, entry) => sum + BigInt(entry.units),
442+
BigInt(0),
443+
);
464444

465-
allowList = validEntries.map((entry) => {
466-
const address = getAddress(entry.address);
467-
const originalUnits = BigInt(entry.units);
468-
// Scale units proportionally to DEFAULT_NUM_UNITS
469-
const scaledUnits =
470-
total > 0 ? (originalUnits * DEFAULT_NUM_UNITS) / total : BigInt(0);
445+
allowList = validEntries.map((entry) => {
446+
const address = getAddress(entry.address);
447+
const originalUnits = BigInt(entry.units);
448+
// Scale units proportionally to DEFAULT_NUM_UNITS
449+
const scaledUnits =
450+
total > 0 ? (originalUnits * DEFAULT_NUM_UNITS) / total : BigInt(0);
471451

472-
return {
473-
address: address,
474-
units: scaledUnits,
475-
};
476-
});
452+
return {
453+
address: address,
454+
units: scaledUnits,
455+
};
456+
});
477457

478-
return allowList;
479-
} catch (e) {
480-
e instanceof Error
481-
? errorToast(e.message)
482-
: errorToast("Failed to parse allowlist.");
458+
return allowList;
459+
} catch (e) {
460+
if (errorHasMessage(e)) {
461+
errorToast(e.message);
462+
throw new Error(e.message);
463+
} else {
464+
errorToast("Failed to parse allowlist.");
465+
throw new Error("Failed to parse allowlist.");
483466
}
484-
} else {
485-
errorToast("Invalid file type.");
486467
}
487468
};
488469

@@ -598,31 +579,57 @@ const AdvancedAndSubmit = ({ form, isBlueprint }: FormStepsProps) => {
598579
};
599580

600581
const fetchAllowlist = async (value: string) => {
601-
const allowList = await parseAllowList(value);
602-
if (!allowList || allowList.length === 0) return;
582+
let data: Response;
583+
const url = value.startsWith("ipfs://")
584+
? `https://ipfs.io/ipfs/${value.replace("ipfs://", "")}`
585+
: value.startsWith("https://")
586+
? value
587+
: null;
603588

604-
const totalUnits = DEFAULT_NUM_UNITS;
589+
if (!url) {
590+
errorToast("Invalid URL. URL must start with 'https://' or 'ipfs://'");
591+
throw new Error(
592+
"Invalid URL. URL must start with 'https://' or 'ipfs://'",
593+
);
594+
}
595+
data = await fetch(url);
605596

606-
// validateAllowlist
607-
try {
608-
validateAllowlist({
609-
allowList,
610-
totalUnits,
611-
});
612-
form.setValue("allowlistEntries", allowList);
613-
} catch (e) {
614-
if (errorHasMessage(e)) {
615-
toast({
616-
title: "Error",
617-
description: e.message,
618-
variant: "destructive",
619-
});
620-
} else {
621-
toast({
622-
title: "Error",
623-
description: "Failed to upload allow list",
597+
const contentType = data.headers.get("content-type");
598+
599+
if (
600+
contentType?.includes("text/csv") ||
601+
contentType?.includes("text/plain") ||
602+
value.endsWith(".csv")
603+
) {
604+
const allowList = await parseAllowList(data);
605+
if (!allowList || allowList.length === 0) return;
606+
607+
const totalUnits = DEFAULT_NUM_UNITS;
608+
609+
// validateAllowlist
610+
try {
611+
validateAllowlist({
612+
allowList,
613+
totalUnits,
624614
});
615+
form.setValue("allowlistEntries", allowList);
616+
} catch (e) {
617+
if (errorHasMessage(e)) {
618+
toast({
619+
title: "Error",
620+
description: e.message,
621+
variant: "destructive",
622+
});
623+
} else {
624+
toast({
625+
title: "Error",
626+
description: "Failed to upload allow list",
627+
});
628+
}
625629
}
630+
} else {
631+
errorToast("Invalid file type.");
632+
throw new Error("Invalid file type.");
626633
}
627634
};
628635

0 commit comments

Comments
 (0)