Skip to content

Commit

Permalink
Merge pull request #43 from LiteraLand-C07/collections
Browse files Browse the repository at this point in the history
fixed : bug auto login ke anonymous user
  • Loading branch information
malifalhakim committed Dec 18, 2023
2 parents 5f03795 + 47ef8a7 commit c603777
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 57 deletions.
102 changes: 53 additions & 49 deletions lib/BookLists/screens/book_lists.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:litera_land_mobile/BookLists/screens/detail_list.dart';
import 'package:litera_land_mobile/BookLists/screens/my_book_list.dart';
Expand All @@ -7,6 +9,7 @@ import 'package:litera_land_mobile/Main/widgets/left_drawer.dart';
import 'package:litera_land_mobile/BookLists/models/book_lists_models.dart';
import 'package:pbp_django_auth/pbp_django_auth.dart';
import 'package:provider/provider.dart';
import 'package:http/http.dart' as http;

class BookListsPage extends StatefulWidget {
const BookListsPage({Key? key}) : super(key: key);
Expand All @@ -17,12 +20,17 @@ class BookListsPage extends StatefulWidget {

class _BookListsPageState extends State<BookListsPage> {
Future<List<BookLists>> fetchBookLists() async {
final request = context.watch<CookieRequest>();
final response = await request.get(
var url = Uri.parse(
'https://literaland-c07-tk.pbp.cs.ui.ac.id/rankingBuku/get-book-list-json/');
var response = await http.get(
url,
headers: {"Content-Type": "application/json"},
);

var data = jsonDecode(utf8.decode(response.bodyBytes));

List<BookLists> listItem = [];
for (var d in response) {
for (var d in data) {
if (d != null) {
listItem.add(BookLists.fromJson(d));
}
Expand Down Expand Up @@ -64,47 +72,46 @@ class _BookListsPageState extends State<BookListsPage> {
bottomNavigationBar: const MyBottomNavigationBar(
selectedIndex: 1,
),
body: Column(
children: [

Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
final authProvider = context.read<CookieRequest>();
if (authProvider.loggedIn) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const MyBookListsPage(),
),
);
} else {
showLoginAlert(context);
}
},
child: const Text('Your List', style: TextStyle(color: Colors.black)),
),
ElevatedButton(
onPressed: () {
// Handle the second button press
body: Column(children: [
Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
final authProvider = context.read<CookieRequest>();
if (authProvider.loggedIn) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) =>
const BookListsPage()),
builder: (context) => const MyBookListsPage(),
),
);
},
child: const Text('Explore Others', style: TextStyle(color: Colors.black)),
),
],
),
} else {
showLoginAlert(context);
}
},
child: const Text('Your List',
style: TextStyle(color: Colors.black)),
),
ElevatedButton(
onPressed: () {
// Handle the second button press
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const BookListsPage()),
);
},
child: const Text('Explore Others',
style: TextStyle(color: Colors.black)),
),
],
),
Expanded(
child: FutureBuilder(
),
Expanded(
child: FutureBuilder(
future: fetchBookLists(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
Expand All @@ -115,7 +122,8 @@ class _BookListsPageState extends State<BookListsPage> {
children: [
Text(
"Tidak ada data produk.",
style: TextStyle(color: Color(0xff59A5D8), fontSize: 20),
style:
TextStyle(color: Color(0xff59A5D8), fontSize: 20),
),
SizedBox(height: 8),
],
Expand All @@ -133,8 +141,8 @@ class _BookListsPageState extends State<BookListsPage> {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
DetailPage(bookList: snapshot.data[index]),
builder: (context) => DetailPage(
bookList: snapshot.data[index]),
),
);
},
Expand All @@ -146,19 +154,15 @@ class _BookListsPageState extends State<BookListsPage> {
child: BookListsWidget(
bookList: snapshot.data[index]),
),
)
);
));
},
),
);
}
}
}),
)

]
),

)
]),
);
}
}
17 changes: 10 additions & 7 deletions lib/collections/screens/detail_book.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import 'package:litera_land_mobile/Main/widgets/bottom_navbar.dart';
import 'package:litera_land_mobile/collections/models/detail_book.dart';
import 'package:litera_land_mobile/collections/screens/mycollection.dart';
import 'package:litera_land_mobile/collections/widgets/card_detail.dart';
import 'package:pbp_django_auth/pbp_django_auth.dart';
import 'package:provider/provider.dart';
import 'package:http/http.dart' as http;

class BookDetailPage extends StatefulWidget {
Expand All @@ -25,13 +23,18 @@ class BookDetailPage extends StatefulWidget {

class _BookDetailPageState extends State<BookDetailPage> {
Future<List<dynamic>> fetchProduct() async {
final request = context.watch<CookieRequest>();
final response = await request.get(
var urlDetail = Uri.parse(
'https://literaland-c07-tk.pbp.cs.ui.ac.id/collections/get_detail_json/${widget.bookId}/');
var response = await http.get(
urlDetail,
headers: {"Content-Type": "application/json"},
);

var data = jsonDecode(utf8.decode(response.bodyBytes));

// melakukan konversi data json menjadi object Product
List<dynamic> listItem = [];
for (var d in response) {
for (var d in data) {
if (d != null) {
listItem.add(DetailBook.fromJson(d));
}
Expand All @@ -46,8 +49,8 @@ class _BookDetailPageState extends State<BookDetailPage> {
);

// melakukan decode response menjadi bentuk json
var data = jsonDecode(utf8.decode(response2.bodyBytes));
String item = data["items"][0]["volumeInfo"]["imageLinks"]["thumbnail"];
var data2 = jsonDecode(utf8.decode(response2.bodyBytes));
String item = data2["items"][0]["volumeInfo"]["imageLinks"]["thumbnail"];
item = item.replaceAll('zoom=1', 'zoom=6');

listItem.add(item);
Expand Down
2 changes: 1 addition & 1 deletion lib/collections/widgets/card_detail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class _DetailBookWidgetState extends State<DetailBookWidget> {
Widget build(BuildContext context) {
final request = context.watch<CookieRequest>();
return FutureBuilder(
future: fetchProduct(),
future: request.loggedIn ? fetchProduct() : Future.value(List.empty()),
builder: (context, AsyncSnapshot snapshot) {
return CustomScrollView(
slivers: [
Expand Down

0 comments on commit c603777

Please sign in to comment.