59 lines
1.2 KiB
Dart
59 lines
1.2 KiB
Dart
import 'package:cubit_form/cubit_form.dart';
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:selfprivacy/logic/server_api.dart';
|
|
import 'package:selfprivacy/models/order.dart';
|
|
|
|
export 'package:provider/provider.dart';
|
|
|
|
part 'ordersstate.dart';
|
|
|
|
class ItemsCubit extends Cubit<OrdersState> {
|
|
ItemsCubit()
|
|
: super(
|
|
const OrdersState(
|
|
<Order>[],
|
|
false,
|
|
),
|
|
);
|
|
|
|
final ServerApi api = ServerApi();
|
|
|
|
Future<void> load() async {
|
|
refresh();
|
|
}
|
|
|
|
Future<void> refresh() async {
|
|
emit(state.copyWith(isLoading: true));
|
|
final resp = await api.getOrders();
|
|
final List<Order> items = resp.data;
|
|
if (items.isNotEmpty) {
|
|
emit(
|
|
OrdersState(
|
|
items,
|
|
false,
|
|
),
|
|
);
|
|
} else {
|
|
emit(state.copyWith(isLoading: false));
|
|
}
|
|
}
|
|
|
|
Future<void> create(final Order order) async {
|
|
await api.createOrder(order);
|
|
|
|
final List<Order> items = List<Order>.from(state.orders);
|
|
items.add(order);
|
|
emit(state.copyWith(orders: items));
|
|
}
|
|
|
|
@override
|
|
void clear() async {
|
|
emit(
|
|
const OrdersState(
|
|
<Order>[],
|
|
false,
|
|
),
|
|
);
|
|
}
|
|
}
|