MembershipBloc constructor

MembershipBloc()

Implementation

MembershipBloc() : super(UnitializedMembership()) {
  on<FetchMembershipEvent>((event, emit) async {
    var accessModel = await accessRepository(appId: event.app.documentID)!
        .get(event.memberId);
    //??   AccessModel(documentID: event.memberId, appId: event.app.documentID, privilegeLevel: PrivilegeLevel.NoPrivilege, points: 0, blocked: false, ) ;
    var member =
        await memberPublicInfoRepository(appId: event.app.documentID)!
            .get(event.memberId);
    emit(MembershipLoaded(accessModel, event.app.documentID, member));
  });

  on<BlockMember>((event, emit) async {
    if (state is MembershipLoaded) {
      MembershipLoaded theState = state as MembershipLoaded;

      emit(await _update(
          theState.appId,
          theState.accessModel,
          AccessModel(
            appId: theState.appId,
            documentID: theState.member!.documentID,
            privilegeLevel: PrivilegeLevel.noPrivilege,
            privilegeLevelBeforeBlocked: theState.accessModel == null
                ? PrivilegeLevelBeforeBlocked.noPrivilege
                : plToPlbb(theState.accessModel!.privilegeLevel!),
            blocked: true,
          ),
          theState.member));
    }
  });

  on<UnblockMember>((event, emit) async {
    if (state is MembershipLoaded) {
      MembershipLoaded theState = state as MembershipLoaded;
      emit(await _update(
          theState.appId,
          theState.accessModel,
          AccessModel(
            appId: theState.appId,
            documentID: theState.member!.documentID,
            privilegeLevel:
                plbbToPl(theState.accessModel!.privilegeLevelBeforeBlocked!),
            privilegeLevelBeforeBlocked: null,
            blocked: false,
          ),
          theState.member));
    }
  });

  on<PromoteMember>((event, emit) async {
    if (state is MembershipLoaded) {
      MembershipLoaded theState = state as MembershipLoaded;

      if ((theState.accessModel == null) ||
          (theState.accessModel!.privilegeLevel!.index <=
              PrivilegeLevel.ownerPrivilege.index)) {
        emit(await _update(
            theState.appId,
            theState.accessModel,
            AccessModel(
              appId: theState.appId,
              documentID: theState.member!.documentID,
              privilegeLevel: theState.accessModel == null
                  ? PrivilegeLevel.level1Privilege
                  : intToPL(theState.accessModel!.privilegeLevel!.index + 1),
            ),
            theState.member));
      }
    }
  });

  on<DemoteMember>((event, emit) async {
    if (state is MembershipLoaded) {
      MembershipLoaded theState = state as MembershipLoaded;

      if (theState.accessModel!.privilegeLevel!.index >
          PrivilegeLevel.noPrivilege.index) {
        emit(await _update(
            theState.appId,
            theState.accessModel,
            AccessModel(
              appId: theState.appId,
              documentID: theState.member!.documentID,
              privilegeLevel:
                  intToPL(theState.accessModel!.privilegeLevel!.index - 1),
            ),
            theState.member));
      }
    }
  });
}