Skip to content

Commit

Permalink
Created first migration for persistentData
Browse files Browse the repository at this point in the history
  • Loading branch information
Freshenext committed Oct 3, 2023
1 parent 53aae1b commit a8bad8f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
28 changes: 27 additions & 1 deletion src/redux/rootReducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { combineReducers } from '@reduxjs/toolkit'
import { persistReducer, createMigrate, PersistConfig } from 'redux-persist'
import {
persistReducer,
createMigrate,
PersistConfig,
getStoredState,
} from 'redux-persist'

import { reduxStorage } from 'storage/ReduxStorage'
import { contactsReducer } from 'store/slices/contactsSlice'
Expand Down Expand Up @@ -33,6 +38,26 @@ const migrations = {
}),
}

const firstPersistentDataMigration = async state => {
// First migration, check if state already exists
if (!state) {
// First step is to get old settings storage - usually testnet storage has all the info
const oldStorage = await getStoredState({
key: 'settings',
storage: reduxStorage(31),
})
// If old storage exists, we will migrate it to the current state
if (oldStorage) {
return {
keysExist: oldStorage.keysExist,
isFirstLaunch: oldStorage.isFirstLaunch,
pin: oldStorage.pin,
}
}
}
return state
}

export const createRootReducer = () => {
const persistedReduxStorageAcrossChainSwitches = reduxStorage(0)

Expand All @@ -46,6 +71,7 @@ export const createRootReducer = () => {
key: 'persistentData',
whitelist: ['keysExist', 'isFirstLaunch', 'pin'],
storage: persistedReduxStorageAcrossChainSwitches,
migrate: firstPersistentDataMigration,
}

const usdPricesPersistConfig: PersistConfig<UsdPricesState> = {
Expand Down
5 changes: 2 additions & 3 deletions src/redux/slices/persistentDataSlice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit'

import { PersistentDataState } from './types'

// @TODO switch back variable state after 15 days - 1 month (november) (keysExist false by default, isFirst.. true by default)
const initialState: PersistentDataState = {
keysExist: true,
isFirstLaunch: false,
keysExist: false,
isFirstLaunch: true,
pin: null,
}

Expand Down

0 comments on commit a8bad8f

Please sign in to comment.