import 'package:redux/redux.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import '../model/UserInfo.dart';

/**
 * 用户相关Redux
 * Created by guoshuyu
 * Date: 2018-07-16
 */
/// redux 的 combineReducers, 通过 TypedReducer 将 UpdateUserAction 与 reducers 关联起来
final userReducer = combineReducers<UserInfo>([
  TypedReducer<UserInfo, UpdateUserAction>(_updateLoaded),
]);

/// 如果有 UpdateUserAction 发起一个请求时
/// 就会调用到 _updateLoaded
/// _updateLoaded 这里接受一个新的userInfo，并返回
UserInfo _updateLoaded(UserInfo userInfo, action) {
  final JPush jpush = JPush();
  if (action.userInfo != null) {
    if (userInfo == null || (userInfo.id != action.userInfo.id)) {
      jpush.setAlias(action.userInfo.id.toString()).then((map) {});
    }
  } else {
    jpush.deleteAlias();
  }
  userInfo = action.userInfo;
  return userInfo;
}

///定一个 UpdateUserAction ，用于发起 userInfo 的的改变
///类名随你喜欢定义，只要通过上面TypedReducer绑定就好
class UpdateUserAction {
  final UserInfo userInfo;
  UpdateUserAction(this.userInfo);
}

class LogoutAction {
  LogoutAction();
}
