Skip to content

Commit

Permalink
feat: adding store.ts to init vuex store
Browse files Browse the repository at this point in the history
  • Loading branch information
Grégor Sternat committed Aug 5, 2024
1 parent 729564f commit 0d5a680
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { createStore } from 'vuex';
import type { WalletClient } from 'viem';

interface Account {
name: string;
address: string;
reducedAddress: string;
mnemonic: string;
wallet: WalletClient;
}

interface State {
accounts: Account[];
selectedAccount: string | null;
}

const state: State = {
accounts: [],
selectedAccount: null,
};

const mutations = {
addAccount(state: State, account: Account) {
account.reducedAddress = `${account.address.slice(0, 7)}...${account.address.slice(-5)}`;
state.accounts.push(account);
state.selectedAccount = account.address;
},
selectAccount(state: State, address: string) {
state.selectedAccount = address;
},
clearAccounts(state: State) {
state.accounts = [];
state.selectedAccount = null;
}
};

const actions = {
addAccount({ commit }: { commit: Function }, account: Account) {
commit('addAccount', account);
},
selectAccount({ commit }: { commit: Function }, address: string) {
commit('selectAccount', address);
},
clearAccounts({ commit }: { commit: Function }) {
commit('clearAccounts');
}
};

const getters = {
accounts: (state: State) => state.accounts,
selectedAccount: (state: State) => state.selectedAccount,
accountReducedAddresses: (state: State) => {
return state.accounts.map(account => ({
...account,
reducedAddress: `${account.address.slice(0, 7)}...${account.address.slice(-5)}`
}));
}
};

const store = createStore({
state,
mutations,
actions,
getters
});

export default store;

0 comments on commit 0d5a680

Please sign in to comment.