mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2024-08-21 04:21:51 +00:00
53 lines
1.3 KiB
Dart
53 lines
1.3 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:cubit_form/cubit_form.dart';
|
|
import 'package:selfprivacy/logic/api_maps/cloud_flare.dart';
|
|
import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
|
|
import 'package:selfprivacy/logic/cubit/forms/validations/validations.dart';
|
|
|
|
class CloudFlareFormCubit extends FormCubit {
|
|
CloudflareApi apiClient = CloudflareApi();
|
|
|
|
CloudFlareFormCubit(this.initializingCubit) {
|
|
var regExp = RegExp(r"\s+|[!$%^&*()_@+|~=`{}\[\]:<>?,.\/]");
|
|
apiKey = FieldCubit(
|
|
initalValue: '',
|
|
validations: [
|
|
RequiredStringValidation('required'),
|
|
ValidationModel<String>(
|
|
(s) => regExp.hasMatch(s), 'invalid key format'),
|
|
LegnthStringValidationWithLenghShowing(40, 'length is [] shoud be 40')
|
|
],
|
|
);
|
|
|
|
super.setFields([apiKey]);
|
|
}
|
|
|
|
@override
|
|
FutureOr<void> onSubmit() async {
|
|
initializingCubit.setCloudFlare(apiKey.state.value);
|
|
}
|
|
|
|
final AppConfigCubit initializingCubit;
|
|
|
|
FieldCubit<String> apiKey;
|
|
|
|
@override
|
|
FutureOr<bool> asyncValidation() async {
|
|
var isKeyValid = await apiClient.isValid(apiKey.state.value);
|
|
|
|
if (!isKeyValid) {
|
|
apiKey.setError('bad key');
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
Future<void> close() async {
|
|
apiClient.close();
|
|
|
|
return super.close();
|
|
}
|
|
}
|