Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error handling, refinement & passing css param between page redirects for /extension page #36

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ export default function App() {
export function ErrorBoundary() {
const error = useRouteError();

// detect if page loaded in wm tools
const location = useLocation();
const isEmbeded = location.pathname.indexOf("extension") !== -1;
// remove quote=true from params if exists
const urlParams = location.search ? location.search.replace('quote=true&', '') : undefined;
const onErrorLink = isEmbeded ? `/extension${urlParams}` : '/';
const onErrorLabel = isEmbeded ? 'Try again' : 'Go to homepage';

const ErrorPage = ({ children }: { children: ReactNode }) => {
return (
<html lang="en" className="h-full overflow-x-hidden">
Expand Down Expand Up @@ -127,9 +135,9 @@ export function ErrorBoundary() {
<div className="text-destructive sm:text-md font-medium">
{error.statusText}
</div>
<Link to="/">
<Link to={onErrorLink}>
<Button variant="outline" size="sm">
Go to homepage
{onErrorLabel}
</Button>
</Link>
</div>
Expand All @@ -152,9 +160,9 @@ export function ErrorBoundary() {
<div className="text-destructive sm:text-md font-medium">
Cause: {errorMessage}
</div>
<Link to="/">
<Link to={onErrorLink}>
<Button variant="outline" size="sm">
Go to homepage
{onErrorLabel}
</Button>
</Link>
</div>
Expand Down
28 changes: 21 additions & 7 deletions app/routes/extension.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ export async function loader({ request }: LoaderFunctionArgs) {
const receiver = params.get("receiver");
const amount = params.get("amount");
const asset = params.get("asset");
const note = params.get("note");
const action = params.get("action");
const paramCss = params.get("css");
let note = params.get("note");
note = note === "undefined" ? null : note;

let css = "";
if (paramCss) {
Expand Down Expand Up @@ -121,15 +122,16 @@ export async function loader({ request }: LoaderFunctionArgs) {
: undefined,
amount: formattedAmount,
currency: asset ? asset : "usd",
note: note ? note : null,
action: action ? action : null,
note: note ? note : undefined,
action: action ? action : undefined,
isValidRequest: isValidRequest,
receiveAmount: receiveAmount ? receiveAmount.amountWithCurrency : null,
debitAmount: debitAmount ? debitAmount.amountWithCurrency : null,
receiverName: receiverName,
isQuote: isQuote,
submission,
assetScale,
encodedCss: paramCss ? paramCss : undefined,
css,
} as const);
}
Expand All @@ -140,9 +142,12 @@ const schema = z.object({
.string()
.transform((val) => val.replace("$", "https://"))
.pipe(z.string().url({ message: "The input is not a wallet address." })),
amount: z.coerce.number(),
amount: z.coerce
.number()
.positive({ message: "The amount has to be a positive number." }),
note: z.string().optional(),
action: z.string().optional(),
css: z.string().optional(),
});

const addCss = (css: string) => {
Expand Down Expand Up @@ -243,6 +248,7 @@ export default function Extension() {
placeholder="Enter wallet address"
compact
{...conform.input(fields.walletAddress)}
errors={fields.walletAddress.errors}
/>
<Field
type="text"
Expand All @@ -269,19 +275,27 @@ export default function Extension() {
/>
<div className="flex justify-center mt-2">
<Button
className="wmt-formattable-button disabled:pointer-events-auto disabled:cursor-progress"
className={cn(
"wmt-formattable-button disabled:pointer-events-auto",
isSubmitting && "disabled:cursor-progress"
)}
aria-label="pay"
type="submit"
name="intent"
value="pay"
disabled={isSubmitting}
disabled={isSubmitting || Number(amountValue) === 0}
>
{data.action || "Pay"}
</Button>
<input
type="hidden"
{...conform.input(fields.action)}
defaultValue={data.action || undefined}
defaultValue={data.action}
/>
<input
type="hidden"
{...conform.input(fields.css)}
defaultValue={data.encodedCss}
/>
</div>
</div>
Expand Down