Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
Handle wallet account change (#4)
Sebastian Martinez committed 4 years ago
commit 30d969e3acec09f3428f36560ad5ef18ae0022d2
parent 756c40d2024be82f6a8293b59c28ffd888deb4fe
1 file changed +14 -1
modified src/session.ts
@@ -1,4 +1,3 @@
-
// TODO: Handle wallet account change.
import { get, writable, derived, Writable } from "svelte/store";
import { ethers } from "ethers";
import type { BigNumber } from 'ethers';
@@ -143,6 +142,17 @@ export const createState = (initial: State) => {
        }
      });
    },
+
    setChangedAccount: ([address]: string[]) => {
+
      store.update(s => {
+
        switch (s.connection) {
+
          case Connection.Connected:
+
            s.session.address = address; 
+
            return s;
+
          default:
+
            return s;
+
        }
+
      })
+
    }
  };
};

@@ -154,6 +164,9 @@ export const session = derived(state, s => {
  return null;
});

+
// Updates state when user changes accounts
+
window.ethereum?.on("accountsChanged", state.setChangedAccount);
+

state.subscribe(s => {
  console.log("session.state", s);
});