Skip to content

Commit

Permalink
Adding Comments on Detail Review
Browse files Browse the repository at this point in the history
  • Loading branch information
kaenova committed Jun 16, 2022
1 parent f2cbe05 commit c3d2fe8
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 43 deletions.
37 changes: 37 additions & 0 deletions mobile/lib/api/komentar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:mobile/model/komentar.dart';
import 'package:mobile/model/profile_secure.dart';
import 'package:mobile/model/user.dart';

Future<Komentar> postKomentar(int reviewId, String komentar) async {
var profile = await SecureProfile.getStorage();
var formData = FormData.fromMap({"komentar": komentar});
try {
var response = await Dio()
.post("https://travelliu.yaudahlah.my.id/api/komentar/$reviewId",
options: Options(
headers: {
'Authorization': 'Bearer ${profile.getApiKey()}',
"Accept": "application/json",
},
),
data: formData);
var data = response.data;
var user = User.empty(name: profile.name!);
return Komentar(id: data["id"] as int, komentar: komentar, user: user);
} on DioError catch (e) {
if (e.response != null) {
var response = e.response!;
if (response.statusCode == 401) {
await profile.setLoggedOut();
return Future.error("Session expired");
}
if (response.statusCode == 400) {
return Future.error(response.data);
}
}
return Future.error("Gagal untuk mempost review");
}
}
3 changes: 2 additions & 1 deletion mobile/lib/api/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ Future<void> loginUser(String email, String password) async {
var decoded = jsonDecode(response.body);
var token = decoded["token"];
var id = decoded["user"]["id"];
var name = decoded["user"]["name"];

var profile = await SecureProfile.getStorage();
await profile.setLoggedIn(id, token);
await profile.setLoggedIn(id, token, name);
} else if (response.statusCode == 400) {
return Future.error("Email atau password salah");
} else {
Expand Down
26 changes: 17 additions & 9 deletions mobile/lib/model/profile_secure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,32 @@ class SecureProfile {

bool isLoggedIn;
int? userId;
String? apiKey;
String? apiKey, name;

SecureProfile(
{required this.storage,
required this.isLoggedIn,
this.userId,
this.apiKey});
this.apiKey,
this.name});

static Future<SecureProfile> getStorage() async {
var storage = await SharedPreferences.getInstance();
var userId = storage.getInt("user_id");
var apiKey = storage.getString("api_key");
var name = storage.getString("user_name");

bool loggedIn = false;

if ((userId == null || userId == -1) || (apiKey == null || apiKey == "")) {
await storage.setInt("user_id", -1);
await storage.setString("api_key", "");
} else {
if (userId != null && apiKey != null && name != null) {
loggedIn = true;
}

return SecureProfile(
storage: storage, isLoggedIn: loggedIn, apiKey: apiKey, userId: userId);
storage: storage,
isLoggedIn: loggedIn,
apiKey: apiKey,
userId: userId,
name: name);
}

bool getLoggedInStatus() {
Expand All @@ -63,15 +65,21 @@ class SecureProfile {
return userId;
}

Future<void> setLoggedIn(int userId, String apiKey) async {
String? getUserName() {
return name;
}

Future<void> setLoggedIn(int userId, String apiKey, String name) async {
await storage.setInt("user_id", userId);
await storage.setString("api_key", apiKey);
await storage.setString("user_name", name);
isLoggedIn = true;
}

Future<void> setLoggedOut() async {
await storage.remove("api_key");
await storage.remove("user_id");
await storage.remove("user_name");
isLoggedIn = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class KomentarCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 12),
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 12),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(
Expand Down
99 changes: 67 additions & 32 deletions mobile/lib/screen/review_detail/components/review_komentar.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'package:flutter/material.dart';
import 'package:mobile/api/komentar.dart';
import 'package:mobile/model/komentar.dart';
import 'package:mobile/model/profile_secure.dart';
import 'package:mobile/model/review.dart';
import 'package:mobile/screen/review_detail/components/komentar_card.dart';
import 'package:mobile/utils/show_snackbar.dart';

class DetailReviewKomentarSection extends StatefulWidget {
final Review data;
Expand All @@ -16,6 +19,8 @@ class _DetailReviewKomentarSectionState
extends State<DetailReviewKomentarSection> {
bool isLoggedIn = false;
final TextEditingController _commentController = TextEditingController();
final _formKey = GlobalKey<FormState>();
late List<Komentar> comments;

void _checkLogin() async {
var profile = await SecureProfile.getStorage();
Expand All @@ -31,58 +36,88 @@ class _DetailReviewKomentarSectionState
return null;
}

void _handlePostComment() async {}

@override
void initState() {
super.initState();
setState(() {
comments = widget.data.komentar;
});
_checkLogin();
}

@override
Widget build(BuildContext context) {
void _handlePostComment() async {
if (_formKey.currentState!.validate()) {
try {
showDialog(
context: context,
builder: (context) => const Center(
child: SizedBox(
height: 100,
width: 100,
child: CircularProgressIndicator(),
),
));
String komentar = _commentController.text;
Komentar sentKomentar = await postKomentar(widget.data.id, komentar);
_commentController.clear();
setState(() {
comments.insert(0, sentKomentar);
});
Navigator.pop(context);
} catch (e) {
Navigator.pop(context);
ShowSnackBar(context, e.toString());
}
}
}

return Column(
children: [
isLoggedIn
? Form(
key: _formKey,
child: Row(
children: [
Expanded(
child: TextFormField(
controller: _commentController,
validator: _validRequired,
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
children: [
Expanded(
child: TextFormField(
controller: _commentController,
validator: _validRequired,
decoration: InputDecoration(
hintText: "Masukkan komentar",
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide:
const BorderSide(color: Colors.black)),
contentPadding:
const EdgeInsets.symmetric(horizontal: 12),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: const BorderSide(color: Colors.black)),
contentPadding:
const EdgeInsets.symmetric(horizontal: 12),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
),
),
const SizedBox(
width: 13,
),
ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.black)),
onPressed: _handlePostComment,
child: const Center(
child: Text(
"Kirim",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
const SizedBox(
width: 13,
),
ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.black)),
onPressed: _handlePostComment,
child: const Center(
child: Text(
"Kirim",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
),
),
],
))
],
))
: const SizedBox.shrink(),
for (var komentar in widget.data.komentar)
for (var komentar in comments)
Column(
children: [
const SizedBox(
Expand Down

0 comments on commit c3d2fe8

Please sign in to comment.