feat: complete leave approval MVP
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import 'package:aioa_mobile/core/auth/authenticated_http_client.dart';
|
||||
import 'package:aioa_mobile/core/config/runtime_config.dart';
|
||||
import 'package:aioa_mobile/features/tasks/data/approval_task_repository.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
final approvalTaskRepositoryProvider = Provider<ApprovalTaskRepository>(
|
||||
(ref) => ApprovalTaskRepository(
|
||||
client: ref.watch(authenticatedHttpClientProvider),
|
||||
baseUrl: RuntimeConfig.apiBaseUrl,
|
||||
),
|
||||
);
|
||||
|
||||
final approvalTaskProvider =
|
||||
AsyncNotifierProvider<ApprovalTaskController, List<ApprovalTaskItem>>(
|
||||
ApprovalTaskController.new,
|
||||
);
|
||||
|
||||
class ApprovalTaskController extends AsyncNotifier<List<ApprovalTaskItem>> {
|
||||
@override
|
||||
Future<List<ApprovalTaskItem>> build() =>
|
||||
ref.read(approvalTaskRepositoryProvider).list();
|
||||
|
||||
Future<void> refresh() async {
|
||||
state = const AsyncLoading();
|
||||
state = await AsyncValue.guard(
|
||||
() => ref.read(approvalTaskRepositoryProvider).list(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<String?> decide({
|
||||
required ApprovalTaskItem task,
|
||||
required bool approved,
|
||||
String? comment,
|
||||
}) async {
|
||||
try {
|
||||
await ref
|
||||
.read(approvalTaskRepositoryProvider)
|
||||
.decide(task: task, approved: approved, comment: comment);
|
||||
state = AsyncData([
|
||||
for (final item in state.value ?? const <ApprovalTaskItem>[])
|
||||
if (item.id != task.id) item,
|
||||
]);
|
||||
return null;
|
||||
} on ApprovalTaskException catch (error) {
|
||||
return error.message;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user