Skip to content

Commit 9503096

Browse files
authored
feat: upgrade playground to new payment-widget version (#15)
1 parent bbeefc4 commit 9503096

File tree

6 files changed

+107
-13
lines changed

6 files changed

+107
-13
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rn-checkout",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"private": true,
55
"scripts": {
66
"dev": "next dev",
@@ -18,7 +18,7 @@
1818
"@radix-ui/react-slot": "^1.1.0",
1919
"@radix-ui/react-switch": "^1.1.0",
2020
"@radix-ui/react-tooltip": "^1.1.2",
21-
"@requestnetwork/payment-widget": "^0.1.2",
21+
"@requestnetwork/payment-widget": "^0.2.0",
2222
"class-variance-authority": "^0.7.0",
2323
"clsx": "^2.1.1",
2424
"cmdk": "^1.0.0",

src/components/Playground.tsx

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { PropsValidation } from "@/lib/validation";
44
import { zodResolver } from "@hookform/resolvers/zod";
55
import PaymentWidget from "@requestnetwork/payment-widget/react";
66
import { CopyIcon } from "lucide-react";
7-
import { useRef, useState } from "react";
7+
import { useEffect, useRef, useState } from "react";
88
import { useForm } from "react-hook-form";
99
import { z } from "zod";
1010
import { Button } from "./ui/button";
@@ -21,6 +21,9 @@ import {
2121
AccordionTrigger,
2222
} from "./ui/accordion";
2323

24+
import { cn } from "@/lib/utils";
25+
import { ZERO_ADDRESS } from "@/lib/constants";
26+
2427
export const Playground = () => {
2528
const {
2629
register,
@@ -38,6 +41,8 @@ export const Playground = () => {
3841
buyerInfo: {},
3942
invoiceNumber: "",
4043
enableBuyerInfo: false,
44+
feeAddress: ZERO_ADDRESS,
45+
feeAmount: 0,
4146
},
4247
});
4348

@@ -65,6 +70,10 @@ export const Playground = () => {
6570
formValues.supportedCurrencies?.length &&
6671
`supportedCurrencies={${JSON.stringify(formValues.supportedCurrencies)}}`,
6772
formValues.invoiceNumber && `invoiceNumber="${formValues.invoiceNumber}"`,
73+
formValues.feeAddress &&
74+
formValues.feeAddress !== ZERO_ADDRESS &&
75+
`feeAddress="${formValues.feeAddress}"`,
76+
formValues.feeAmount && `feeAmountInUSD={${formValues.feeAmount}}`,
6877
]
6978
.filter(Boolean)
7079
.join("\n ");
@@ -99,6 +108,13 @@ const YourComponent = () => {
99108
}
100109
};
101110

111+
useEffect(() => {
112+
if (formValues.feeAddress?.length === 0) {
113+
setValue("feeAddress", ZERO_ADDRESS);
114+
setValue("feeAmount", 0);
115+
}
116+
}, [formValues.feeAddress, formValues.feeAmount]);
117+
102118
return (
103119
<div className="flex flex-col gap-4 mt-4">
104120
<section className="flex flex-col gap-6 lg:gap-4 items-center md:items-start md:justify-between lg:flex-row">
@@ -359,10 +375,17 @@ const YourComponent = () => {
359375
</div>
360376
{/* Seller Address */}
361377
<div className="flex flex-col gap-2">
362-
<Label>Seller address</Label>
378+
<Label className="flex items-center">
379+
Seller address
380+
<span className="text-red-500 ml-1">*</span>
381+
</Label>
363382
<Input
364383
placeholder="0x1234567890123456789012345678901234567890"
365384
{...register("sellerAddress")}
385+
className={cn(
386+
"border-2",
387+
errors.sellerAddress ? "border-red-500" : "border-gray-200"
388+
)}
366389
/>
367390
{errors.sellerAddress?.message && (
368391
<Error>{errors.sellerAddress.message}</Error>
@@ -371,12 +394,19 @@ const YourComponent = () => {
371394

372395
{/* Amount in USD */}
373396
<div className="flex flex-col gap-2">
374-
<Label>Amount in USD</Label>
397+
<Label className="flex items-center">
398+
Amount in USD
399+
<span className="text-red-500 ml-1">*</span>
400+
</Label>
375401
<Input
376402
placeholder="25.55"
377403
{...register("amountInUSD", {
378404
valueAsNumber: true,
379405
})}
406+
className={cn(
407+
"border-2",
408+
errors.amountInUSD ? "border-red-500" : "border-gray-200"
409+
)}
380410
/>
381411
{errors.amountInUSD?.message && (
382412
<Error>{errors.amountInUSD.message}</Error>
@@ -392,10 +422,44 @@ const YourComponent = () => {
392422
)}
393423
</div>
394424

425+
{/* Fee */}
426+
<div className="flex flex-col gap-2">
427+
<Label>Fee Address</Label>
428+
<Input
429+
placeholder="0x1234567890123456789012345678901234567890"
430+
{...register("feeAddress")}
431+
/>
432+
{errors.feeAddress?.message && (
433+
<Error>{errors.feeAddress.message}</Error>
434+
)}
435+
<Label>Fee Amount in USD</Label>
436+
<Input
437+
placeholder="25.55"
438+
{...register("feeAmount", {
439+
valueAsNumber: true,
440+
})}
441+
/>
442+
{errors.feeAmount?.message && (
443+
<Error>{errors.feeAmount.message}</Error>
444+
)}
445+
</div>
446+
395447
{/* Currencies */}
396448
<div className="flex flex-col gap-2">
397-
<Label>Currencies</Label>
398-
<CurrencyCombobox register={register} name="supportedCurrencies" />
449+
<Label className="flex items-center">
450+
Currencies
451+
<span className="text-red-500 ml-1">*</span>
452+
</Label>
453+
<CurrencyCombobox
454+
register={register}
455+
name="supportedCurrencies"
456+
className={cn(
457+
"border-2",
458+
errors.supportedCurrencies
459+
? "border-red-500"
460+
: "border-gray-200"
461+
)}
462+
/>
399463
<div className="flex items-center gap-2 flex-wrap">
400464
{formValues.supportedCurrencies?.map((currency) => {
401465
return (
@@ -408,6 +472,9 @@ const YourComponent = () => {
408472
);
409473
})}
410474
</div>
475+
{errors.supportedCurrencies?.message && (
476+
<Error>{errors.supportedCurrencies.message}</Error>
477+
)}
411478
</div>
412479
</div>
413480
<div className="w-full lg:w-1/2">
@@ -426,6 +493,18 @@ const YourComponent = () => {
426493
// @ts-ignore
427494
supportedCurrencies={formValues.supportedCurrencies}
428495
invoiceNumber={formValues.invoiceNumber}
496+
feeAddress={
497+
formValues.feeAddress && formValues.feeAddress.length > 0
498+
? formValues.feeAddress
499+
: ZERO_ADDRESS
500+
}
501+
feeAmountInUSD={
502+
formValues.feeAddress &&
503+
formValues.feeAddress.length > 0 &&
504+
formValues.feeAddress !== ZERO_ADDRESS
505+
? formValues.feeAmount
506+
: 0
507+
}
429508
/>
430509
</div>
431510
</section>

src/components/ui/combobox.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ const currencies = Object.entries(CURRENCY_ID).map(([key, value]) => ({
2929
interface CurrencyComboboxProps {
3030
register: UseFormRegister<any>;
3131
name: string;
32+
className?: string;
3233
}
3334

34-
export function CurrencyCombobox({ register, name }: CurrencyComboboxProps) {
35+
export function CurrencyCombobox({
36+
register,
37+
name,
38+
className,
39+
}: CurrencyComboboxProps) {
3540
const [open, setOpen] = React.useState(false);
3641
const [selectedCurrencies, setSelectedCurrencies] = React.useState<string[]>(
3742
[]
@@ -55,7 +60,7 @@ export function CurrencyCombobox({ register, name }: CurrencyComboboxProps) {
5560
variant="outline"
5661
role="combobox"
5762
aria-expanded={open}
58-
className="w-[300px] justify-between"
63+
className={cn("w-[300px] justify-between", className)}
5964
>
6065
{selectedCurrencies.length > 0
6166
? `${selectedCurrencies.length} selected`

src/lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";

src/lib/validation.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,13 @@ export const PropsValidation = z.object({
6262
.default([]),
6363
invoiceNumber: z.string().optional(),
6464
enableBuyerInfo: z.boolean().default(false),
65+
feeAddress: z
66+
.string()
67+
.refine(isEthereumAddress, "Invalid fee address")
68+
.optional()
69+
.nullable(),
70+
feeAmount: z
71+
.number()
72+
.gte(0, "Fee amount needs to be higher than 0")
73+
.default(0),
6574
});

0 commit comments

Comments
 (0)