Initial commit
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../../../../router/app_routes.dart';
|
||||
import '../../../data/models/comment_request.dart';
|
||||
import '../../../domain/usecase/comment_usecase.dart';
|
||||
|
||||
part 'comment_event.dart';
|
||||
|
||||
part 'comment_state.dart';
|
||||
|
||||
class CommentBloc extends Bloc<CommentEvent, CommentState> {
|
||||
CommentBloc(this.commentUseCase)
|
||||
: super(const CommentState(rating: 0, isLoading: false)) {
|
||||
on<RatingChangedEvent>(_ratingChange);
|
||||
on<CommentEnterEvent>(_commentEnter);
|
||||
on<SubmitEvent>(_commentSubmit);
|
||||
}
|
||||
|
||||
final CommentUseCase commentUseCase;
|
||||
|
||||
void _ratingChange(RatingChangedEvent event, Emitter<CommentState> emit) {
|
||||
emit(state.copyWith(rating: event.rating));
|
||||
}
|
||||
|
||||
void _commentEnter(CommentEnterEvent event, Emitter<CommentState> emit) {
|
||||
emit(state.copyWith(comment: event.comment));
|
||||
}
|
||||
|
||||
Future<void> _commentSubmit(
|
||||
SubmitEvent event,
|
||||
Emitter<CommentState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
final response = await commentUseCase(
|
||||
CommentRequest(
|
||||
orderNumber: event.orderId,
|
||||
rate: state.rating.toInt(),
|
||||
message: state.comment,
|
||||
),
|
||||
);
|
||||
response.fold(
|
||||
(l) {
|
||||
emit(state.copyWith(isLoading: false));
|
||||
},
|
||||
(r) {
|
||||
emit(state.copyWith(isLoading: false));
|
||||
Navigator.pop(rootNavigatorKey.currentContext!);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
part of 'comment_bloc.dart';
|
||||
|
||||
sealed class CommentEvent extends Equatable {
|
||||
const CommentEvent();
|
||||
}
|
||||
|
||||
final class RatingChangedEvent extends CommentEvent {
|
||||
final double rating;
|
||||
|
||||
const RatingChangedEvent({required this.rating});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [rating];
|
||||
}
|
||||
|
||||
final class CommentEnterEvent extends CommentEvent {
|
||||
final String comment;
|
||||
|
||||
const CommentEnterEvent({required this.comment});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [comment];
|
||||
}
|
||||
|
||||
final class SubmitEvent extends CommentEvent {
|
||||
const SubmitEvent({required this.orderId});
|
||||
|
||||
final String orderId;
|
||||
|
||||
@override
|
||||
List<Object?> get props => [orderId];
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
part of 'comment_bloc.dart';
|
||||
|
||||
class CommentState extends Equatable {
|
||||
const CommentState({
|
||||
required this.rating,
|
||||
required this.isLoading,
|
||||
this.comment,
|
||||
});
|
||||
|
||||
final double rating;
|
||||
final bool isLoading;
|
||||
final String? comment;
|
||||
|
||||
CommentState copyWith({double? rating, bool? isLoading, String? comment}) {
|
||||
return CommentState(
|
||||
rating: rating ?? this.rating,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
comment: comment ?? this.comment,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [rating, isLoading, comment];
|
||||
}
|
||||
Reference in New Issue
Block a user