Я не могу очистить свой магазин при выходе из системы - PullRequest
0 голосов
/ 03 февраля 2020

Я пытаюсь очистить свой магазин Redux после выхода из системы, но я не могу этого добиться. Я использую Persister для хранения. Вот код для currencyReducer:

import {
  FETCH_CURRENCIES,
  DELETE_CURRENCIES
} from "../actionCreator/actionCreator";

import { persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";

const initState = [];

const currenciesReducer = persistReducer(
  { storage, key: "currency"},
  (state = initState, { type, payload }) => {
    switch (type) {
      case FETCH_CURRENCIES:
        return {data:payload.data};
      case DELETE_CURRENCIES:
        var ar=[];
        ar=state.data.filter(el => el._id !== payload)
        return {data:ar};
      default:
        return state;
    }
  }
);
export default currenciesReducer;

, а вот маршрутизатор выхода из системы:

import { persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import { put, takeLatest } from "redux-saga/effects";
import { getUserByToken } from "../../crud/auth.crud";
import * as routerHelpers from "../../router/RouterHelpers";

export const actionTypes = {
  Login: "[Login] Action",
  Logout: "[Logout] Action",
  Register: "[Register] Action",
  UserRequested: "[Request User] Action",
  UserLoaded: "[Load User] Auth API"
};

const initialAuthState = {
  user: undefined,
  authToken: undefined
};

export const reducer = persistReducer(
  { storage, key: "demo2-auth", whitelist: ["user", "authToken"] },
  (state = initialAuthState, action) => {
    switch (action.type) {
      case actionTypes.Login: {
        const { authToken } = action.payload;

        return { authToken, user: undefined };
      }

      case actionTypes.Register: {
        const { authToken } = action.payload;

        return { authToken, user: undefined };
      }

      case actionTypes.Logout: {
        routerHelpers.forgotLastLocation();
        storage.removeItem('persist:currency');
        return initialAuthState;
      }

      case actionTypes.UserLoaded: {
        const { user } = action.payload;

        return { ...state, user };
      }

      default:
        return state;
    }
  }
);

export const actions = {
  login: authToken => ({ type: actionTypes.Login, payload: { authToken } }),
  register: authToken => ({
    type: actionTypes.Register,
    payload: { authToken }
  }),
  logout: () => ({ type: actionTypes.Logout }),
  requestUser: user => ({ type: actionTypes.UserRequested, payload: { user } }),
  fulfillUser: user => ({ type: actionTypes.UserLoaded, payload: { user } })
};

export function* saga() {
  yield takeLatest(actionTypes.Login, function* loginSaga() {
    yield put(actions.requestUser());
  });

  yield takeLatest(actionTypes.Register, function* registerSaga() {
    yield put(actions.requestUser());
  });

  yield takeLatest(actionTypes.UserRequested, function* userRequested() {
    const { data: user } = yield getUserByToken();

    yield put(actions.fulfillUser(user));
  });
}

Я думаю, что неправильно задаю строку storage.removeItem('persist:currency');. Кто-нибудь может меня поправить?

Я пробовал разные варианты, но, похоже, они не работают.

...