Commit eac237ca authored by Ilham Maulana's avatar Ilham Maulana 💻

feat: book list loading

parent 057213c1
...@@ -12,8 +12,15 @@ class BookProvider with ChangeNotifier { ...@@ -12,8 +12,15 @@ class BookProvider with ChangeNotifier {
String? searchKeyword; String? searchKeyword;
String? filterByCategory; String? filterByCategory;
bool isLoading = false;
void setLoading(bool value) {
isLoading = value;
}
Future<void> getBooks() async { Future<void> getBooks() async {
try { try {
setLoading(true);
String url = '$baseUrl/books'; String url = '$baseUrl/books';
if (filterByCategory != null) { if (filterByCategory != null) {
url += '?category__name=$filterByCategory'; url += '?category__name=$filterByCategory';
...@@ -34,6 +41,7 @@ class BookProvider with ChangeNotifier { ...@@ -34,6 +41,7 @@ class BookProvider with ChangeNotifier {
debugPrint("Error: Fetch books failed, $code"); debugPrint("Error: Fetch books failed, $code");
} }
setLoading(false);
notifyListeners(); notifyListeners();
} catch (error) { } catch (error) {
debugPrint("Error: Fetch books failed, $error"); debugPrint("Error: Fetch books failed, $error");
......
...@@ -25,7 +25,7 @@ class _BookList extends State<BookList> { ...@@ -25,7 +25,7 @@ class _BookList extends State<BookList> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Consumer<BookProvider>( return Consumer<BookProvider>(
builder: (context, bookProvider, child) { builder: (context, bookProvider, child) {
if (bookProvider.books != null) { if (!bookProvider.isLoading) {
final Iterable<Book> books = bookProvider.books!.map((book) { final Iterable<Book> books = bookProvider.books!.map((book) {
if (book["category_detail"] != null) { if (book["category_detail"] != null) {
final Category category = Category.fromJson( final Category category = Category.fromJson(
......
...@@ -27,9 +27,14 @@ class _SearchForm extends State<SearchForm> { ...@@ -27,9 +27,14 @@ class _SearchForm extends State<SearchForm> {
hintText: "Enter keywords...", hintText: "Enter keywords...",
elevation: WidgetStateProperty.all(0), elevation: WidgetStateProperty.all(0),
onChanged: (value) { onChanged: (value) {
Provider.of<BookProvider>(context, listen: false) Future.delayed(
.setSearchKeyword(value); Duration.zero,
Provider.of<BookProvider>(context, listen: false).getBooks(); () {
Provider.of<BookProvider>(context, listen: false)
.setSearchKeyword(value);
Provider.of<BookProvider>(context, listen: false).getBooks();
},
);
}, },
leading: const Icon(Icons.search), leading: const Icon(Icons.search),
), ),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment