import 'package:bloc/bloc.dart';
import 'activity_event.dart';
import 'activity_state.dart';
import '../../services/activity_api.dart';

class ActivityBloc extends Bloc<ActivityEvent, ActivityState> {
  final ActivityApi activityApi;

  ActivityBloc(this.activityApi) : super(ActivityInitial()) {
    on<LoadOrgActivities>(_onLoadOrgActivities);
  }

  void _onLoadOrgActivities(
    LoadOrgActivities event,
    Emitter<ActivityState> emit,
  ) async {
    emit(ActivityLoading());
    try {
      final activities = await activityApi.getOrgActivities(event.orgId);
      emit(ActivityLoaded(activities: activities));
    } catch (e) {
      emit(ActivityError(e.toString()));
    }
  }
}
