Skip to content

Commit 3b5135a

Browse files
authored
fix/#62: Add onConfirmation callback mechanism in RequestProvider (#68)
Signed-off-by: OjusWiZard <[email protected]>
1 parent e641c88 commit 3b5135a

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

packages/create/src/components/AppBar.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import React, { useState, useEffect } from "react";
1+
import { useState, useEffect } from "react";
22
import { NavLink, useLocation } from "react-router-dom";
33
import { RLogo } from "request-ui";
4-
import { Link, Hidden } from "@material-ui/core";
54

65
import {
76
AppBar,

packages/create/src/containers/CreatePage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FormikHelpers } from "formik";
2-
import React, { useState } from "react";
2+
import { useState } from "react";
33
import { useHistory } from "react-router";
44
import { isCancelError, useCreateRequest } from "request-shared";
55
import { useErrorReporter } from "request-ui";

packages/create/src/containers/RequestPage.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import { useEffect, useState } from "react";
22
import { makeStyles, Typography } from "@material-ui/core";
33
import { useWeb3React } from "@web3-react/core";
44
import {
@@ -84,10 +84,18 @@ const RequestPageInner = () => {
8484
request,
8585
loading,
8686
update,
87+
setOnConfirmation,
8788
counterCurrency,
8889
counterValue,
8990
} = useRequest();
9091

92+
useEffect(() => {
93+
if (request?.status === 'waiting') {
94+
setOnConfirmation(update);
95+
}
96+
// eslint-disable-next-line
97+
}, [request]); // not including update and setOnConfirmation here because they won't change
98+
9199
const cancel = async () => {
92100
if (!request || !account || !chainId) {
93101
throw new Error("cannot cancel because page is not ready");

packages/pay/src/containers/DemoPage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@ const DemoPage = () => {
530530
counterValue: "1.00",
531531
request,
532532
loading: !request,
533-
setPending: () => {},
533+
setOnConfirmation: () => { },
534+
setPending: () => { },
534535
update: () => Promise.resolve(),
535536
}}
536537
>

packages/shared/src/contexts/RequestContext.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ interface IContext {
2020
counterCurrency: CurrencyDefinition;
2121
/** the request's expected amount in counter currency */
2222
counterValue?: string;
23+
setOnConfirmation: (onConfirmation: () => void) => void;
2324
/**
2425
* set the pending status for UX purposes
2526
* Pending means the payment is being processed and takes a long time.
@@ -72,6 +73,7 @@ export const RequestProvider: React.FC<{ chainId?: string | number }> = ({
7273
const counterCurrency = currencyManager.from("USD")!;
7374
const [counterValue, setCounterValue] = useState<string>("");
7475
const [pending, setPending] = useState(false);
76+
const [onConfirmation, setOnConfirmation] = useState<() => void>();
7577

7678
// gets counter currency rate
7779
const rate = useRate(parsedRequest?.currency, counterCurrency);
@@ -100,6 +102,9 @@ export const RequestProvider: React.FC<{ chainId?: string | number }> = ({
100102
});
101103
parseResult.loaded = true;
102104
setParsedRequest(parseResult);
105+
if (onConfirmation) {
106+
result.request.waitForConfirmation().then(onConfirmation);
107+
}
103108
}
104109
};
105110

@@ -125,6 +130,7 @@ export const RequestProvider: React.FC<{ chainId?: string | number }> = ({
125130
counterCurrency,
126131
counterValue,
127132
setPending,
133+
setOnConfirmation,
128134
update: useCallback(
129135
() => fetchRequest(id, chainId, pending),
130136
[id, chainId, pending]

0 commit comments

Comments
 (0)