Skip to content

Commit

Permalink
Refactor more
Browse files Browse the repository at this point in the history
  • Loading branch information
DeD1rk committed Jan 31, 2023
1 parent d4ae310 commit b1b4787
Showing 1 changed file with 140 additions and 123 deletions.
263 changes: 140 additions & 123 deletions lib/ui/screens/event_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ class _EventScreenState extends State<EventScreen> {

if (event.canCreateRegistration) {
if (event.reachedMaxParticipants) {
registrationButton = _makeJoinQueueButton(event);
registrationButton = _JoinQueueButton(event);
} else {
registrationButton = _makeCreateRegistrationButton(event);
registrationButton = _CreateRegistrationButton(event);
}
} else if (event.canCancelRegistration) {
if (event.cancelDeadlinePassed()) {
Expand Down Expand Up @@ -385,103 +385,6 @@ class _EventScreenState extends State<EventScreen> {
);
}

Widget _makeCreateRegistrationButton(Event event) {
return ElevatedButton.icon(
onPressed: () async {
final messenger = ScaffoldMessenger.of(context);
final calendarCubit = BlocProvider.of<CalendarCubit>(context);
final router = GoRouter.of(context);
var confirmed = !event.cancelDeadlinePassed();
if (!confirmed) {
confirmed = await showDialog<bool>(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Register'),
content: Text(
'Are you sure you want to register? The '
'cancellation deadline has already passed.',
style: Theme.of(context).textTheme.bodyText2,
),
actions: [
TextButton.icon(
onPressed: () => Navigator.of(
context,
rootNavigator: true,
).pop(false),
icon: const Icon(Icons.clear),
label: const Text('NO'),
),
ElevatedButton.icon(
onPressed: () => Navigator.of(
context,
rootNavigator: true,
).pop(true),
icon: const Icon(Icons.check),
label: const Text('YES'),
),
],
);
},
) ??
false;
}

if (confirmed) {
try {
final registration = await _eventCubit.register();
if (event.hasFields) {
router.pushNamed('event-registration', params: {
'eventPk': event.pk.toString(),
'registrationPk': registration.pk.toString(),
});
}
calendarCubit.load();
} on ApiException {
messenger.showSnackBar(const SnackBar(
behavior: SnackBarBehavior.floating,
content: Text('Could not register for the event.'),
));
}
await _registrationsCubit.load();
}
},
icon: const Icon(Icons.create_outlined),
label: const Text('REGISTER'),
);
}

Widget _makeJoinQueueButton(Event event) {
return ElevatedButton.icon(
onPressed: () async {
final messenger = ScaffoldMessenger.of(context);
final calendarCubit = BlocProvider.of<CalendarCubit>(context);
final router = GoRouter.of(context);
try {
final registration = await _eventCubit.register();
if (event.hasFields) {
router.pushNamed(
'event-registration',
params: {
'eventPk': event.pk.toString(),
'registrationPk': registration.pk.toString(),
},
);
}
calendarCubit.load();
} on ApiException {
messenger.showSnackBar(const SnackBar(
behavior: SnackBarBehavior.floating,
content: Text('Could not join the waiting list for the event.'),
));
}
await _registrationsCubit.load();
},
icon: const Icon(Icons.create_outlined),
label: const Text('JOIN QUEUE'),
);
}

TextSpan _makeTermsAndConditions(Event event) {
final url = config.termsAndConditionsUrl;
return TextSpan(
Expand Down Expand Up @@ -674,6 +577,100 @@ class _EventScreenState extends State<EventScreen> {
}
}

class _JoinQueueButton extends StatelessWidget {
const _JoinQueueButton(this.event);

final Event event;

@override
Widget build(BuildContext context) {
return ElevatedButton.icon(
onPressed: () async {
final router = GoRouter.of(context);
final messenger = ScaffoldMessenger.of(context);
final calendarCubit = BlocProvider.of<CalendarCubit>(context);
final eventCubit = BlocProvider.of<EventCubit>(context);
final registrationsCubit = BlocProvider.of<RegistrationsCubit>(context);

try {
final registration = await eventCubit.register();
if (event.hasFields) {
router.pushNamed(
'event-registration',
params: {
'eventPk': event.pk.toString(),
'registrationPk': registration.pk.toString(),
},
);
}
calendarCubit.load();
} on ApiException {
messenger.showSnackBar(const SnackBar(
behavior: SnackBarBehavior.floating,
content: Text('Could not join the waiting list for the event.'),
));
}
registrationsCubit.load();
},
icon: const Icon(Icons.create_outlined),
label: const Text('JOIN QUEUE'),
);
}
}

class _CreateRegistrationButton extends StatelessWidget {
const _CreateRegistrationButton(this.event);

final Event event;

@override
Widget build(BuildContext context) {
return ElevatedButton.icon(
onPressed: () async {
final router = GoRouter.of(context);
final messenger = ScaffoldMessenger.of(context);
final calendarCubit = BlocProvider.of<CalendarCubit>(context);
final eventCubit = BlocProvider.of<EventCubit>(context);
final registrationsCubit = BlocProvider.of<RegistrationsCubit>(context);

var confirmed = !event.cancelDeadlinePassed();
if (!confirmed) {
confirmed = await showDialog<bool>(
context: context,
builder: (context) => const _ConfirmationDialog(
titleText: 'Register',
warningText: 'Are you sure you want to register? '
'The cancellation deadline has already passed.',
),
) ??
false;
}

if (confirmed) {
try {
final registration = await eventCubit.register();
if (event.hasFields) {
router.pushNamed('event-registration', params: {
'eventPk': event.pk.toString(),
'registrationPk': registration.pk.toString(),
});
}
calendarCubit.load();
} on ApiException {
messenger.showSnackBar(const SnackBar(
behavior: SnackBarBehavior.floating,
content: Text('Could not register for the event.'),
));
}
registrationsCubit.load();
}
},
icon: const Icon(Icons.create_outlined),
label: const Text('REGISTER'),
);
}
}

class _IWontBeThereButton extends StatelessWidget {
const _IWontBeThereButton(this.event);

Expand Down Expand Up @@ -762,30 +759,9 @@ class _CancelRegistrationButton extends StatelessWidget {
final confirmed = await showDialog<bool>(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Cancel registration'),
content: Text(
warningText,
style: Theme.of(context).textTheme.bodyText2,
),
actions: [
TextButton.icon(
onPressed: () => Navigator.of(
context,
rootNavigator: true,
).pop(false),
icon: const Icon(Icons.clear),
label: const Text('NO'),
),
ElevatedButton.icon(
onPressed: () => Navigator.of(
context,
rootNavigator: true,
).pop(true),
icon: const Icon(Icons.check),
label: const Text('YES'),
),
],
return _ConfirmationDialog(
titleText: 'Cancel registration',
warningText: warningText,
);
},
);
Expand All @@ -812,6 +788,47 @@ class _CancelRegistrationButton extends StatelessWidget {
}
}

/// A dialog that shows a message with buttons 'YES' and 'NO', popping with a bool.
class _ConfirmationDialog extends StatelessWidget {
const _ConfirmationDialog({
Key? key,
required this.titleText,
required this.warningText,
}) : super(key: key);

final String titleText;
final String warningText;

@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(titleText),
content: Text(
warningText,
style: Theme.of(context).textTheme.bodyText2,
),
actions: [
TextButton.icon(
onPressed: () => Navigator.of(
context,
rootNavigator: true,
).pop(false),
icon: const Icon(Icons.clear),
label: const Text('NO'),
),
ElevatedButton.icon(
onPressed: () => Navigator.of(
context,
rootNavigator: true,
).pop(true),
icon: const Icon(Icons.check),
label: const Text('YES'),
),
],
);
}
}

/// A button that opens the registration fields page.
class _UpdateRegistrationButton extends StatelessWidget {
const _UpdateRegistrationButton(this.event);
Expand Down

0 comments on commit b1b4787

Please sign in to comment.