Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

mantle-demo - frontend - Move the side effects from reducers. #73

@TdfGomes

Description

@TdfGomes

Other thing I change in the project I worked using this base-app was in the auth reducer.
I move this :

case LOAD_MNEMONIC_SUCCESS:
      localStorage.setItem(localStorage.keys.tempMnemonic, action.payload.mnemonic)

and this :

case LOAD_MNEMONIC_PERSIST: {
      const mnemonic = localStorage.getItem(localStorage.keys.tempMnemonic)
      localStorage.removeItem(localStorage.keys.tempMnemonic)

to the auth saga
so I wrap the saga like this:

export function* loadAccount(action) {
  let mnemonic
  if (action.payload === 'from_store') {
    mnemonic = yield select(state => state.auth.mnemonic)
  } else {
    mnemonic = action.payload
  }
  if (mnemonic) {
    try {
      const authData = performLoadMnemonic(mnemonic)
      yield put(loadAccountSuccess(authData))
      if (action.payload !== 'from_store') {
        yield call(
          [ localStorage, 'setItem' ],
          localStorage.keys.tempMnemonic, action.payload
        )
      } else {
        yield call(
          [ localStorage, 'setItem' ],
          localStorage.keys.tempMnemonic, mnemonic
        )
      }
      const mnemonicFromStorage = yield call(
        [ localStorage, 'getItem' ],
        localStorage.keys.tempMnemonic
      )
      yield call([ localStorage, 'removeItem' ], localStorage.keys.tempMnemonic)
      yield put(loadAccountPersist(mnemonicFromStorage))
    } catch (error) {
      console.error(error)
      yield put(loadAccountFail())
    }
  }
}

I think it's better to have this localstorage calls inside the a saga.
I agree this saga could be improved but I think it's a start.

Metadata

Metadata

Assignees

No one assigned

    Labels

    frontendIssues and features relating to frontend

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions