feat: complete leave approval MVP
This commit is contained in:
@@ -1,21 +1,47 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:aioa_mobile/core/forms/schema/form_schema.dart';
|
||||
import 'package:aioa_mobile/core/forms/schema/form_validator.dart';
|
||||
import 'package:aioa_mobile/features/form/domain/leave_form_definition.dart';
|
||||
import 'package:aioa_mobile/features/form/data/leave_local_draft_repository.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
final leaveLocalDraftRepositoryProvider = Provider<LeaveLocalDraftRepository>(
|
||||
(ref) => LeaveLocalDraftRepository(),
|
||||
);
|
||||
|
||||
final leaveDraftProvider =
|
||||
NotifierProvider<LeaveDraftController, DynamicFormState>(
|
||||
LeaveDraftController.new,
|
||||
);
|
||||
|
||||
class LeaveDraftController extends Notifier<DynamicFormState> {
|
||||
late final LeaveLocalDraftRepository _repository;
|
||||
|
||||
@override
|
||||
DynamicFormState build() => const DynamicFormState();
|
||||
DynamicFormState build() {
|
||||
_repository = ref.watch(leaveLocalDraftRepositoryProvider);
|
||||
unawaited(_restore());
|
||||
return const DynamicFormState();
|
||||
}
|
||||
|
||||
Future<void> _restore() async {
|
||||
final restored = await _repository.load();
|
||||
if (restored == null || state.values.isNotEmpty) return;
|
||||
state = DynamicFormState(
|
||||
values: restored.values,
|
||||
restoredAt: restored.savedAt,
|
||||
);
|
||||
}
|
||||
|
||||
void setValue(String field, Object? value) {
|
||||
final values = {...state.values, field: value};
|
||||
final errors = {...state.errors}..remove(field);
|
||||
state = state.copyWith(values: values, errors: errors);
|
||||
state = state.copyWith(
|
||||
values: values,
|
||||
errors: errors,
|
||||
clearRestoredAt: true,
|
||||
);
|
||||
unawaited(_repository.save(values));
|
||||
}
|
||||
|
||||
void applyAiSuggestion() {
|
||||
@@ -36,13 +62,22 @@ class LeaveDraftController extends Notifier<DynamicFormState> {
|
||||
'reason': '办理个人事务,已提前完成工作交接。',
|
||||
},
|
||||
);
|
||||
unawaited(_repository.save(state.values));
|
||||
}
|
||||
|
||||
bool validate() {
|
||||
final errors = validateDynamicForm(
|
||||
leaveFormDefinition.dataSchema,
|
||||
state.values,
|
||||
);
|
||||
void applySuggestion(Map<String, Object?> suggestion) {
|
||||
final values = {...state.values, ...suggestion};
|
||||
state = DynamicFormState(values: values);
|
||||
unawaited(_repository.save(values));
|
||||
}
|
||||
|
||||
Future<void> clear() async {
|
||||
await _repository.clear();
|
||||
state = const DynamicFormState();
|
||||
}
|
||||
|
||||
bool validate(JsonFormSchema schema) {
|
||||
final errors = validateDynamicForm(schema, state.values);
|
||||
final startsAt = DateTime.tryParse(
|
||||
state.values['startsAt'] as String? ?? '',
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user