mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-08-25 15:39:41 +00:00
debouncer Note
This commit is contained in:
@ -1,51 +1,62 @@
|
|||||||
|
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) {
|
||||||
try {
|
final completer = Completer<PaginatedDataModel<BookableSpacemodel>>();
|
||||||
final response = await _httpService.get(
|
|
||||||
path: ApiEndpoints.bookableSpaces,
|
_debounce?.cancel();
|
||||||
queryParameters: {
|
_debounce = Timer(const Duration(milliseconds: 500), () async {
|
||||||
'configured': false,
|
try {
|
||||||
'page': params.currentPage,
|
final response = await _httpService.get(
|
||||||
'search': params.searchedWords,
|
path: ApiEndpoints.bookableSpaces,
|
||||||
},
|
queryParameters: {
|
||||||
expectedResponseModel: (json) {
|
'configured': false,
|
||||||
final result = json as Map<String, dynamic>;
|
'page': params.currentPage,
|
||||||
return PaginatedDataModel.fromJson(
|
'search': params.searchedWords,
|
||||||
result,
|
},
|
||||||
BookableSpacemodel.fromJsonList,
|
expectedResponseModel: (json) {
|
||||||
);
|
final result = json as Map<String, dynamic>;
|
||||||
},
|
return PaginatedDataModel.fromJson(
|
||||||
);
|
result,
|
||||||
return response;
|
BookableSpacemodel.fromJsonList,
|
||||||
} 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? ?? '';
|
completer.complete(response);
|
||||||
final formattedErrorMessage = [
|
} on DioException catch (e) {
|
||||||
_defaultErrorMessage,
|
final message = e.response?.data as Map<String, dynamic>?;
|
||||||
errorMessage,
|
final error = message?['error'] as Map<String, dynamic>?;
|
||||||
].join(': ');
|
final errorMessage = error?['error'] as String? ?? '';
|
||||||
throw APIException(formattedErrorMessage);
|
final formattedErrorMessage = [
|
||||||
} catch (e) {
|
_defaultErrorMessage,
|
||||||
final formattedErrorMessage = [_defaultErrorMessage, '$e'].join(': ');
|
errorMessage,
|
||||||
throw APIException(formattedErrorMessage);
|
].join(': ');
|
||||||
}
|
completer.completeError(APIException(formattedErrorMessage));
|
||||||
|
} catch (e) {
|
||||||
|
final formattedErrorMessage = [_defaultErrorMessage, '$e'].join(': ');
|
||||||
|
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,19 +101,16 @@ class _SpacesStepDetailsWidgetState extends State<SpacesStepDetailsWidget> {
|
|||||||
child: SearchUnbookableSpacesWidget(
|
child: SearchUnbookableSpacesWidget(
|
||||||
title: 'Search',
|
title: 'Search',
|
||||||
onChanged: (p0) {
|
onChanged: (p0) {
|
||||||
if (_debounce?.isActive ?? false) _debounce!.cancel();
|
currentSearchTerm = p0;
|
||||||
_debounce = Timer(const Duration(milliseconds: 500), () {
|
currentPage = 1;
|
||||||
currentSearchTerm = p0;
|
context.read<NonBookableSpacesBloc>().add(
|
||||||
currentPage = 1;
|
LoadUnBookableSpacesEvent(
|
||||||
context.read<NonBookableSpacesBloc>().add(
|
nonBookableSpacesParams: NonBookableSpacesParams(
|
||||||
LoadUnBookableSpacesEvent(
|
currentPage: currentPage,
|
||||||
nonBookableSpacesParams: NonBookableSpacesParams(
|
searchedWords: currentSearchTerm,
|
||||||
currentPage: currentPage,
|
|
||||||
searchedWords: currentSearchTerm,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
);
|
),
|
||||||
});
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
Reference in New Issue
Block a user