mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 22:57:21 +00:00
Refactor Space Update Logic: Introduced UpdateSpaceParam for better parameter handling in update operations. Enhanced SpaceDetailsDialogHelper to manage loading and error states during space updates. Updated RemoteUpdateSpaceService to construct dynamic URLs for space updates based on community UUID. Improved CommunitiesTreeFailureWidget UI with SelectableText and added spacing for better layout.
This commit is contained in:
@ -97,6 +97,7 @@ class CommunityStructureHeader extends StatelessWidget {
|
||||
SpaceDetailsDialogHelper.showEdit(
|
||||
context,
|
||||
spaceModel: selectedSpace!,
|
||||
communityUuid: selectedCommunity.uuid,
|
||||
);
|
||||
},
|
||||
selectedSpace: selectedSpace,
|
||||
|
@ -13,14 +13,14 @@ class CommunitiesTreeFailureWidget extends StatelessWidget {
|
||||
return Expanded(
|
||||
child: Center(
|
||||
child: Column(
|
||||
spacing: 16,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
SelectableText(
|
||||
errorMessage ?? 'Something went wrong',
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
FilledButton(
|
||||
onPressed: () => context.read<CommunitiesBloc>().add(
|
||||
LoadCommunities(
|
||||
LoadCommunitiesParam(
|
||||
|
@ -2,23 +2,38 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/space_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/data/services/remote_space_details_service.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/domain/models/space_details_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/presentation/bloc/space_details_bloc.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/presentation/widgets/space_details_dialog.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/update_space/data/services/remote_update_space_service.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/update_space/domain/params/update_space_param.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/update_space/presentation/bloc/update_space_bloc.dart';
|
||||
import 'package:syncrow_web/services/api/http_service.dart';
|
||||
|
||||
abstract final class SpaceDetailsDialogHelper {
|
||||
static void showCreate(BuildContext context) {
|
||||
showDialog<void>(
|
||||
context: context,
|
||||
builder: (_) => BlocProvider(
|
||||
builder: (_) => MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider(
|
||||
create: (context) => SpaceDetailsBloc(
|
||||
RemoteSpaceDetailsService(httpService: HTTPService()),
|
||||
),
|
||||
child: SpaceDetailsDialog(
|
||||
),
|
||||
BlocProvider(
|
||||
create: (context) => UpdateSpaceBloc(
|
||||
RemoteUpdateSpaceService(HTTPService()),
|
||||
),
|
||||
),
|
||||
],
|
||||
child: Builder(
|
||||
builder: (context) => SpaceDetailsDialog(
|
||||
context: context,
|
||||
title: const SelectableText('Create Space'),
|
||||
spaceModel: SpaceModel.empty(),
|
||||
onSave: print,
|
||||
onSave: (space) {},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
@ -27,20 +42,81 @@ abstract final class SpaceDetailsDialogHelper {
|
||||
static void showEdit(
|
||||
BuildContext context, {
|
||||
required SpaceModel spaceModel,
|
||||
required String communityUuid,
|
||||
}) {
|
||||
showDialog<void>(
|
||||
context: context,
|
||||
builder: (_) => BlocProvider(
|
||||
builder: (_) => MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider(
|
||||
create: (context) => SpaceDetailsBloc(
|
||||
RemoteSpaceDetailsService(httpService: HTTPService()),
|
||||
),
|
||||
),
|
||||
BlocProvider(
|
||||
create: (context) => UpdateSpaceBloc(
|
||||
RemoteUpdateSpaceService(HTTPService()),
|
||||
),
|
||||
),
|
||||
],
|
||||
child: Builder(
|
||||
builder: (context) => BlocListener<UpdateSpaceBloc, UpdateSpaceState>(
|
||||
listener: _updateListener,
|
||||
child: SpaceDetailsDialog(
|
||||
context: context,
|
||||
title: const SelectableText('Edit Space'),
|
||||
spaceModel: spaceModel,
|
||||
onSave: (space) {},
|
||||
onSave: (space) => context.read<UpdateSpaceBloc>().add(
|
||||
UpdateSpace(
|
||||
UpdateSpaceParam(
|
||||
communityUuid: communityUuid,
|
||||
space: space,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static void _updateListener(BuildContext context, UpdateSpaceState state) {
|
||||
return switch (state) {
|
||||
UpdateSpaceInitial() => null,
|
||||
UpdateSpaceLoading() => _onLoading(context),
|
||||
UpdateSpaceSuccess(:final space) => _onUpdateSuccess(context, space),
|
||||
UpdateSpaceFailure(:final errorMessage) => _onError(context, errorMessage),
|
||||
};
|
||||
}
|
||||
|
||||
static void _onUpdateSuccess(BuildContext context, SpaceDetailsModel space) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
static void _onLoading(BuildContext context) {
|
||||
showDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => const Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
|
||||
static void _onError(BuildContext context, String errorMessage) {
|
||||
Navigator.of(context).pop();
|
||||
showDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => AlertDialog(
|
||||
title: const Text('Error'),
|
||||
content: Text(errorMessage),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: Navigator.of(context).pop,
|
||||
child: const Text('OK'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:syncrow_web/pages/common/bloc/project_manager.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/domain/models/space_details_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/update_space/domain/params/update_space_param.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/update_space/domain/services/update_space_service.dart';
|
||||
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||
import 'package:syncrow_web/services/api/http_service.dart';
|
||||
@ -12,14 +14,19 @@ class RemoteUpdateSpaceService implements UpdateSpaceService {
|
||||
static const _defaultErrorMessage = 'Failed to update space';
|
||||
|
||||
@override
|
||||
Future<SpaceDetailsModel> updateSpace(SpaceDetailsModel space) async {
|
||||
Future<SpaceDetailsModel> updateSpace(UpdateSpaceParam param) async {
|
||||
try {
|
||||
final path = await _makeUrl(param);
|
||||
final response = await _httpService.put(
|
||||
path: 'endpoint',
|
||||
body: space.toJson(),
|
||||
expectedResponseModel: (data) => SpaceDetailsModel.fromJson(
|
||||
data as Map<String, dynamic>,
|
||||
),
|
||||
path: path,
|
||||
body: param.space.toJson(),
|
||||
expectedResponseModel: (data) {
|
||||
final response = data as Map<String, dynamic>;
|
||||
final space = SpaceDetailsModel.fromJson(
|
||||
response['data'] as Map<String, dynamic>,
|
||||
);
|
||||
return space;
|
||||
},
|
||||
);
|
||||
|
||||
return response;
|
||||
@ -37,4 +44,13 @@ class RemoteUpdateSpaceService implements UpdateSpaceService {
|
||||
throw APIException(formattedErrorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _makeUrl(UpdateSpaceParam param) async {
|
||||
final projectUuid = await ProjectManager.getProjectUUID();
|
||||
if (projectUuid == null || projectUuid.isEmpty) {
|
||||
throw APIException('Project UUID is not set');
|
||||
}
|
||||
|
||||
return '/projects/$projectUuid/communities/${param.communityUuid}/spaces/${param.space.uuid}';
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/domain/models/space_details_model.dart';
|
||||
|
||||
class UpdateSpaceParam {
|
||||
UpdateSpaceParam({
|
||||
required this.space,
|
||||
required this.communityUuid,
|
||||
});
|
||||
|
||||
final SpaceDetailsModel space;
|
||||
final String communityUuid;
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/domain/models/space_details_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/update_space/domain/params/update_space_param.dart';
|
||||
|
||||
abstract class UpdateSpaceService {
|
||||
Future<SpaceDetailsModel> updateSpace(SpaceDetailsModel space);
|
||||
abstract interface class UpdateSpaceService {
|
||||
Future<SpaceDetailsModel> updateSpace(UpdateSpaceParam param);
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/domain/models/space_details_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/update_space/domain/params/update_space_param.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/update_space/domain/services/update_space_service.dart';
|
||||
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||
|
||||
@ -20,7 +21,7 @@ class UpdateSpaceBloc extends Bloc<UpdateSpaceEvent, UpdateSpaceState> {
|
||||
) async {
|
||||
emit(UpdateSpaceLoading());
|
||||
try {
|
||||
final updatedSpace = await _updateSpaceService.updateSpace(event.space);
|
||||
final updatedSpace = await _updateSpaceService.updateSpace(event.param);
|
||||
emit(UpdateSpaceSuccess(updatedSpace));
|
||||
} on APIException catch (e) {
|
||||
emit(UpdateSpaceFailure(e.message));
|
||||
|
@ -8,10 +8,10 @@ sealed class UpdateSpaceEvent extends Equatable {
|
||||
}
|
||||
|
||||
final class UpdateSpace extends UpdateSpaceEvent {
|
||||
const UpdateSpace(this.space);
|
||||
const UpdateSpace(this.param);
|
||||
|
||||
final SpaceDetailsModel space;
|
||||
final UpdateSpaceParam param;
|
||||
|
||||
@override
|
||||
List<Object> get props => [space];
|
||||
List<Object> get props => [param];
|
||||
}
|
||||
|
@ -21,10 +21,10 @@ final class UpdateSpaceSuccess extends UpdateSpaceState {
|
||||
}
|
||||
|
||||
final class UpdateSpaceFailure extends UpdateSpaceState {
|
||||
final String message;
|
||||
final String errorMessage;
|
||||
|
||||
const UpdateSpaceFailure(this.message);
|
||||
const UpdateSpaceFailure(this.errorMessage);
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
List<Object> get props => [errorMessage];
|
||||
}
|
||||
|
Reference in New Issue
Block a user