redux-persist icon indicating copy to clipboard operation
redux-persist copied to clipboard

When should I add new migration to the state?

Open alamenai opened this issue 1 year ago • 0 comments

My current state looks like :


/* eslint-disable @typescript-eslint/no-explicit-any */
export const migrations = {
  0: (state: any) => {
    return {
      ...state,
    }
  },
  1: (state: any) => {
    const measures = state.customInstallation.panel.measures || {}
    return {
      ...state,
      customInstallation: {
        ...state.customInstallation,
        panel: {
          ...state.customInstallation.panel,
          measures: {
            ...measures,
            neighborSpace: measures.neighborSpace || 0.5,
            obstructionSpace: measures.obstructionSpace || measures.roofSuperStructure || 0.3,
          },
        },
      },
    }
  },
  2: (state: any) => {
    return {
      ...state,
      organization: {
        adminDeleteEnabled: false,
        createdAt: undefined,
        createdBy: undefined,
        hasImage: false,
        id: undefined,
        imageUrl: undefined,
        maxAllowedMemberships: 0,
        name: undefined,
        privateMetadata: {},
        publicMetadata: {},
        slug: undefined,
        updatedAt: undefined,
      },
    }
  },
}

I have created new slice with its reducers and added it to the store :

import { createSlice } from "@reduxjs/toolkit"

import { BuildingAddress } from "../types"
import { reducers } from "./reducers"

export const initialState: BuildingAddress = {
  id: undefined,
  city: undefined,
  houseNumber: undefined,
  street: undefined,
  zipCode: undefined,
}

export const buildingAddressSlice = createSlice({
  name: "buildingAddress",
  initialState,
  reducers,
})

export const { initialAddress, addressChanged } = buildingAddressSlice.actions

export default buildingAddressSlice.reducer

I just confused if should I add next migration or it's made when my current state change it's structure ( not the new added ones ).

I mean like this :


/* eslint-disable @typescript-eslint/no-explicit-any */
export const migrations = {
  0: (state: any) => {
    return {
      ...state,
    }
  },
  1: (state: any) => {
    const measures = state.customInstallation.panel.measures || {}
    return {
      ...state,
      customInstallation: {
        ...state.customInstallation,
        panel: {
          ...state.customInstallation.panel,
          measures: {
            ...measures,
            neighborSpace: measures.neighborSpace || 0.5,
            obstructionSpace: measures.obstructionSpace || measures.roofSuperStructure || 0.3,
          },
        },
      },
    }
  },
  2: (state: any) => {
    return {
      ...state,
      organization: {
        adminDeleteEnabled: false,
        createdAt: undefined,
        createdBy: undefined,
        hasImage: false,
        id: undefined,
        imageUrl: undefined,
        maxAllowedMemberships: 0,
        name: undefined,
        privateMetadata: {},
        publicMetadata: {},
        slug: undefined,
        updatedAt: undefined,
      },
    }
  },
3:(state:any)=>{
  return { 
    ...state,
   buildingAddress:initialBuildingAddress
}
}

alamenai avatar Aug 09 '24 10:08 alamenai