Skip to content

Commit bf411a9

Browse files
authored
[FIX] - Adding custom error modal script (#715)
* fix: custom modal * fix: connect to metamask script
1 parent 190dcbe commit bf411a9

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

js/connect-to-metamask.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,52 @@ const supportedNetworks = {
1919
NOTE: This calls "eth_requestAccounts" at the end, which prompts for wallet connection
2020
*/
2121
const connectNetwork = async (network) => {
22+
if (!provider) {
23+
handleError('No Ethereum-compatible wallet found. Please install MetaMask.');
24+
return;
25+
}
2226
try {
2327
const targetNetwork = { ...supportedNetworks[network] };
24-
delete targetNetwork.name; // remove 'name' property if needed
28+
delete targetNetwork.name;
2529

26-
await provider.request({
27-
method: 'wallet_addEthereumChain',
28-
params: [targetNetwork],
29-
});
30-
// This line requests user accounts, which triggers a "connect" prompt if not already connected:
30+
// Try to switch first, then add if not available
31+
try {
32+
await provider.request({
33+
method: 'wallet_switchEthereumChain',
34+
params: [{ chainId: targetNetwork.chainId }],
35+
});
36+
} catch (switchError) {
37+
// 4902 = chain not added
38+
if (switchError.code === 4902) {
39+
await provider.request({
40+
method: 'wallet_addEthereumChain',
41+
params: [targetNetwork],
42+
});
43+
} else if (switchError.code !== 4001 && switchError.code !== -32002) {
44+
handleError(switchError.message);
45+
return;
46+
}
47+
}
48+
// Request accounts after switching/adding
3149
await provider.request({ method: 'eth_requestAccounts' });
3250
} catch (e) {
33-
// 4001: user rejected, -32002: request already pending
3451
if (e.code !== 4001 && e.code !== -32002) {
3552
handleError(e.message);
3653
}
3754
}
3855
};
3956

40-
// Get the network that the user is currently connected to
57+
// Fix for getConnectedNetwork
4158
const getConnectedNetwork = async () => {
4259
const chainId = await provider.request({ method: 'eth_chainId' });
4360
const connectedHubNetwork = Object.values(supportedNetworks).find(
4461
(network) => network.chainId === chainId
4562
);
46-
if (connectedNetwork) {
63+
if (connectedHubNetwork) {
4764
const connectedNetworkButton = document.querySelector(
48-
`.connect-network[data-value="${connectedNetwork.name}"]`
65+
`.connect-network[data-value="${connectedHubNetwork.name}"]`
4966
);
50-
return { connectedNetwork, connectedNetworkButton };
67+
return { connectedNetwork: connectedHubNetwork, connectedNetworkButton };
5168
} else {
5269
return {
5370
connectedNetwork: null,
@@ -65,6 +82,7 @@ const displayConnectedAccount = async (connectedNetwork, networkButton) => {
6582
-4
6683
)}`;
6784
networkButton.innerHTML = `Connected to ${connectedNetwork.chainName}: ${shortenedAccount}`;
85+
networkButton.disabled = true;
6886
networkButton.className += ' disabled-button';
6987
};
7088

@@ -102,6 +120,7 @@ connectMetaMaskBodyButtons.forEach((btn) => {
102120
await connectNetwork(network);
103121
//Update the button to reflect the "connected" state
104122
btn.textContent = 'Connected to ' + supportedNetworks[network]['name'];
123+
btn.disabled = true;
105124
btn.classList.add('disabled-button');
106125
});
107126
});
@@ -124,4 +143,4 @@ if (provider) {
124143
window.location.reload();
125144
}
126145
});
127-
}
146+
}

js/error-modal.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/** Grab the body so we can append the modal to it */
2+
const main = document.querySelector('main');
3+
4+
/** Create the modal */
5+
const modalContainer = document.createElement('div');
6+
const modal = document.createElement('div');
7+
const modalHeader = document.createElement('h3');
8+
const modalMessage = document.createElement('p');
9+
const closeErrorModal = document.createElement('span');
10+
11+
/** Add classes to modal elements so we can find and update as needed */
12+
modalContainer.className = 'error-modal-container';
13+
modalHeader.className = 'error-modal-header';
14+
modal.className = 'error-modal';
15+
modalMessage.className = 'error-message';
16+
closeErrorModal.className = 'close-modal';
17+
18+
/** Set the display to none to hide the modal until it is needed */
19+
modalContainer.style.display = 'none';
20+
21+
/** Set generic header for the error modal */
22+
modalHeader.textContent = 'There was a problem connecting to your wallet';
23+
24+
/** Set up close button */
25+
closeErrorModal.innerHTML = '×';
26+
closeErrorModal.onclick = () => {
27+
modalContainer.style.display = 'none';
28+
};
29+
30+
/** Put the modal together and append it to the main area on the page */
31+
modal.appendChild(closeErrorModal);
32+
modal.appendChild(modalHeader);
33+
modal.appendChild(modalMessage);
34+
modalContainer.appendChild(modal);
35+
main.append(modalContainer);

0 commit comments

Comments
 (0)