Skip to content

Commit

Permalink
Rework how the error messages work for event admin (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
JAicewizard authored Dec 11, 2023
1 parent 8d49cea commit a164919
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 54 deletions.
138 changes: 91 additions & 47 deletions lib/blocs/event_admin_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ class EventAdminState extends Equatable {
final List<AdminEventRegistration> cancelledRegistrations;
final List<AdminEventRegistration> queuedRegistrations;

final String? exception;
final String? message;
final String? cancelledMessage;
final String? queuedMessage;
final bool isLoading;

bool get hasException => message != null;
bool get hasException => exception != null;

@protected
const EventAdminState({
Expand All @@ -29,7 +32,10 @@ class EventAdminState extends Equatable {
required this.cancelledRegistrations,
required this.queuedRegistrations,
required this.isLoading,
required this.exception,
required this.message,
required this.cancelledMessage,
required this.queuedMessage,
}) : assert(
event != null || isLoading || message != null,
'event can only be null when isLoading or hasException is true.',
Expand All @@ -42,6 +48,8 @@ class EventAdminState extends Equatable {
cancelledRegistrations,
queuedRegistrations,
message,
cancelledMessage,
queuedMessage,
isLoading
];

Expand All @@ -51,7 +59,10 @@ class EventAdminState extends Equatable {
List<AdminEventRegistration>? cancelledRegistrations,
List<AdminEventRegistration>? queuedRegistrations,
bool? isLoading,
String? exception,
String? message,
String? cancelledMessage,
String? queuedMessage,
}) =>
EventAdminState(
event: event ?? this.event,
Expand All @@ -60,30 +71,42 @@ class EventAdminState extends Equatable {
cancelledRegistrations ?? this.cancelledRegistrations,
queuedRegistrations: queuedRegistrations ?? this.queuedRegistrations,
isLoading: isLoading ?? this.isLoading,
exception: exception ?? this.exception,
message: message ?? this.message,
cancelledMessage: cancelledMessage ?? this.cancelledMessage,
queuedMessage: queuedMessage ?? this.queuedMessage,
);

const EventAdminState.result(
{required AdminEvent this.event,
required this.registrations,
required this.cancelledRegistrations,
required this.queuedRegistrations})
: message = null,
isLoading = false;
const EventAdminState.result({
required AdminEvent this.event,
required this.registrations,
required this.cancelledRegistrations,
required this.queuedRegistrations,
this.exception,
this.message,
this.cancelledMessage,
this.queuedMessage,
}) : isLoading = false;

const EventAdminState.loading(
{this.event,
required this.registrations,
required this.cancelledRegistrations,
required this.queuedRegistrations})
: message = null,
: exception = null,
message = null,
cancelledMessage = null,
queuedMessage = null,
isLoading = true;

const EventAdminState.failure({required String this.message, this.event})
const EventAdminState.failure({required String this.exception, this.event})
: registrations = const [],
cancelledRegistrations = const [],
queuedRegistrations = const [],
isLoading = false;
isLoading = false,
message = null,
cancelledMessage = null,
queuedMessage = null;
}

class EventAdminCubit extends Cubit<EventAdminState> {
Expand Down Expand Up @@ -140,29 +163,38 @@ class EventAdminCubit extends Cubit<EventAdminState> {

// temporary fix, remove when api is updated
registrations.results.removeWhere((r) => r.queuePosition != null);

String? message;
if (registrations.results.isEmpty) {
if (query?.isEmpty ?? true) {
emit(EventAdminState.failure(
event: event,
message: 'There are no registrations.',
));
} else {
emit(EventAdminState.failure(
event: event,
message: 'There are no registrations matching "$query".',
));
}
} else {
emit(EventAdminState.result(
event: event,
registrations: registrations.results,
cancelledRegistrations: cancelledRegistrations.results,
queuedRegistrations: queuedRegistrations.results,
));
message = (query?.isEmpty ?? true)
? 'There are no registrations.'
: 'There are no registrations matching "$query".';
}
String? cancelledMessage;
if (cancelledRegistrations.results.isEmpty) {
cancelledMessage = (query?.isEmpty ?? true)
? 'There are no cancelled registrations.'
: 'There are no cancelled registrations matching "$query".';
}
String? queuedMessage;
if (queuedRegistrations.results.isEmpty) {
queuedMessage = (query?.isEmpty ?? true)
? 'There are no queued registrations.'
: 'There are no queued registrations matching "$query".';
}

emit(EventAdminState.result(
event: event,
registrations: registrations.results,
cancelledRegistrations: cancelledRegistrations.results,
queuedRegistrations: queuedRegistrations.results,
message: message,
cancelledMessage: cancelledMessage,
queuedMessage: queuedMessage,
));
} on ApiException catch (exception) {
emit(EventAdminState.failure(
message: exception.getMessage(notFound: 'The event does not exist.'),
exception: exception.getMessage(notFound: 'The event does not exist.'),
));
}
}
Expand Down Expand Up @@ -201,27 +233,39 @@ class EventAdminCubit extends Cubit<EventAdminState> {

// temporary fix, remove when api is updated
registrations.results.removeWhere((r) => r.queuePosition != null);

String? message;
if (registrations.results.isEmpty) {
if (query?.isEmpty ?? true) {
emit(const EventAdminState.failure(
message: 'There are no registrations.',
));
} else {
emit(EventAdminState.failure(
message: 'There are no registrations matching "$query".',
));
}
} else {
emit(EventAdminState.result(
event: event,
registrations: registrations.results,
cancelledRegistrations: cancelledRegistrations.results,
queuedRegistrations: queuedRegistrations.results,
));
message = (query?.isEmpty ?? true)
? 'There are no registrations.'
: 'There are no registrations matching "$query".';
}
String? cancelledMessage;
if (cancelledRegistrations.results.isEmpty) {
cancelledMessage = (query?.isEmpty ?? true)
? 'There are no cancelled registrations.'
: 'There are no cancelled registrations matching "$query".';
}
String? queuedMessage;
if (queuedRegistrations.results.isEmpty) {
queuedMessage = (query?.isEmpty ?? true)
? 'There are no queued registrations.'
: 'There are no queued registrations matching "$query".';
}

emit(EventAdminState.result(
event: event,
registrations: registrations.results,
cancelledRegistrations: cancelledRegistrations.results,
queuedRegistrations: queuedRegistrations.results,
message: message,
cancelledMessage: cancelledMessage,
queuedMessage: queuedMessage,
));
} on ApiException catch (exception) {
emit(EventAdminState.failure(
message: exception.getMessage(notFound: 'The event does not exist.'),
exception:
exception.getMessage(notFound: 'The event does not exist.'),
));
}
} else {
Expand Down
14 changes: 7 additions & 7 deletions lib/ui/screens/event_admin_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ class _EventAdminScreenState extends State<EventAdminScreen> {
child: BlocBuilder<EventAdminCubit, EventAdminState>(
builder: (context, state) {
if (state.hasException) {
return ErrorScrollView(state.message!);
return ErrorScrollView(state.exception!);
} else if (state.isLoading) {
return const Center(child: CircularProgressIndicator());
} else {
return TabBarView(children: [
if (state.queuedRegistrations.isEmpty)
const ErrorCenter('No queued registrations found')
if (state.queuedMessage != null)
ErrorCenter(state.queuedMessage!)
else
Scrollbar(
child: ListView.separated(
Expand All @@ -166,8 +166,8 @@ class _EventAdminScreenState extends State<EventAdminScreen> {
itemCount: state.queuedRegistrations.length,
),
),
if (state.registrations.isEmpty)
const ErrorCenter('No registrations found')
if (state.message != null)
ErrorCenter(state.message!)
else
Scrollbar(
child: ListView.separated(
Expand All @@ -181,8 +181,8 @@ class _EventAdminScreenState extends State<EventAdminScreen> {
itemCount: state.registrations.length,
),
),
if (state.cancelledRegistrations.isEmpty)
const ErrorCenter('No cancelled registrations found')
if (state.cancelledMessage != null)
ErrorCenter(state.cancelledMessage!)
else
Scrollbar(
child: ListView.separated(
Expand Down

0 comments on commit a164919

Please sign in to comment.