Skip to content

Commit

Permalink
Remove accounts'batches from the store on accounts removal
Browse files Browse the repository at this point in the history
  • Loading branch information
asiia-trilitech committed Mar 14, 2024
1 parent c4d8c1c commit 0c56a09
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/utils/hooks/removeAccountDataHooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { accountsSlice } from "../redux/slices/accountsSlice";
import { store } from "../redux/store";

describe("useRemoveAccount", () => {
test("deletes relevant data from accounts slice", () => {
test("deletes secret key on deleting secret key account", () => {
const account = mockSecretKeyAccount(0);
store.dispatch(accountsSlice.actions.addAccount(account));
store.dispatch(
Expand Down
34 changes: 33 additions & 1 deletion src/utils/hooks/removeAccountDataHooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { AccountType, LedgerAccount, SecretKeyAccount, SocialAccount } from "../../types/Account";
import { useGetAccountsByFingerPrint, useGetAccountsByType } from "./getAccountDataHooks";
import {
Account,
AccountType,
LedgerAccount,
SecretKeyAccount,
SocialAccount,
} from "../../types/Account";
import { useAppDispatch } from "../redux/hooks";
import { accountsSlice } from "../redux/slices/accountsSlice";
import { batchesSlice } from "../redux/slices/batches";

const { removeMnemonicAndAccounts, removeNonMnemonicAccounts } = accountsSlice.actions;

Expand All @@ -9,7 +17,12 @@ const { removeMnemonicAndAccounts, removeNonMnemonicAccounts } = accountsSlice.a
*/
export const useRemoveMnemonic = () => {
const dispatch = useAppDispatch();
const getAccountsByFingerPrint = useGetAccountsByFingerPrint();
const removeAccountsDependencies = useRemoveAccountsDependencies();

return (fingerPrint: string) => {
removeAccountsDependencies(getAccountsByFingerPrint(fingerPrint));

dispatch(
removeMnemonicAndAccounts({
fingerPrint,
Expand All @@ -23,7 +36,12 @@ export const useRemoveMnemonic = () => {
*/
export const useRemoveNonMnemonic = () => {
const dispatch = useAppDispatch();
const getAccountsByType = useGetAccountsByType();
const removeAccountsDependencies = useRemoveAccountsDependencies();

return (accountType: AccountType) => {
removeAccountsDependencies(getAccountsByType(accountType));

Check warning on line 43 in src/utils/hooks/removeAccountDataHooks.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

dispatch(
removeNonMnemonicAccounts({
accountType,
Expand All @@ -37,8 +55,22 @@ export const useRemoveNonMnemonic = () => {
*/
export const useRemoveAccount = () => {
const dispatch = useAppDispatch();
const removeAccountsDependencies = useRemoveAccountsDependencies();

return (account: SocialAccount | LedgerAccount | SecretKeyAccount) => {
removeAccountsDependencies([account]);
dispatch(accountsSlice.actions.removeAccount(account));
};
};

/**
* Hook for removing all stored accounts' dependencies.
*/
const useRemoveAccountsDependencies = () => {
const dispatch = useAppDispatch();

return (accounts: Account[]) => {
const pkhs = accounts.map(account => account.address.pkh);
dispatch(batchesSlice.actions.removeByAccounts({ pkhs }));
};
};

0 comments on commit 0c56a09

Please sign in to comment.