diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/blocs/setup_bookable_spaces_bloc/setup_bookable_spaces_bloc.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/blocs/setup_bookable_spaces_bloc/setup_bookable_spaces_bloc.dart index 2212da77..7ba4eb0a 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/blocs/setup_bookable_spaces_bloc/setup_bookable_spaces_bloc.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/blocs/setup_bookable_spaces_bloc/setup_bookable_spaces_bloc.dart @@ -10,51 +10,123 @@ part 'setup_bookable_spaces_state.dart'; class SetupBookableSpacesBloc extends Bloc { NonBookableSpacesService nonBookableSpacesService; - List selectedBookableSpaces = []; + SetupBookableSpacesBloc(this.nonBookableSpacesService) - : super(SetupBookableSpacesInitial()) { + : super(const SetupBookableSpacesInitial(bookableSpaces: [])) { on(_onAddToBookableSpaceEvent); on(_onRemoveFromBookableSpaceEvent); + on(_onAddBookableDays); + on(_onChangeStartTimeEvent); + on(_onChangeEndTimeEvent); + on(_onChangeCostEvent); on(_onCheckConfigurValidityEvent); on(_onEditModeSelected); } - TimeOfDay? get endTime => - selectedBookableSpaces.first.spaceConfig!.bookingEndTime; - - TimeOfDay? get startTime => - selectedBookableSpaces.first.spaceConfig!.bookingStartTime; + List get currentBookableSpaces { + return switch (state) { + AddNonBookableSpaceIntoBookableState(:final bookableSpaces) => + bookableSpaces, + RemoveBookableSpaceIntoNonBookableState(:final bookableSpaces) => + bookableSpaces, + SetupBookableSpacesInitial(:final bookableSpaces) => bookableSpaces, + InProgressState(:final bookableSpaces) => bookableSpaces, + ValidSaveButtonState(:final bookableSpaces) => bookableSpaces, + UnValidSaveButtonState(:final bookableSpaces) => bookableSpaces, + }; + } void _onAddToBookableSpaceEvent( AddToBookableSpaceEvent event, Emitter emit, ) { - emit(InProgressState()); - selectedBookableSpaces.add(event.nonBookableSpace); - emit(AddNonBookableSpaceIntoBookableState( - bookableSpaces: selectedBookableSpaces)); + emit(InProgressState(bookableSpaces: state.bookableSpaces)); + final updatedSpaces = List.from(state.bookableSpaces); + + updatedSpaces.add(event.nonBookableSpace); + + emit(AddNonBookableSpaceIntoBookableState(bookableSpaces: updatedSpaces)); } void _onRemoveFromBookableSpaceEvent(RemoveFromBookableSpaceEvent event, Emitter emit) { - emit(InProgressState()); - selectedBookableSpaces.remove(event.bookableSpace); + emit(InProgressState(bookableSpaces: state.bookableSpaces)); + state.bookableSpaces.remove(event.bookableSpace); emit(RemoveBookableSpaceIntoNonBookableState( - bookableSpaces: selectedBookableSpaces)); + bookableSpaces: state.bookableSpaces)); + } + + void _onAddBookableDays( + AddBookableDaysEvent event, Emitter emit) { + final updatedSpaces = state.bookableSpaces.map((space) { + final updatedConfig = space.spaceConfig?.copyWith( + bookableDays: event.bookableDays, + ); + + return space.copyWith(spaceConfig: updatedConfig); + }).toList(); + + emit(SetupBookableSpacesInitial(bookableSpaces: updatedSpaces)); + } + + void _onChangeStartTimeEvent( + ChangeStartTimeEvent event, Emitter emit) { + final updatedSpaces = state.bookableSpaces.map((space) { + final updatedConfig = space.spaceConfig?.copyWith( + bookingStartTime: event.startTime, + ); + + return space.copyWith(spaceConfig: updatedConfig); + }).toList(); + + emit(SetupBookableSpacesInitial(bookableSpaces: updatedSpaces)); + } + + void _onChangeEndTimeEvent( + ChangeEndTimeEvent event, Emitter emit) { + final updatedSpaces = state.bookableSpaces.map((space) { + final updatedConfig = space.spaceConfig?.copyWith( + bookingEndTime: event.endTime, + ); + + return space.copyWith(spaceConfig: updatedConfig); + }).toList(); + + emit(SetupBookableSpacesInitial(bookableSpaces: updatedSpaces)); + } + + void _onChangeCostEvent( + ChangeCostEvent event, Emitter emit) { + final updatedSpaces = state.bookableSpaces.map((space) { + final updatedConfig = space.spaceConfig?.copyWith( + cost: event.cost, + ); + + return space.copyWith(spaceConfig: updatedConfig); + }).toList(); + + emit(SetupBookableSpacesInitial(bookableSpaces: updatedSpaces)); } void _onCheckConfigurValidityEvent(CheckConfigurValidityEvent event, Emitter emit) { - if (selectedBookableSpaces.first.spaceConfig!.isValid) { - emit(ValidSaveButtonState()); + if (state.bookableSpaces.first.spaceConfig!.isValid) { + emit(ValidSaveButtonState( + bookableSpaces: state.bookableSpaces, + )); } else { - emit(UnValidSaveButtonState()); + emit(UnValidSaveButtonState( + bookableSpaces: state.bookableSpaces, + )); } } void _onEditModeSelected( - EditModeSelected event, Emitter emit) { - selectedBookableSpaces.clear(); - selectedBookableSpaces.add(event.editingBookableSpace); + EditModeSelected event, + Emitter emit, + ) { + final updatedList = [event.editingBookableSpace]; + + emit(SetupBookableSpacesInitial(bookableSpaces: updatedList)); } } diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/blocs/setup_bookable_spaces_bloc/setup_bookable_spaces_event.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/blocs/setup_bookable_spaces_bloc/setup_bookable_spaces_event.dart index 68deee73..8bc66f95 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/blocs/setup_bookable_spaces_bloc/setup_bookable_spaces_event.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/blocs/setup_bookable_spaces_bloc/setup_bookable_spaces_event.dart @@ -21,6 +21,34 @@ class RemoveFromBookableSpaceEvent extends SetupBookableSpacesEvent { }); } +class AddBookableDaysEvent extends SetupBookableSpacesEvent { + final List bookableDays; + const AddBookableDaysEvent({ + required this.bookableDays, + }); +} + +class ChangeCostEvent extends SetupBookableSpacesEvent { + final int cost; + const ChangeCostEvent({ + required this.cost, + }); +} + +class ChangeStartTimeEvent extends SetupBookableSpacesEvent { + final TimeOfDay startTime; + const ChangeStartTimeEvent({ + required this.startTime, + }); +} + +class ChangeEndTimeEvent extends SetupBookableSpacesEvent { + final TimeOfDay endTime; + const ChangeEndTimeEvent({ + required this.endTime, + }); +} + class CheckConfigurValidityEvent extends SetupBookableSpacesEvent {} class EditModeSelected extends SetupBookableSpacesEvent { diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/blocs/setup_bookable_spaces_bloc/setup_bookable_spaces_state.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/blocs/setup_bookable_spaces_bloc/setup_bookable_spaces_state.dart index b6dbad70..5b04f075 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/blocs/setup_bookable_spaces_bloc/setup_bookable_spaces_state.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/blocs/setup_bookable_spaces_bloc/setup_bookable_spaces_state.dart @@ -1,32 +1,37 @@ part of 'setup_bookable_spaces_bloc.dart'; sealed class SetupBookableSpacesState extends Equatable { - const SetupBookableSpacesState(); - + final List bookableSpaces; + const SetupBookableSpacesState({required this.bookableSpaces}); + TimeOfDay? get startTime => + bookableSpaces.first.spaceConfig!.bookingStartTime; + TimeOfDay? get endTime => bookableSpaces.first.spaceConfig!.bookingEndTime; @override List get props => []; } -final class SetupBookableSpacesInitial extends SetupBookableSpacesState {} +final class SetupBookableSpacesInitial extends SetupBookableSpacesState { + const SetupBookableSpacesInitial({required super.bookableSpaces}); +} class AddNonBookableSpaceIntoBookableState extends SetupBookableSpacesState { - final List bookableSpaces; - const AddNonBookableSpaceIntoBookableState({ - required this.bookableSpaces, - }); + const AddNonBookableSpaceIntoBookableState({required super.bookableSpaces}); } -class InProgressState extends SetupBookableSpacesState {} +class InProgressState extends SetupBookableSpacesState { + const InProgressState({required super.bookableSpaces}); +} class RemoveBookableSpaceIntoNonBookableState extends SetupBookableSpacesState { - final List bookableSpaces; const RemoveBookableSpaceIntoNonBookableState({ - required this.bookableSpaces, + required super.bookableSpaces, }); } -class ValidSaveButtonState extends SetupBookableSpacesState {} - -class UnValidSaveButtonState extends SetupBookableSpacesState {} - +class ValidSaveButtonState extends SetupBookableSpacesState { + const ValidSaveButtonState({required super.bookableSpaces}); +} +class UnValidSaveButtonState extends SetupBookableSpacesState { + const UnValidSaveButtonState({required super.bookableSpaces}); +} diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/screens/setup_bookable_spaces_dialog.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/screens/setup_bookable_spaces_dialog.dart index a0e8da84..07be929f 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/screens/setup_bookable_spaces_dialog.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/screens/setup_bookable_spaces_dialog.dart @@ -63,9 +63,10 @@ class _SetupBookableSpacesDialogState extends State { RemoteNonBookableSpaces(HTTPService())) : (context) => SetupBookableSpacesBloc( RemoteNonBookableSpaces(HTTPService())) - ..add(EditModeSelected( - editingBookableSpace: widget.editingBookableSpace!, - )), + ..add( + EditModeSelected( + editingBookableSpace: widget.editingBookableSpace!), + ), ), BlocProvider( create: (context) => SendBookableSpacesBloc( @@ -115,16 +116,14 @@ class _SetupBookableSpacesDialogState extends State { ), Builder(builder: (context) { final stepsState = context.watch().state; - final setupBookableSpacesBloc = - context.watch(); - final selectedSpaces = - setupBookableSpacesBloc.selectedBookableSpaces; + final bookableSpaces = + context.watch().state.bookableSpaces; return stepsState is StepOneState - ? NextFirstStepButton(selectedSpaces: selectedSpaces) + ? const NextFirstStepButton() : SaveSecondStepButton( - selectedSpaces: selectedSpaces, pointsController: pointsController, isEditingMode: widget.editingBookableSpace != null, + bookableSpaces: bookableSpaces, ); }), ], diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/booking_period_widget.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/booking_period_widget.dart index f81e1c8c..c434fc3d 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/booking_period_widget.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/booking_period_widget.dart @@ -17,6 +17,8 @@ class BookingPeriodWidget extends StatelessWidget { @override Widget build(BuildContext context) { + final state = context.watch().state; + return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ @@ -32,139 +34,108 @@ class BookingPeriodWidget extends StatelessWidget { const Text('Booking Period'), ], ), - const SizedBox( - height: 5, - ), + const SizedBox(height: 5), Container( width: 230, decoration: BoxDecoration( - borderRadius: BorderRadius.circular(8), - color: ColorsManager.circleRolesBackground, - boxShadow: [ - BoxShadow( - offset: Offset.zero, - blurRadius: 4, - spreadRadius: 0, - color: ColorsManager.timePickerColor.withValues(alpha: 0.15), - ) - ]), + borderRadius: BorderRadius.circular(8), + color: ColorsManager.circleRolesBackground, + boxShadow: [ + BoxShadow( + offset: Offset.zero, + blurRadius: 4, + spreadRadius: 0, + color: ColorsManager.timePickerColor.withValues(alpha: 0.15), + ) + ], + ), child: Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ TimePickerWidget( - title: editingBookableSpace == null - ? 'Start Time' - : editingBookableSpace!.spaceConfig!.bookingStartTime! - .format(context), - onTimePicked: (timePicked) { - if (timePicked == null) { + title: editingBookableSpace?.spaceConfig?.bookingStartTime + ?.format(context) ?? + 'Start Time', + onTimePicked: (pickedStartTime) { + if (pickedStartTime == null) return; + + if (state.endTime != null && + isEndTimeAfterStartTime( + pickedStartTime, state.endTime!)) { + _showInvalidSnackBar( + context, "You can't choose Start Time after End Time"); return; } - final setupBookableSpacesBloc = - context.read(); - if (setupBookableSpacesBloc.endTime != null && - isEndTimeAfterStartTime( - timePicked, setupBookableSpacesBloc.endTime!)) { - ScaffoldMessenger.of(context).clearSnackBars(); - ScaffoldMessenger.of(context).showSnackBar(const SnackBar( - content: - Text("You can't choose start Time Before End time"), - duration: Duration(seconds: 2), - backgroundColor: ColorsManager.red, - )); - throw Exception(); - } else { - for (int i = 0; - i < - setupBookableSpacesBloc - .selectedBookableSpaces.length; - i++) { - final space = - setupBookableSpacesBloc.selectedBookableSpaces[i]; - final updatedConfig = space.spaceConfig - ?.copyWith(bookingStartTime: timePicked); - final updatedSpace = - space.copyWith(spaceConfig: updatedConfig); - - setupBookableSpacesBloc.selectedBookableSpaces[i] = - updatedSpace; - } - } + context.read().add( + ChangeStartTimeEvent(startTime: pickedStartTime), + ); + context.read().add( + CheckConfigurValidityEvent(), + ); }, ), - const SizedBox( - width: 10, - ), + const SizedBox(width: 10), const Icon( Icons.arrow_right_alt, color: ColorsManager.grayColor, size: 13, ), TimePickerWidget( - title: editingBookableSpace == null - ? 'End Time' - : editingBookableSpace!.spaceConfig!.bookingEndTime! - .format(context), - onTimePicked: (timePicked) { - if (timePicked == null) { + title: editingBookableSpace?.spaceConfig?.bookingEndTime + ?.format(context) ?? + 'End Time', + onTimePicked: (pickedEndTime) { + if (pickedEndTime == null) return; + + if (state.startTime != null && + isEndTimeAfterStartTime( + state.startTime!, pickedEndTime)) { + _showInvalidSnackBar( + context, "You can't choose End Time before Start Time"); return; } - final setupBookableSpacesBloc = - context.read(); - if (setupBookableSpacesBloc.startTime != null && - isEndTimeAfterStartTime( - setupBookableSpacesBloc.startTime!, timePicked)) { - ScaffoldMessenger.of(context).clearSnackBars(); - ScaffoldMessenger.of(context).showSnackBar(const SnackBar( - content: - Text("You can't choose End Time After Start time"), - duration: Duration(seconds: 2), - backgroundColor: ColorsManager.red, - )); - throw Exception(); - } else { - for (int i = 0; - i < - setupBookableSpacesBloc - .selectedBookableSpaces.length; - i++) { - final space = - setupBookableSpacesBloc.selectedBookableSpaces[i]; - final updatedConfig = space.spaceConfig - ?.copyWith(bookingEndTime: timePicked); - final updatedSpace = - space.copyWith(spaceConfig: updatedConfig); - setupBookableSpacesBloc.selectedBookableSpaces[i] = - updatedSpace; - } - } + context.read().add( + ChangeEndTimeEvent(endTime: pickedEndTime), + ); + context.read().add( + CheckConfigurValidityEvent(), + ); }, ), - const SizedBox( - width: 15, - ), + const SizedBox(width: 15), Container( width: 30, height: 32, + alignment: Alignment.center, decoration: const BoxDecoration( borderRadius: BorderRadius.only( topLeft: Radius.circular(10), bottomLeft: Radius.circular(10), ), ), - alignment: Alignment.center, child: SvgPicture.asset( Assets.clockIcon, height: 18, color: ColorsManager.blackColor.withValues(alpha: 0.4), ), - ) + ), ], ), ), ], ); } + + void _showInvalidSnackBar(BuildContext context, String message) { + ScaffoldMessenger.of(context).clearSnackBars(); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(message), + duration: const Duration(seconds: 2), + backgroundColor: ColorsManager.red, + ), + ); + } } diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/check_box_space_widget.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/check_box_space_widget.dart index 6fdb0b0d..862b11fa 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/check_box_space_widget.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/check_box_space_widget.dart @@ -7,12 +7,10 @@ import 'package:syncrow_web/utils/color_manager.dart'; class CheckBoxSpaceWidget extends StatelessWidget { final BookableSpacemodel nonBookableSpace; - final List selectedSpaces; const CheckBoxSpaceWidget({ super.key, required this.nonBookableSpace, - required this.selectedSpaces, }); @override @@ -37,12 +35,15 @@ class CheckBoxSpaceWidget extends StatelessWidget { value: isChecked, onChanged: (value) { final bloc = context.read(); + if (value ?? false) { bloc.add(AddToBookableSpaceEvent( - nonBookableSpace: nonBookableSpace)); + nonBookableSpace: nonBookableSpace, + )); } else { bloc.add(RemoveFromBookableSpaceEvent( - bookableSpace: nonBookableSpace)); + bookableSpace: nonBookableSpace, + )); } }, ); diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/next_first_step_button.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/next_first_step_button.dart index 23c759a8..12e6bb2e 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/next_first_step_button.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/next_first_step_button.dart @@ -1,32 +1,42 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:go_router/go_router.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/presentation/blocs/setup_bookable_spaces_bloc/setup_bookable_spaces_bloc.dart'; import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/presentation/blocs/steps_cubit/cubit/steps_cubit.dart'; import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/presentation/widgets/buttons_divider_bottom_dialog_widget.dart'; class NextFirstStepButton extends StatelessWidget { - final List selectedSpaces; - const NextFirstStepButton({ super.key, - required this.selectedSpaces, }); @override Widget build(BuildContext context) { - return ButtonsDividerBottomDialogWidget( - title: 'Next', - onNextPressed: selectedSpaces.isEmpty - ? null - : () { - context.read().goToNextStep(); - context - .read() - .add(CheckConfigurValidityEvent()); - }, - onCancelPressed: () => context.pop(), + return BlocBuilder( + builder: (context, state) { + return switch (state) { + SetupBookableSpacesInitial() => ButtonsDividerBottomDialogWidget( + title: 'Next', + onNextPressed: null, + onCancelPressed: () => context.pop(), + ), + AddNonBookableSpaceIntoBookableState(:final bookableSpaces) || + RemoveBookableSpaceIntoNonBookableState(:final bookableSpaces) => + ButtonsDividerBottomDialogWidget( + title: 'Next', + onNextPressed: bookableSpaces.isEmpty + ? null + : () { + context.read().goToNextStep(); + context.read().add( + CheckConfigurValidityEvent(), + ); + }, + onCancelPressed: () => context.pop(), + ), + _ => const SizedBox(), + }; + }, ); } } diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/points_part_widget.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/points_part_widget.dart index 0dcad1f2..3df5343d 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/points_part_widget.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/points_part_widget.dart @@ -9,14 +9,14 @@ import 'package:syncrow_web/utils/color_manager.dart'; class PointsPartWidget extends StatefulWidget { final BookableSpacemodel? editingBookableSpace; + final TextEditingController pointsController; + const PointsPartWidget({ super.key, required this.pointsController, this.editingBookableSpace, }); - final TextEditingController pointsController; - @override State createState() => _PointsPartWidgetState(); } @@ -24,26 +24,28 @@ class PointsPartWidget extends StatefulWidget { class _PointsPartWidgetState extends State { @override void initState() { + super.initState(); + if (widget.editingBookableSpace != null) { widget.pointsController.text = widget.editingBookableSpace!.spaceConfig!.cost.toString(); } - super.initState(); } @override Widget build(BuildContext context) { return BlocBuilder( - builder: (context, state) { + builder: (context, switchState) { + final isSwitchOn = switchState is ActivatePointsSwitch; + return Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Row( - mainAxisAlignment: MainAxisAlignment.start, children: [ - if (state is ActivatePointsSwitch) + if (isSwitchOn) Text( '* ', style: Theme.of(context) @@ -52,9 +54,7 @@ class _PointsPartWidgetState extends State { .copyWith(color: Colors.red), ) else - const SizedBox( - width: 11, - ), + const SizedBox(width: 11), const Text('Points/hrs'), ], ), @@ -62,83 +62,54 @@ class _PointsPartWidgetState extends State { scale: 0.7, child: Switch( materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, - trackOutlineColor: WidgetStateProperty.resolveWith( - (Set states) { - return ColorsManager.whiteColors; - }, - ), + trackOutlineColor: + WidgetStateProperty.all(ColorsManager.whiteColors), activeTrackColor: ColorsManager.dialogBlueTitle, inactiveTrackColor: ColorsManager.lightGrayBorderColor, - thumbColor: WidgetStateProperty.resolveWith( - (Set states) { - return ColorsManager.whiteColors; - }, - ), - value: state is ActivatePointsSwitch, + thumbColor: + WidgetStateProperty.all(ColorsManager.whiteColors), + value: isSwitchOn, onChanged: (value) { + final toggleCubit = + context.read(); final bloc = context.read(); + final updatedCost = value ? -1 : 0; - final switchCubit = - context.read(); if (value) { - switchCubit.activateSwitch(); + toggleCubit.activateSwitch(); } else { - switchCubit.unActivateSwitch(); + toggleCubit.unActivateSwitch(); widget.pointsController.clear(); } - - for (int i = 0; - i < bloc.selectedBookableSpaces.length; - i++) { - final space = bloc.selectedBookableSpaces[i]; - final updatedConfig = - space.spaceConfig?.copyWith(cost: updatedCost); - final updatedSpace = - space.copyWith(spaceConfig: updatedConfig); - - bloc.selectedBookableSpaces[i] = updatedSpace; - } - + bloc.add(ChangeCostEvent(cost: updatedCost)); bloc.add(CheckConfigurValidityEvent()); }, ), - ) + ), ], ), - const SizedBox( - height: 5, - ), - if (state is ActivatePointsSwitch) + const SizedBox(height: 5), + if (isSwitchOn) SearchUnbookableSpacesWidget( title: 'Ex: 0', topPadding: 0, blur: 1, raduis: 10, height: 34, - onChanged: (p0) { + controller: widget.pointsController, + suffix: const SizedBox(), + inputFormatters: [FilteringTextInputFormatter.digitsOnly], + onChanged: (_) { final updatedCost = int.tryParse(widget.pointsController.text) ?? 0; - - final bloc = context.read(); - - for (var i = 0; i < bloc.selectedBookableSpaces.length; i++) { - final space = bloc.selectedBookableSpaces[i]; - final updatedConfig = - space.spaceConfig?.copyWith(cost: updatedCost); - final updatedSpace = - space.copyWith(spaceConfig: updatedConfig); - - bloc.selectedBookableSpaces[i] = updatedSpace; - } - context .read() - .add(CheckConfigurValidityEvent()); + .add(ChangeCostEvent(cost: updatedCost)); + context.read().add( + CheckConfigurValidityEvent(), + ); }, - controller: widget.pointsController, - inputFormatters: [FilteringTextInputFormatter.digitsOnly], - suffix: const SizedBox(), ) else const SizedBox(), diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/save_second_step_button.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/save_second_step_button.dart index e9fc0289..de35a38a 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/save_second_step_button.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/save_second_step_button.dart @@ -10,68 +10,68 @@ import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/prese import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/presentation/widgets/buttons_divider_bottom_dialog_widget.dart'; class SaveSecondStepButton extends StatelessWidget { - final List selectedSpaces; final TextEditingController pointsController; final bool isEditingMode; + final List bookableSpaces; const SaveSecondStepButton({ super.key, - required this.selectedSpaces, required this.pointsController, required this.isEditingMode, + required this.bookableSpaces, }); @override Widget build(BuildContext context) { - return BlocConsumer( + return BlocListener( listener: (context, state) { if (state is SendBookableSpacesSuccess) { context.read().add(CallInitStateEvent()); } else if (state is SendBookableSpacesError) { ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text(state.error), - ), + SnackBar(content: Text(state.error)), ); } }, - builder: (context, state) { - return ButtonsDividerBottomDialogWidget( - title: 'Save', - onNextPressed: state is UnValidSaveButtonState - ? null - : () { - if (selectedSpaces.any( - (element) => element.isValid, - )) { - isEditingMode - ? callEditLogic(context) - : context.read().add( + child: BlocBuilder( + builder: (context, state) { + return ButtonsDividerBottomDialogWidget( + title: 'Save', + onNextPressed: state is UnValidSaveButtonState + ? null + : () { + if (bookableSpaces.any((e) => e.isValid)) { + if (isEditingMode) { + callEditLogic(context); + } else { + context.read().add( SendBookableSpacesToApi( - selectedBookableSpaces: context - .read() - .selectedBookableSpaces), + selectedBookableSpaces: bookableSpaces, + ), ); - } - }, - onCancelPressed: () => context.pop(), - ); - }, + } + } + }, + onCancelPressed: () => context.pop(), + ); + }, + ), ); } void callEditLogic(BuildContext context) { - context.read().add( - UpdateBookableSpace( - onSuccess: () => - context.read().add(CallInitStateEvent()), - updatedParam: UpdateBookableSpaceParam.fromBookableModel( - context - .read() - .selectedBookableSpaces - .first, + print(bookableSpaces.first.spaceConfig!.cost); + if (bookableSpaces.isNotEmpty) { + context.read().add( + UpdateBookableSpace( + onSuccess: () => context + .read() + .add(CallInitStateEvent()), + updatedParam: UpdateBookableSpaceParam.fromBookableModel( + bookableSpaces.first, + ), ), - ), - ); + ); + } } } diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/time_picker_widget.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/time_picker_widget.dart index b72a714d..c215079e 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/time_picker_widget.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/time_picker_widget.dart @@ -12,7 +12,7 @@ class TimePickerWidget extends StatefulWidget { required this.onTimePicked, required this.title, }); - late SetupBookableSpacesBloc setupBookableSpacesBloc; + late final SetupBookableSpacesBloc setupBookableSpacesBloc; final void Function(TimeOfDay? timePicked) onTimePicked; @override State createState() => _TimePickerWidgetState(); @@ -47,9 +47,14 @@ class _TimePickerWidgetState extends State { ); }, ); + + if (tempTime == null) return; + widget.onTimePicked(tempTime); timePicked = tempTime; + widget.setupBookableSpacesBloc.add(CheckConfigurValidityEvent()); + setState(() {}); }, child: Container( diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/unbookable_list_widget.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/unbookable_list_widget.dart index 225514bb..2aa09b24 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/unbookable_list_widget.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/unbookable_list_widget.dart @@ -36,9 +36,7 @@ class UnbookableListWidget extends StatelessWidget { if (index < nonBookableSpaces.data.length) { return CheckBoxSpaceWidget( nonBookableSpace: nonBookableSpaces.data[index], - selectedSpaces: context - .read() - .selectedBookableSpaces, + ); } else { return const Padding( diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/week_checkbox_title_widget.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/week_checkbox_title_widget.dart index 3673ef1c..c01dc714 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/week_checkbox_title_widget.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/week_checkbox_title_widget.dart @@ -25,6 +25,7 @@ class _WeekDaysCheckboxRowState extends State { 'Sat': false, 'Sun': false, }; + @override void initState() { super.initState(); @@ -56,9 +57,7 @@ class _WeekDaysCheckboxRowState extends State { const Text('Days'), ], ), - const SizedBox( - height: 20, - ), + const SizedBox(height: 20), Row( crossAxisAlignment: CrossAxisAlignment.start, children: _daysChecked.entries.map((entry) { @@ -79,34 +78,24 @@ class _WeekDaysCheckboxRowState extends State { .map((e) => e.key) .toList(); - final bloc = context.read(); - - for (int i = 0; - i < bloc.selectedBookableSpaces.length; - i++) { - final space = bloc.selectedBookableSpaces[i]; - final updatedConfig = space.spaceConfig - ?.copyWith(bookableDays: selectedDays); - final updatedSpace = - space.copyWith(spaceConfig: updatedConfig); - - bloc.selectedBookableSpaces[i] = updatedSpace; - } - - bloc.add(CheckConfigurValidityEvent()); + context.read().add( + AddBookableDaysEvent(bookableDays: selectedDays), + ); + context.read().add( + CheckConfigurValidityEvent(), + ); }, ), - const SizedBox( - width: 8, - ), + const SizedBox(width: 8), Expanded( - child: Text( - entry.key, - style: const TextStyle( - fontSize: 13, - fontWeight: FontWeight.w400, + child: Text( + entry.key, + style: const TextStyle( + fontSize: 13, + fontWeight: FontWeight.w400, + ), ), - )), + ), ], ), );