mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
Created update community bloc, services, and param.
This commit is contained in:
@ -0,0 +1,39 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/update_community/domain/params/update_community_param.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/update_community/domain/services/update_community_service.dart';
|
||||
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||
import 'package:syncrow_web/services/api/http_service.dart';
|
||||
|
||||
class RemoteUpdateCommunityService implements UpdateCommunityService {
|
||||
const RemoteUpdateCommunityService(this._httpService);
|
||||
|
||||
final HTTPService _httpService;
|
||||
|
||||
static const _defaultErrorMessage = 'Failed to update community';
|
||||
|
||||
@override
|
||||
Future<CommunityModel> updateCommunity(UpdateCommunityParam param) async {
|
||||
try {
|
||||
final response = await _httpService.put(
|
||||
path: 'endpoint',
|
||||
expectedResponseModel: (data) => CommunityModel.fromJson(
|
||||
data as Map<String, dynamic>,
|
||||
),
|
||||
);
|
||||
return response;
|
||||
} on DioException catch (e) {
|
||||
final message = e.response?.data as Map<String, dynamic>?;
|
||||
final error = message?['error'] as Map<String, dynamic>?;
|
||||
final errorMessage = error?['error'] as String? ?? '';
|
||||
final formattedErrorMessage = [
|
||||
_defaultErrorMessage,
|
||||
errorMessage,
|
||||
].join(': ');
|
||||
throw APIException(formattedErrorMessage);
|
||||
} catch (e) {
|
||||
final formattedErrorMessage = [_defaultErrorMessage, '$e'].join(': ');
|
||||
throw APIException(formattedErrorMessage);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class UpdateCommunityParam extends Equatable {
|
||||
const UpdateCommunityParam({required this.name});
|
||||
|
||||
final String name;
|
||||
|
||||
@override
|
||||
List<Object> get props => [name];
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/update_community/domain/params/update_community_param.dart';
|
||||
|
||||
abstract class UpdateCommunityService {
|
||||
Future<CommunityModel> updateCommunity(UpdateCommunityParam param);
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/update_community/domain/params/update_community_param.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/update_community/domain/services/update_community_service.dart';
|
||||
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||
|
||||
part 'update_community_event.dart';
|
||||
part 'update_community_state.dart';
|
||||
|
||||
class UpdateCommunityBloc extends Bloc<UpdateCommunityEvent, UpdateCommunityState> {
|
||||
final UpdateCommunityService _updateCommunityService;
|
||||
|
||||
UpdateCommunityBloc(
|
||||
this._updateCommunityService,
|
||||
) : super(UpdateCommunityInitial()) {
|
||||
on<UpdateCommunity>(_onUpdateCommunity);
|
||||
}
|
||||
|
||||
Future<void> _onUpdateCommunity(
|
||||
UpdateCommunity event,
|
||||
Emitter<UpdateCommunityState> emit,
|
||||
) async {
|
||||
emit(UpdateCommunityLoading());
|
||||
try {
|
||||
final updatedCommunity = await _updateCommunityService.updateCommunity(
|
||||
event.param,
|
||||
);
|
||||
emit(UpdateCommunitySuccess(updatedCommunity));
|
||||
} on APIException catch (e) {
|
||||
emit(UpdateCommunityFailure(e.message));
|
||||
} catch (e) {
|
||||
emit(UpdateCommunityFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
part of 'update_community_bloc.dart';
|
||||
|
||||
sealed class UpdateCommunityEvent extends Equatable {
|
||||
const UpdateCommunityEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
final class UpdateCommunity extends UpdateCommunityEvent {
|
||||
const UpdateCommunity(this.param);
|
||||
|
||||
final UpdateCommunityParam param;
|
||||
|
||||
@override
|
||||
List<Object> get props => [param];
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
part of 'update_community_bloc.dart';
|
||||
|
||||
sealed class UpdateCommunityState extends Equatable {
|
||||
const UpdateCommunityState();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
final class UpdateCommunityInitial extends UpdateCommunityState {}
|
||||
|
||||
final class UpdateCommunityLoading extends UpdateCommunityState {}
|
||||
|
||||
final class UpdateCommunitySuccess extends UpdateCommunityState {
|
||||
final CommunityModel community;
|
||||
|
||||
const UpdateCommunitySuccess(this.community);
|
||||
|
||||
@override
|
||||
List<Object> get props => [community];
|
||||
}
|
||||
|
||||
final class UpdateCommunityFailure extends UpdateCommunityState {
|
||||
final String message;
|
||||
|
||||
const UpdateCommunityFailure(this.message);
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
}
|
Reference in New Issue
Block a user