|
1 | | -import { useState } from "react"; |
| 1 | +import { useEffect, useState } from "react"; |
2 | 2 | import { useAppSelector } from "store/store"; |
3 | 3 |
|
4 | 4 | import Button from "components/Button/Button"; |
@@ -49,7 +49,22 @@ const CreateCertificate = () => { |
49 | 49 | const [ showError, setShowError ] = useState(""); |
50 | 50 | const [ openModal, setOpenModal ] = useState(false); |
51 | 51 | const [ disableCertify, setDisableCertify ] = useState(false); |
| 52 | + const [certificationPrice, setCertificationPrice] = useState(0); |
| 53 | + const [performTransaction, setPerformTransaction] = useState(true); |
52 | 54 |
|
| 55 | + // to run only once initially |
| 56 | + useEffect(() => { |
| 57 | + fetchData.get('/profile/current/balance').then(response => { |
| 58 | + const availableProfileBalance: number = response.data |
| 59 | + fetchData.get('/run/' + uuid + '/details').then(res => { |
| 60 | + const runDetails: Run = res.data |
| 61 | + setCertificationPrice(runDetails.certificationPrice); |
| 62 | + setPerformTransaction((availableProfileBalance > 0 && (availableProfileBalance - runDetails.certificationPrice) < 0) ? true : false) |
| 63 | + }) |
| 64 | + }) |
| 65 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 66 | + }, []) |
| 67 | + |
53 | 68 | const onCloseModal = () => { setOpenModal(false) } |
54 | 69 |
|
55 | 70 | const convertAdaToLovelace = (fee_ada: number) => { |
@@ -112,17 +127,11 @@ const CreateCertificate = () => { |
112 | 127 | const triggerGetCertificate = async () => { |
113 | 128 | setCertifying(true); |
114 | 129 | setShowError("") |
115 | | - fetchData.get('/profile/current/balance').then(response => { |
116 | | - const availableProfileBalance: number = response.data |
117 | | - fetchData.get('/run/' + uuid + '/details').then(res => { |
118 | | - const runDetails: Run = res.data |
119 | | - if ((availableProfileBalance - runDetails.certificationPrice) < 0) { |
120 | | - triggerTransactionFromWallet(runDetails.certificationPrice) |
121 | | - } else { |
122 | | - triggerSubmitCertificate() |
123 | | - } |
124 | | - }) |
125 | | - }) |
| 130 | + if (performTransaction) { |
| 131 | + triggerTransactionFromWallet(certificationPrice) |
| 132 | + } else { |
| 133 | + triggerSubmitCertificate() |
| 134 | + } |
126 | 135 | } |
127 | 136 |
|
128 | 137 | const triggerTransactionFromWallet = async (cert_fee_in_lovelaces: number) => { |
@@ -197,7 +206,7 @@ const CreateCertificate = () => { |
197 | 206 | {certified || disableCertify ? null : (<Button |
198 | 207 | displayStyle="gradient" |
199 | 208 | onClick={() => triggerGetCertificate()} |
200 | | - buttonLabel="Get Certificate" |
| 209 | + buttonLabel={"Purchase a Certificate"+ (certificationPrice ? " (" + (certificationPrice/1000000).toString() + " ADA)" : "")} |
201 | 210 | showLoader={certifying} |
202 | 211 | />)} |
203 | 212 | {transactionId ? ( |
|
0 commit comments