46 lines
1.3 KiB
Dart
46 lines
1.3 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import '../models/work_order.dart';
|
|
import '../services/api_service.dart';
|
|
|
|
class WorkOrderState {
|
|
final List<WorkOrder> orders;
|
|
final bool isLoading;
|
|
final String? error;
|
|
|
|
const WorkOrderState({this.orders = const [], this.isLoading = false, this.error});
|
|
|
|
WorkOrderState copyWith({List<WorkOrder>? orders, bool? isLoading, String? error}) {
|
|
return WorkOrderState(
|
|
orders: orders ?? this.orders,
|
|
isLoading: isLoading ?? this.isLoading,
|
|
error: error,
|
|
);
|
|
}
|
|
}
|
|
|
|
class WorkOrderNotifier extends StateNotifier<WorkOrderState> {
|
|
WorkOrderNotifier() : super(const WorkOrderState());
|
|
|
|
Future<void> loadOrders(WorkOrderStatus status) async {
|
|
state = state.copyWith(isLoading: true, error: null);
|
|
try {
|
|
final orders = await apiService.getWorkOrders(status);
|
|
state = state.copyWith(orders: orders, isLoading: false);
|
|
} catch (e) {
|
|
state = state.copyWith(isLoading: false, error: e.toString());
|
|
}
|
|
}
|
|
|
|
Future<WorkOrder?> getDetail(String id) async {
|
|
try {
|
|
return await apiService.getWorkOrderDetail(id);
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
final workOrderProvider = StateNotifierProvider<WorkOrderNotifier, WorkOrderState>((ref) {
|
|
return WorkOrderNotifier();
|
|
});
|