Skip to content

Commit

Permalink
reset state on wallet address change
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi committed Oct 2, 2024
1 parent 7057897 commit a356a05
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/background/services/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export class Background {

case 'CONNECT_WALLET':
await this.openPaymentsService.connectWallet(message.payload);
if (message.payload.recurring) {
if (message.payload?.recurring) {
this.scheduleResetOutOfFundsState();
}
return success(undefined);
Expand Down
13 changes: 7 additions & 6 deletions src/background/services/openPayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,13 @@ export class OpenPaymentsService {
});
}

async connectWallet({
walletAddressUrl,
amount,
recurring,
skipAutoKeyShare,
}: ConnectWalletPayload) {
async connectWallet(params: ConnectWalletPayload | null) {
if (!params) {
this.setConnectState(null);
return;
}
const { walletAddressUrl, amount, recurring, skipAutoKeyShare } = params;

const walletAddress = await getWalletInformation(walletAddressUrl);
const exchangeRates = await getExchangeRates();

Expand Down
10 changes: 10 additions & 0 deletions src/popup/components/ConnectWalletForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface ConnectWalletFormProps {
saveValue?: (key: keyof Inputs, val: Inputs[typeof key]) => void;
getWalletInfo: (walletAddressUrl: string) => Promise<WalletAddress>;
connectWallet: (data: ConnectWalletPayload) => Promise<Response>;
clearConnectState: () => Promise<unknown>;
onConnect?: () => void;
}

Expand All @@ -46,6 +47,7 @@ export const ConnectWalletForm = ({
state,
getWalletInfo,
connectWallet,
clearConnectState,
saveValue = () => {},
onConnect = () => {},
}: ConnectWalletFormProps) => {
Expand All @@ -64,6 +66,12 @@ export const ConnectWalletForm = ({
isAutoKeyAddFailed(state),
);

const resetState = React.useCallback(async () => {
await clearConnectState();
setErrors((_) => ({ ..._, keyPair: '', connect: '' }));
setAutoKeyShareFailed(false);
}, [clearConnectState]);

const [walletAddressInfo, setWalletAddressInfo] =
React.useState<WalletAddress | null>(null);

Expand Down Expand Up @@ -268,6 +276,7 @@ export const ConnectWalletForm = ({
}
}
const ok = await handleWalletAddressUrlChange(value, input);
resetState();
if (ok) document.getElementById('connectAmount')?.focus();
}}
onBlur={async (ev) => {
Expand All @@ -278,6 +287,7 @@ export const ConnectWalletForm = ({
}
}
await handleWalletAddressUrlChange(value, ev.currentTarget);
resetState();
}}
/>

Expand Down
1 change: 1 addition & 0 deletions src/popup/pages/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const Component = () => {
// But we reload it, as it's open all-time when running E2E tests
window.location.reload();
}}
clearConnectState={() => message.send('CONNECT_WALLET', null)}
/>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/shared/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export type PopupToBackgroundMessage = {
output: PopupState;
};
CONNECT_WALLET: {
input: ConnectWalletPayload;
input: null | ConnectWalletPayload;
output: void;
};
RECONNECT_WALLET: {
Expand Down

0 comments on commit a356a05

Please sign in to comment.