Skip to content

Commit

Permalink
fixing show live data after addreview
Browse files Browse the repository at this point in the history
  • Loading branch information
crosbydoo committed Feb 16, 2024
1 parent 553176e commit be58807
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ class RestaurantBloc extends Bloc<RestaurantEvent, RestaurantState> {
dataReview: response,
error: null,
);

emit(ReviewSuccessState(stateData));

// add(RestaurantShowDetailEvent(id: event.id));
// emit(RestaurantDetailSuccessState(stateData));
},
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class RestaurantShowListEvent extends RestaurantEvent {
}

class RestaurantShowDetailEvent extends RestaurantEvent {
final String id;
final String? id;

const RestaurantShowDetailEvent({required this.id});
const RestaurantShowDetailEvent({this.id});

@override
List<Object?> get props => [id];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,82 +1,86 @@
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:get_it/get_it.dart';
import 'package:go_router/go_router.dart';
import 'package:restaurant_app/src/restaurant/presentation/bloc/restaurant/restaurant_bloc.dart';

import 'package:restaurant_app/resources/styles/typograph.dart';
import 'package:restaurant_app/resources/widgets/common_button.dart';
import 'package:restaurant_app/resources/widgets/common_snackbar.dart';
import 'package:restaurant_app/resources/widgets/common_textformfield.dart';
import 'package:restaurant_app/resources/widgets/common_button.dart';
import 'package:restaurant_app/resources/styles/typograph.dart';
import 'package:gap/gap.dart';
import 'package:restaurant_app/src/restaurant/presentation/bloc/restaurant/restaurant_bloc.dart';

class FormReviewComponent extends HookWidget {
class FormReviewComponent extends StatefulWidget {
const FormReviewComponent({super.key, required this.id});

final String id;

@override
Widget build(BuildContext context) {
final nameInput = useTextEditingController();
final reviewInput = useTextEditingController();
final formKey = useMemoized(() => GlobalKey<FormState>());
State<FormReviewComponent> createState() => _FormReviewComponentState();
}

bool isSubmitDisabled = nameInput.text.isEmpty || reviewInput.text.isEmpty;
class _FormReviewComponentState extends State<FormReviewComponent> {
final TextEditingController nameInput = TextEditingController();
final TextEditingController reviewInput = TextEditingController();

useEffect(() {
final subscription =
context.read<RestaurantBloc>().stream.listen((state) {
if (state is ReviewSuccessState) {
CommonSnackbar.showSuccessSnackbar(
context: context,
title: 'Success',
message: 'Successfully adding review',
);
context.pop(); // Kembali ke halaman sebelumnya
}
});
// Membersihkan langganan saat komponen di-unmount
return subscription.cancel;
}, []);
final _formKey = GlobalKey<FormState>();
late RestaurantBloc bloc;

@override
void initState() {
bloc = RestaurantBloc(
detailUsecase: GetIt.instance(),
usecase: GetIt.instance(),
reviewUsecase: GetIt.instance(),
);
super.initState();
}

@override
Widget build(BuildContext context) {
return Container(
height: MediaQuery.of(context).size.height * 0.45,
height: MediaQuery.sizeOf(context).height * 0.45,
width: double.infinity,
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 20),
child: Form(
key: formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Add Your Review',
style: StyleTypograph.heading3.bold,
),
const Gap(20),
Gap(20),
CommonTextForm(
controller: nameInput,
obscured: false,
hint: 'Your name',
),
const Gap(12),
Gap(12),
CommonTextForm(
controller: reviewInput,
obscured: false,
hint: 'Message review',
hint: 'Add Review',
),
Spacer(),
Container(
child: CommonButton.normalButton(
disabled: isSubmitDisabled,
onPressed: () {
if (formKey.currentState!.validate()) {
context.read<RestaurantBloc>().add(
AddReviewEvent(
id: id,
name: nameInput.text,
review: reviewInput.text,
),
);
onPressed: () async {
if (_formKey.currentState!.validate() == true) {
bloc.add(
AddReviewEvent(
id: widget.id,
name: nameInput.text,
review: reviewInput.text,
),
);
CommonSnackbar.showSuccessSnackbar(
context: context,
title: 'Success',
message: 'Successfully adding review',
);

context.pop(true);
}
},
text: 'Submit',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class _DetailRestaurantScreenState extends State<DetailRestaurantScreen> {
return boxBookmark;
}

void refreshData() {
setState(() {
bloc.add(RestaurantShowDetailEvent(id: widget.id!));
});
}

@override
Widget build(BuildContext context) {
return BlocConsumer<RestaurantBloc, RestaurantState>(
Expand Down Expand Up @@ -159,6 +165,8 @@ class _DetailRestaurantScreenState extends State<DetailRestaurantScreen> {
),
);
},
).then(
(value) => refreshData(),
);
},
),
Expand Down

0 comments on commit be58807

Please sign in to comment.