Skip to content

Commit

Permalink
Done Ulasan
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaffailhami committed Jun 28, 2024
1 parent 80e950f commit 36f5717
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 7 deletions.
61 changes: 59 additions & 2 deletions lib/components/ulasan_modal_bottom_sheet.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gasku/cubits/daftar_pangkalan.dart';
import 'package:gasku/cubits/pengguna_masuk.dart';
import 'package:gasku/models/pangkalan.dart';
import 'package:gasku/models/pengguna.dart';
import 'package:gasku/pages/detail_pangkalan.dart';
import 'package:gasku/utils/show_loading_screen.dart';

class MyUlasanModalBottomSheet extends StatefulWidget {
const MyUlasanModalBottomSheet({super.key});
const MyUlasanModalBottomSheet({super.key, required this.pangkalan});

final Pangkalan pangkalan;

@override
State<MyUlasanModalBottomSheet> createState() =>
Expand All @@ -10,6 +19,53 @@ class MyUlasanModalBottomSheet extends StatefulWidget {

class _MyUlasanModalBottomSheetState extends State<MyUlasanModalBottomSheet> {
int _rating = 0;
String _komentar = '';

Future<void> _onSubmit() async {
showLoadingScreen(context);

final Pengguna pengguna = context.read<PenggunaMasukCubit>().state!;

widget.pangkalan.ulasan.add({
'nik': pengguna.nik,
'rating': _rating,
'komentar': _komentar,
});

try {
await Pangkalan.updateOnly(
widget.pangkalan.id!,
{'ulasan': widget.pangkalan.ulasan},
);

Navigator.of(context).pop();
Navigator.of(context).pop();
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) =>
MyDetailPangkalanPage(pangkalan: widget.pangkalan),
),
);

ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Ulasan terkirim'),
backgroundColor: Theme.of(context).colorScheme.primary,
action: SnackBarAction(
label: 'Tutup',
onPressed: () => ScaffoldMessenger.of(context).hideCurrentSnackBar(),
),
));
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(e.toString()),
backgroundColor: Theme.of(context).colorScheme.primary,
action: SnackBarAction(
label: 'Tutup',
onPressed: () => ScaffoldMessenger.of(context).hideCurrentSnackBar(),
),
));
}
}

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -62,6 +118,7 @@ class _MyUlasanModalBottomSheetState extends State<MyUlasanModalBottomSheet> {
TextField(
keyboardType: TextInputType.multiline,
maxLines: 3,
onChanged: (value) => _komentar = value,
decoration: InputDecoration(
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
Expand All @@ -76,7 +133,7 @@ class _MyUlasanModalBottomSheetState extends State<MyUlasanModalBottomSheet> {
SizedBox(
width: double.infinity,
child: FilledButton(
onPressed: () {},
onPressed: () => _onSubmit(),
child: const Text(
'Kirim Ulasan',
style: TextStyle(fontWeight: FontWeight.bold),
Expand Down
15 changes: 15 additions & 0 deletions lib/cubits/daftar_pangkalan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,19 @@ class DaftarPangkalanCubit extends Cubit<List<Pangkalan>> {

Future<void> search(String keywords) async =>
emit(await Pangkalan.get(search: keywords));

// Future<void> tambahUlasan(
// String idPangkalan, Map<String, dynamic> ulasan) async {
// await Pangkalan.updateOnly(
// idPangkalan,
// {'ulasan': ulasan},
// );

// final List<Pangkalan> newState = List.from(state);
// final edittedIndex =
// newState.indexWhere((pangkalan) => pangkalan.id == idPangkalan);
// newState[edittedIndex].ulasan.add(ulasan);

// emit(newState);
// }
}
32 changes: 28 additions & 4 deletions lib/models/pangkalan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:http/http.dart' as http;

class Pangkalan {
final String? id;
final String nama;
final String alamat;
final int harga;
Expand All @@ -18,6 +19,7 @@ class Pangkalan {
final List ulasan;

Pangkalan({
this.id,
required this.nama,
required this.alamat,
required this.harga,
Expand All @@ -34,6 +36,7 @@ class Pangkalan {
static final url = '${dotenv.env['SERVER_URL']}/pangkalan';

Pangkalan copyWith({
String? id,
String? nama,
String? alamat,
int? harga,
Expand All @@ -47,6 +50,7 @@ class Pangkalan {
List? ulasan,
}) {
return Pangkalan(
id: id ?? this.id,
nama: nama ?? this.nama,
alamat: alamat ?? this.alamat,
harga: harga ?? this.harga,
Expand Down Expand Up @@ -77,6 +81,7 @@ class Pangkalan {
for (Map<String, dynamic> pangkalan in response['pangkalan']) {
daftarPangkalan.add(
Pangkalan(
id: pangkalan['_id'],
nama: pangkalan['nama'],
alamat: pangkalan['alamat'],
harga: pangkalan['harga'],
Expand All @@ -95,17 +100,33 @@ class Pangkalan {
return daftarPangkalan;
}

static Future<void> updateOnly(String id, Map<String, dynamic> data) async {
final Map<String, dynamic> response = json.decode((await http.patch(
Uri.parse('$url/$id'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(data),
))
.body);

if (response['status'] == 'failed') throw response['message'];
}

double get ratingAverage {
double sum = 0;
for (var u in ulasan) {
sum = sum + u['rating'];
}

return ulasan.isEmpty ? 0 : sum / ulasan.length;
final double average = sum / ulasan.length;

return ulasan.isEmpty ? 0 : double.parse(average.toStringAsFixed(1));
}

Map<String, dynamic> toMap() {
return <String, dynamic>{
'_id': id,
'nama': nama,
'alamat': alamat,
'harga': harga,
Expand All @@ -122,6 +143,7 @@ class Pangkalan {

factory Pangkalan.fromMap(Map<String, dynamic> map) {
return Pangkalan(
id: map['_id'] as String,
nama: map['nama'] as String,
alamat: map['alamat'] as String,
harga: map['harga'] as int,
Expand Down Expand Up @@ -149,14 +171,15 @@ class Pangkalan {

@override
String toString() {
return 'Pangkalan(nama: $nama, alamat: $alamat, harga: $harga, stok: $stok, email: $email, telepon: $telepon, gmap: $gmap, kataSandi: $kataSandi, coordinates: $coordinates, foto: $foto, ulasan: $ulasan)';
return 'Pangkalan(id: $id, nama: $nama, alamat: $alamat, harga: $harga, stok: $stok, email: $email, telepon: $telepon, gmap: $gmap, kataSandi: $kataSandi, coordinates: $coordinates, foto: $foto, ulasan: $ulasan)';
}

@override
bool operator ==(covariant Pangkalan other) {
if (identical(this, other)) return true;

return other.nama == nama &&
return other.id == id &&
other.nama == nama &&
other.alamat == alamat &&
other.harga == harga &&
other.stok == stok &&
Expand All @@ -171,7 +194,8 @@ class Pangkalan {

@override
int get hashCode {
return nama.hashCode ^
return id.hashCode ^
nama.hashCode ^
alamat.hashCode ^
harga.hashCode ^
stok.hashCode ^
Expand Down
5 changes: 4 additions & 1 deletion lib/pages/detail_pangkalan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class MyDetailPangkalanPage extends StatelessWidget {
builder: (context) {
final double ratingAverage =
pangkalan.ratingAverage;

final int totalStar = ratingAverage.floor();
final List<Widget> widgets = [];

Expand Down Expand Up @@ -269,7 +270,9 @@ class MyDetailPangkalanPage extends StatelessWidget {
context: context,
isScrollControlled: true,
builder: (context) {
return const MyUlasanModalBottomSheet();
return MyUlasanModalBottomSheet(
pangkalan: pangkalan,
);
},
);
},
Expand Down

0 comments on commit 36f5717

Please sign in to comment.