mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-08-26 03:29:42 +00:00
debouncer Note
This commit is contained in:
@ -1,21 +1,29 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.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';
|
||||||
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/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/access_management/manage_bookable_spaces/domain/params/send_bookable_spaces_to_api_params.dart';
|
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/domain/params/send_bookable_spaces_to_api_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';
|
import 'package:syncrow_web/pages/space_management_v2/main_module/shared/models/paginated_data_model.dart';
|
||||||
import 'package:syncrow_web/services/api/api_exception.dart';
|
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||||
import 'package:syncrow_web/services/api/http_service.dart';
|
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) async {
|
NonBookableSpacesParams params) {
|
||||||
|
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,
|
||||||
@ -32,7 +40,7 @@ class RemoteNonBookableSpaces implements NonBookableSpacesService {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
return response;
|
completer.complete(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>?;
|
||||||
@ -41,11 +49,14 @@ class RemoteNonBookableSpaces implements NonBookableSpacesService {
|
|||||||
_defaultErrorMessage,
|
_defaultErrorMessage,
|
||||||
errorMessage,
|
errorMessage,
|
||||||
].join(': ');
|
].join(': ');
|
||||||
throw APIException(formattedErrorMessage);
|
completer.completeError(APIException(formattedErrorMessage));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
final formattedErrorMessage = [_defaultErrorMessage, '$e'].join(': ');
|
final formattedErrorMessage = [_defaultErrorMessage, '$e'].join(': ');
|
||||||
throw APIException(formattedErrorMessage);
|
completer.completeError(APIException(formattedErrorMessage));
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return completer.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -19,7 +19,6 @@ class SpacesStepDetailsWidget extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _SpacesStepDetailsWidgetState extends State<SpacesStepDetailsWidget> {
|
class _SpacesStepDetailsWidgetState extends State<SpacesStepDetailsWidget> {
|
||||||
Timer? _debounce;
|
|
||||||
ScrollController scrollController = ScrollController();
|
ScrollController scrollController = ScrollController();
|
||||||
int currentPage = 1;
|
int currentPage = 1;
|
||||||
String? currentSearchTerm;
|
String? currentSearchTerm;
|
||||||
@ -94,7 +93,7 @@ class _SpacesStepDetailsWidgetState extends State<SpacesStepDetailsWidget> {
|
|||||||
padding:
|
padding:
|
||||||
const EdgeInsets.symmetric(vertical: 15, horizontal: 20),
|
const EdgeInsets.symmetric(vertical: 15, horizontal: 20),
|
||||||
decoration: const BoxDecoration(
|
decoration: const BoxDecoration(
|
||||||
color: Color(0xFFF8F8F8),
|
color: ColorsManager.circleRolesBackground,
|
||||||
borderRadius: BorderRadius.vertical(
|
borderRadius: BorderRadius.vertical(
|
||||||
top: Radius.circular(20),
|
top: Radius.circular(20),
|
||||||
),
|
),
|
||||||
@ -102,8 +101,6 @@ class _SpacesStepDetailsWidgetState extends State<SpacesStepDetailsWidget> {
|
|||||||
child: SearchUnbookableSpacesWidget(
|
child: SearchUnbookableSpacesWidget(
|
||||||
title: 'Search',
|
title: 'Search',
|
||||||
onChanged: (p0) {
|
onChanged: (p0) {
|
||||||
if (_debounce?.isActive ?? false) _debounce!.cancel();
|
|
||||||
_debounce = Timer(const Duration(milliseconds: 500), () {
|
|
||||||
currentSearchTerm = p0;
|
currentSearchTerm = p0;
|
||||||
currentPage = 1;
|
currentPage = 1;
|
||||||
context.read<NonBookableSpacesBloc>().add(
|
context.read<NonBookableSpacesBloc>().add(
|
||||||
@ -114,7 +111,6 @@ class _SpacesStepDetailsWidgetState extends State<SpacesStepDetailsWidget> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
});
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
Reference in New Issue
Block a user