diff --git a/src/App.js b/src/App.js index cf8528c..7282ce8 100644 --- a/src/App.js +++ b/src/App.js @@ -10,31 +10,37 @@ const App = function AppWrapper() { const [balance, setBalance] = useState(0); const fetchBalance = async (accountAddress) => { - indexerClient - .lookupAccountByID(accountAddress) - .do() - .then((response) => { - const _balance = response.account.amount; - setBalance(_balance); - }) - .catch((error) => { - console.log(error); - }); + try { + const response = await indexerClient + .lookupAccountByID(accountAddress) + .do() + + if (!response) return + + const _balance = response.account.amount; + setBalance(_balance); + + } catch (error) { + console.log(error); + } + }; const connectWallet = async () => { - myAlgoConnect - .connect() - .then((accounts) => { - const _account = accounts[0]; - setAddress(_account.address); - setName(_account.name); - fetchBalance(_account.address); - }) - .catch((error) => { - console.log("Could not connect to MyAlgo wallet"); - console.error(error); - }); + try { + const accounts = await myAlgoConnect + .connect() + + const _account = accounts[0]; + setAddress(_account.address); + setName(_account.name); + fetchBalance(_account.address); + + } catch (error) { + console.log("Could not connect to MyAlgo wallet"); + console.error(error); + } + }; const disconnect = () => { diff --git a/src/components/Home.jsx b/src/components/Home.jsx index 108015e..047a4c8 100644 --- a/src/components/Home.jsx +++ b/src/components/Home.jsx @@ -68,63 +68,67 @@ export default function Home({ }, []); const createRequest = async () => { - setLoading(true); - createRequestAction(address, { ...newRequest, createdAt: Date.now() }) - .then(() => { - toast(); - getRequests(); - fetchBalance(address); - }) - .catch((error) => { - console.log(error); - toast(); - setLoading(false); - }); + try { + setLoading(true); + await createRequestAction(address, {...newRequest, createdAt: Date.now()}) + + toast(); + getRequests(); + fetchBalance(address); + + } catch (error) { + console.log(error); + toast(); + } finally { + setLoading(false); + } }; const editRequest = async (editedRequest) => { - setLoading(true); - editRequestAction(address, editedRequest) - .then(() => { - toast(); - getRequests(); - fetchBalance(address); - }) - .catch((error) => { - console.log(error); - toast(); - setLoading(false); - }); + try { + setLoading(true); + await editRequestAction(address, editedRequest) + toast(); + getRequests(); + fetchBalance(address); + } catch (error) { + console.log(error); + toast(); + } finally { + setLoading(false); + } + }; const donateRequest = async (request, amount) => { - setLoading(true); - donateRequestAction(address, request, amount) - .then(() => { - toast(); - getRequests(); - fetchBalance(address); - }) - .catch((error) => { - console.log(error); - toast(); - setLoading(false); - }); + try { + setLoading(true); + await donateRequestAction(address, request, amount) + toast(); + getRequests(); + fetchBalance(address); + } catch (error) { + console.log(error); + toast(); + setLoading(false); + } + }; const deleteRequest = async (request) => { - setLoading(true); - deleteRequestAction(address, request.appId) - .then(() => { - toast(); - getRequests(); - fetchBalance(address); - }) - .catch((error) => { - console.log(error); - toast(); - setLoading(false); - }); + try { + setLoading(true); + await deleteRequestAction(address, request.appId) + + toast(); + getRequests(); + fetchBalance(address); + } catch (error) { + console.log(error); + toast(); + setLoading(false); + } + }; const isFormFilled = useCallback(() => { @@ -261,11 +265,20 @@ export default function Home({
Select Image - setNewRequest({ - ...newRequest, - image: await uploadToIpfs(e), - }) + onChange={async (e) => { + try { + setLoading(true) + const uploadImage = await uploadToIpfs(e) + setNewRequest({ + ...newRequest, + image: uploadImage, + }) + } catch (e) { + console.log({e}) + } finally { + setLoading(false) + } + } } type="file" name="image"