mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-08-25 14:19:40 +00:00
add decorator layer
This commit is contained in:
@ -0,0 +1,32 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/domain/models/bookable_space_model.dart';
|
||||||
|
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/domain/params/non_bookable_spaces_params.dart';
|
||||||
|
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/domain/service/non_bookable_spaces_service.dart';
|
||||||
|
import 'package:syncrow_web/pages/space_management_v2/main_module/shared/models/paginated_data_model.dart';
|
||||||
|
|
||||||
|
class NonBookableSpacesDebouncerDecoratorService
|
||||||
|
implements NonBookableSpacesService {
|
||||||
|
final NonBookableSpacesService _delegate;
|
||||||
|
Timer? _debounce;
|
||||||
|
|
||||||
|
NonBookableSpacesDebouncerDecoratorService(this._delegate);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<PaginatedDataModel<BookableSpacemodel>> load(
|
||||||
|
NonBookableSpacesParams params) {
|
||||||
|
final completer = Completer<PaginatedDataModel<BookableSpacemodel>>();
|
||||||
|
|
||||||
|
_debounce?.cancel();
|
||||||
|
_debounce = Timer(const Duration(milliseconds: 500), () async {
|
||||||
|
try {
|
||||||
|
final result = await _delegate.load(params);
|
||||||
|
completer.complete(result);
|
||||||
|
} catch (e) {
|
||||||
|
completer.completeError(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return completer.future;
|
||||||
|
}
|
||||||
|
}
|
@ -10,19 +10,15 @@ import 'package:syncrow_web/services/api/http_service.dart';
|
|||||||
import 'package:syncrow_web/utils/constants/api_const.dart';
|
import 'package:syncrow_web/utils/constants/api_const.dart';
|
||||||
|
|
||||||
class RemoteNonBookableSpaces implements NonBookableSpacesService {
|
class RemoteNonBookableSpaces implements NonBookableSpacesService {
|
||||||
Timer? _debounce;
|
|
||||||
|
|
||||||
final HTTPService _httpService;
|
final HTTPService _httpService;
|
||||||
|
|
||||||
RemoteNonBookableSpaces(this._httpService);
|
RemoteNonBookableSpaces(this._httpService);
|
||||||
|
|
||||||
static const _defaultErrorMessage = 'Failed to load Spaces';
|
static const _defaultErrorMessage = 'Failed to load Spaces';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<PaginatedDataModel<BookableSpacemodel>> load(
|
Future<PaginatedDataModel<BookableSpacemodel>> load(
|
||||||
NonBookableSpacesParams params) {
|
NonBookableSpacesParams params) async {
|
||||||
final completer = Completer<PaginatedDataModel<BookableSpacemodel>>();
|
|
||||||
|
|
||||||
_debounce?.cancel();
|
|
||||||
_debounce = Timer(const Duration(milliseconds: 500), () async {
|
|
||||||
try {
|
try {
|
||||||
final response = await _httpService.get(
|
final response = await _httpService.get(
|
||||||
path: ApiEndpoints.bookableSpaces,
|
path: ApiEndpoints.bookableSpaces,
|
||||||
@ -39,7 +35,7 @@ class RemoteNonBookableSpaces implements NonBookableSpacesService {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
completer.complete(response);
|
return response;
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
final message = e.response?.data as Map<String, dynamic>?;
|
final message = e.response?.data as Map<String, dynamic>?;
|
||||||
final error = message?['error'] as Map<String, dynamic>?;
|
final error = message?['error'] as Map<String, dynamic>?;
|
||||||
@ -48,13 +44,10 @@ class RemoteNonBookableSpaces implements NonBookableSpacesService {
|
|||||||
_defaultErrorMessage,
|
_defaultErrorMessage,
|
||||||
errorMessage,
|
errorMessage,
|
||||||
].join(': ');
|
].join(': ');
|
||||||
completer.completeError(APIException(formattedErrorMessage));
|
throw APIException(formattedErrorMessage);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
final formattedErrorMessage = [_defaultErrorMessage, '$e'].join(': ');
|
final formattedErrorMessage = [_defaultErrorMessage, '$e'].join(': ');
|
||||||
completer.completeError(APIException(formattedErrorMessage));
|
throw APIException(formattedErrorMessage);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
return completer.future;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/data/non_bookable_spaces_decorator.dart';
|
||||||
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/data/remote_non_bookable_spaces.dart';
|
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/data/remote_non_bookable_spaces.dart';
|
||||||
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/data/remote_send_bookable_spaces.dart';
|
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/data/remote_send_bookable_spaces.dart';
|
||||||
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/domain/models/bookable_space_model.dart';
|
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/domain/models/bookable_space_model.dart';
|
||||||
@ -46,7 +47,9 @@ class _SetupBookableSpacesDialogState extends State<SetupBookableSpacesDialog> {
|
|||||||
),
|
),
|
||||||
BlocProvider<NonBookableSpacesBloc>(
|
BlocProvider<NonBookableSpacesBloc>(
|
||||||
create: (context) => NonBookableSpacesBloc(
|
create: (context) => NonBookableSpacesBloc(
|
||||||
|
NonBookableSpacesDebouncerDecoratorService(
|
||||||
RemoteNonBookableSpaces(HTTPService()),
|
RemoteNonBookableSpaces(HTTPService()),
|
||||||
|
),
|
||||||
)..add(
|
)..add(
|
||||||
LoadUnBookableSpacesEvent(
|
LoadUnBookableSpacesEvent(
|
||||||
nonBookableSpacesParams:
|
nonBookableSpacesParams:
|
||||||
|
Reference in New Issue
Block a user