Skip to content
Open
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
50 changes: 28 additions & 22 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
119 changes: 66 additions & 53 deletions src/components/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,63 +68,67 @@ export default function Home({
}, []);

const createRequest = async () => {
setLoading(true);
createRequestAction(address, { ...newRequest, createdAt: Date.now() })
.then(() => {
toast(<NotificationSuccess text="Request added successfully." />);
getRequests();
fetchBalance(address);
})
.catch((error) => {
console.log(error);
toast(<NotificationError text="Failed to create a request." />);
setLoading(false);
});
try {
setLoading(true);
await createRequestAction(address, {...newRequest, createdAt: Date.now()})

toast(<NotificationSuccess text="Request added successfully."/>);
getRequests();
fetchBalance(address);

} catch (error) {
console.log(error);
toast(<NotificationError text="Failed to create a request."/>);
} finally {
setLoading(false);
}
};

const editRequest = async (editedRequest) => {
setLoading(true);
editRequestAction(address, editedRequest)
.then(() => {
toast(<NotificationSuccess text="Request edited successfully." />);
getRequests();
fetchBalance(address);
})
.catch((error) => {
console.log(error);
toast(<NotificationError text="Failed to edit request." />);
setLoading(false);
});
try {
setLoading(true);
await editRequestAction(address, editedRequest)
toast(<NotificationSuccess text="Request edited successfully."/>);
getRequests();
fetchBalance(address);
} catch (error) {
console.log(error);
toast(<NotificationError text="Failed to edit request."/>);
} finally {
setLoading(false);
}

};

const donateRequest = async (request, amount) => {
setLoading(true);
donateRequestAction(address, request, amount)
.then(() => {
toast(<NotificationSuccess text="Donated successfully" />);
getRequests();
fetchBalance(address);
})
.catch((error) => {
console.log(error);
toast(<NotificationError text="Failed to donate request." />);
setLoading(false);
});
try {
setLoading(true);
await donateRequestAction(address, request, amount)
toast(<NotificationSuccess text="Donated successfully"/>);
getRequests();
fetchBalance(address);
} catch (error) {
console.log(error);
toast(<NotificationError text="Failed to donate request."/>);
setLoading(false);
}

};

const deleteRequest = async (request) => {
setLoading(true);
deleteRequestAction(address, request.appId)
.then(() => {
toast(<NotificationSuccess text="Request deleted successfully" />);
getRequests();
fetchBalance(address);
})
.catch((error) => {
console.log(error);
toast(<NotificationError text="Failed to delete request." />);
setLoading(false);
});
try {
setLoading(true);
await deleteRequestAction(address, request.appId)

toast(<NotificationSuccess text="Request deleted successfully"/>);
getRequests();
fetchBalance(address);
} catch (error) {
console.log(error);
toast(<NotificationError text="Failed to delete request."/>);
setLoading(false);
}

};

const isFormFilled = useCallback(() => {
Expand Down Expand Up @@ -261,11 +265,20 @@ export default function Home({
<div className="btn">
<span>Select Image</span>
<input
onChange={async (e) =>
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"
Expand Down