Skip to content

Commit

Permalink
Init Contacts Redux store
Browse files Browse the repository at this point in the history
  • Loading branch information
buberdds committed Sep 6, 2023
1 parent c4f326e commit 0d93d08
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/app/state/contacts/index.ts
@@ -0,0 +1,25 @@
import { PayloadAction } from '@reduxjs/toolkit'
import { createSlice } from 'utils/@reduxjs/toolkit'
import { ContactsState, Contact } from './types'

export const initialState: ContactsState = {}

const slice = createSlice({
name: 'contacts',
initialState,
reducers: {
add(state, action: PayloadAction<Contact>) {
state[action.payload.address] = action.payload

Check warning on line 12 in src/app/state/contacts/index.ts

View check run for this annotation

Codecov / codecov/patch

src/app/state/contacts/index.ts#L11-L12

Added lines #L11 - L12 were not covered by tests
},
update(state, action: PayloadAction<Contact>) {
state[action.payload.address] = action.payload

Check warning on line 15 in src/app/state/contacts/index.ts

View check run for this annotation

Codecov / codecov/patch

src/app/state/contacts/index.ts#L14-L15

Added lines #L14 - L15 were not covered by tests
},
delete(state, action: PayloadAction<string>) {
delete state[action.payload]

Check warning on line 18 in src/app/state/contacts/index.ts

View check run for this annotation

Codecov / codecov/patch

src/app/state/contacts/index.ts#L17-L18

Added lines #L17 - L18 were not covered by tests
},
},
})

export const { actions: contactsActions } = slice

export default slice.reducer
15 changes: 15 additions & 0 deletions src/app/state/contacts/selectors.ts
@@ -0,0 +1,15 @@
import { createSelector } from '@reduxjs/toolkit'

Check warning on line 1 in src/app/state/contacts/selectors.ts

View check run for this annotation

Codecov / codecov/patch

src/app/state/contacts/selectors.ts#L1

Added line #L1 was not covered by tests

import { RootState } from 'types'
import { initialState } from '.'

Check warning on line 4 in src/app/state/contacts/selectors.ts

View check run for this annotation

Codecov / codecov/patch

src/app/state/contacts/selectors.ts#L4

Added line #L4 was not covered by tests

const selectSlice = (state: RootState) => state.contacts || initialState

Check warning on line 6 in src/app/state/contacts/selectors.ts

View check run for this annotation

Codecov / codecov/patch

src/app/state/contacts/selectors.ts#L6

Added line #L6 was not covered by tests

export const selectContactsList = createSelector([selectSlice], contacts =>
Object.keys(contacts).map(contact => ({

Check warning on line 9 in src/app/state/contacts/selectors.ts

View check run for this annotation

Codecov / codecov/patch

src/app/state/contacts/selectors.ts#L8-L9

Added lines #L8 - L9 were not covered by tests
address: contacts[contact].address,
name: contacts[contact].name,
})),
)

export const selectContact = (address: string) => createSelector([selectSlice], contacts => contacts[address])

Check warning on line 15 in src/app/state/contacts/selectors.ts

View check run for this annotation

Codecov / codecov/patch

src/app/state/contacts/selectors.ts#L15

Added line #L15 was not covered by tests
9 changes: 9 additions & 0 deletions src/app/state/contacts/types.ts
@@ -0,0 +1,9 @@
export interface Contact {
address: string
name: string
}

/* --- STATE --- */
export interface ContactsState {
[key: string]: Contact
}
1 change: 1 addition & 0 deletions src/app/state/persist/index.ts
Expand Up @@ -101,6 +101,7 @@ export function receivePersistedRootState(
): RootState {
return {
...prevState,
contacts: persistedRootState.contacts,
theme: persistedRootState.theme,
wallet: persistedRootState.wallet,
network: persistedRootState.network,
Expand Down
1 change: 1 addition & 0 deletions src/app/state/persist/saga.ts
Expand Up @@ -116,6 +116,7 @@ function* resetRootState(action: ReturnType<typeof persistActions.resetRootState
// Encrypting
async function encryptState(state: RootState, keyWithSalt: KeyWithSalt): Promise<EncryptedString> {
const persistedRootState: PersistedRootState = {
contacts: state.contacts,
theme: state.theme,
wallet: state.wallet,
network: state.network,
Expand Down
12 changes: 11 additions & 1 deletion src/app/state/persist/syncTabs.ts
Expand Up @@ -9,6 +9,7 @@ import {
import { networkActions } from 'app/state/network'
import { isSyncingTabsSupported, needsSyncingTabs, persistActions } from 'app/state/persist'
import { walletActions } from 'app/state/wallet'
import { contactsActions } from 'app/state/contacts'
import { themeActions } from 'styles/theme/slice'
import {
createStateSyncMiddleware,
Expand Down Expand Up @@ -42,6 +43,9 @@ export function receiveInitialTabSyncState(
* before {@link receiveInitialTabSyncState}!
*/
export const whitelistTabSyncActions = [
contactsActions.add.type,
contactsActions.update.type,
contactsActions.delete.type,
themeActions.changeTheme.type,
walletActions.walletOpened.type,
walletActions.updateBalance.type,
Expand All @@ -59,7 +63,13 @@ const stateSyncConfig: StateSyncConfig = {
},
whitelist: whitelistTabSyncActions,
prepareState: (state: RootState): SyncedRootState => {
return { theme: state.theme, wallet: state.wallet, network: state.network, persist: state.persist }
return {

Check warning on line 66 in src/app/state/persist/syncTabs.ts

View check run for this annotation

Codecov / codecov/patch

src/app/state/persist/syncTabs.ts#L66

Added line #L66 was not covered by tests
contacts: state.contacts,
theme: state.theme,
wallet: state.wallet,
network: state.network,
persist: state.persist,
}
},
}

Expand Down
5 changes: 3 additions & 2 deletions src/app/state/persist/types.ts
Expand Up @@ -33,5 +33,6 @@ export interface PersistState {
enteredWrongPassword: boolean
}

export interface PersistedRootState extends Pick<RootState, 'theme' | 'wallet' | 'network'> {}
export interface SyncedRootState extends Pick<RootState, 'theme' | 'wallet' | 'network' | 'persist'> {}
export interface PersistedRootState extends Pick<RootState, 'contacts' | 'theme' | 'wallet' | 'network'> {}
export interface SyncedRootState
extends Pick<RootState, 'contacts' | 'theme' | 'wallet' | 'network' | 'persist'> {}
2 changes: 2 additions & 0 deletions src/store/reducers.ts
Expand Up @@ -11,6 +11,7 @@ import importAccountsReducer from 'app/state/importaccounts'
import networkReducer from 'app/state/network'
import paraTimesReducer from 'app/state/paratimes'
import stakingReducer from 'app/state/staking'
import contactsReducer from 'app/state/contacts'
import transactionReducer from 'app/state/transaction'
import walletReducer from 'app/state/wallet'
import themeReducer from 'styles/theme/slice'
Expand All @@ -20,6 +21,7 @@ import { RootState } from 'types'
function createRootReducer() {
const rootReducer = combineReducers({
account: accountReducer,
contacts: contactsReducer,
createWallet: createWalletReducer,
fiatOnramp: fiatOnrampReducer,
fatalError: fatalErrorReducer,
Expand Down
2 changes: 2 additions & 0 deletions src/types/RootState.ts
Expand Up @@ -4,6 +4,7 @@ import { WalletState } from 'app/state/wallet/types'
import { CreateWalletState } from 'app/pages/CreateWalletPage/slice/types'
import { FiatOnrampState } from 'app/pages/FiatOnrampPage/slice/types'
import { AccountState } from 'app/state/account/types'
import { ContactsState } from 'app/state/contacts/types'
import { NetworkState } from 'app/state/network/types'
import { TransactionState } from 'app/state/transaction/types'
import { ImportAccountsState } from 'app/state/importaccounts/types'
Expand All @@ -20,6 +21,7 @@ import { receiveInitialTabSyncState, whitelistTabSyncActions } from 'app/state/p

export interface RootState {
/** Stored slices, see {@link receivePersistedRootState} */
contacts: ContactsState
theme: ThemeState
wallet: WalletState
network: NetworkState
Expand Down

0 comments on commit 0d93d08

Please sign in to comment.