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

feat: book list loading

parent 057213c1
......@@ -12,8 +12,15 @@ class BookProvider with ChangeNotifier {
String? searchKeyword;
String? filterByCategory;
bool isLoading = false;
void setLoading(bool value) {
isLoading = value;
}
Future<void> getBooks() async {
try {
setLoading(true);
String url = '$baseUrl/books';
if (filterByCategory != null) {
url += '?category__name=$filterByCategory';
......@@ -34,6 +41,7 @@ class BookProvider with ChangeNotifier {
debugPrint("Error: Fetch books failed, $code");
}
setLoading(false);
notifyListeners();
} catch (error) {
debugPrint("Error: Fetch books failed, $error");
......
......@@ -25,7 +25,7 @@ class _BookList extends State<BookList> {
Widget build(BuildContext context) {
return Consumer<BookProvider>(
builder: (context, bookProvider, child) {
if (bookProvider.books != null) {
if (!bookProvider.isLoading) {
final Iterable<Book> books = bookProvider.books!.map((book) {
if (book["category_detail"] != null) {
final Category category = Category.fromJson(
......
......@@ -27,10 +27,15 @@ class _SearchForm extends State<SearchForm> {
hintText: "Enter keywords...",
elevation: WidgetStateProperty.all(0),
onChanged: (value) {
Future.delayed(
Duration.zero,
() {
Provider.of<BookProvider>(context, listen: false)
.setSearchKeyword(value);
Provider.of<BookProvider>(context, listen: false).getBooks();
},
);
},
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