Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question on how to use the reducer right way #240

Open
Lyba97 opened this issue Sep 2, 2022 · 2 comments
Open

Question on how to use the reducer right way #240

Lyba97 opened this issue Sep 2, 2022 · 2 comments

Comments

@Lyba97
Copy link

Lyba97 commented Sep 2, 2022

I am working on redux for state management and using MVVM architecture. 

I have made a generic reducer that only returns the updated store. For each feature I make a class, let’s call it a sub-store, and make this class in AppStore as a data member to maintain its own state. Now for each sub-store, I made a reducer that extends the generic reducer for common operations. My generic reducer takes the old sub-store and takes the new sub-store as action and depending on the API state e.g. (‘isLoading’, ’success’), updates the store. Since reducers are supposed to have logic. I am processing and calculating the new sub-store state either in thunkAction or in ViewModel and dispatching the Action only when the new sub-store is ready to be updated. Let's say I want to update the data member isNewUser. I will do this in ThunkAction:


UserLoginStore loginStore = store.state.userLoginStore.copyWith(isNewUser: true);
store.dispatch(ApiResponseAction<UserLoginStore>(apiResState: ApiResState.success, updateStore: loginStore));



Am I doing it right? In this way, the reducer does not have much logic despite some specific functionalities that may be required by a specific sub-store. 

As it can be seen in the above snippet I created an instance of UserLoginStore and kept the updated store in that and then dispatched the action to the reducer where the actual state will be updated for the application.

// sub-store
class 
UserLoginStore {
  final UserModel userModel;
  final bool isNewUser;
  final ApiResState apiResState;
}


//main store
class AppStore {
  final QueriesStore queriesStore;
  final UserLoginStore userLoginStore;
  final UserExistsModel userExistsStore;
}


//generic Action 
class ApiResponseAction<T> {
  ApiResState apiResState;
  T? updateStore; //only populated if response is a success

  ApiResponseAction({required this.apiResState, this.updateStore});
}


// generic reducer
abstract class AppStateReducer<T> {
  T apiResponse(state, ApiResponseAction<T> action) {
    switch (action.apiResState) {
        case ApiResState.isLoading:
        return state!.copyWith(apiResState: action.apiResState);

      case ApiResState.success:
        return action.updateStore == null
            ? state.copyWith(apiResState: action.apiResState)
            : action.updateStore;
    }
  }

  Reducer<T> get apiResponseReducer => combineReducers<T>([
        TypedReducer<T, ApiResponseAction<T>>(apiResponse),
      ]);
}

@Lyba97
Copy link
Author

Lyba97 commented Sep 3, 2022

@brianegan

@Lyba97
Copy link
Author

Lyba97 commented Sep 13, 2022

hi @brianegan. Did you get a chance to review this, please? It would be great if you could check it will help me a lot these days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant