mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 08:15:16 +00:00
Create account form is working
This commit is contained in:
@ -1,6 +1,10 @@
|
||||
import 'package:comunic/helpers/account_helper.dart';
|
||||
import 'package:comunic/models/config.dart';
|
||||
import 'package:comunic/models/new_account.dart';
|
||||
import 'package:comunic/utils/input_utils.dart';
|
||||
import 'package:comunic/utils/intl_utils.dart';
|
||||
import 'package:comunic/utils/ui_utils.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
@ -34,6 +38,9 @@ class __CreateAccountRouteBodyState extends State<_CreateAccountRouteBody> {
|
||||
final _verifyPasswordController = TextEditingController();
|
||||
bool _acceptedTOS = false;
|
||||
|
||||
bool _isCreating = false;
|
||||
CreateAccountResult _createAccountResult;
|
||||
|
||||
bool _showErrors = false;
|
||||
|
||||
bool get _isFirstNameValid => _firstNameController.text.length > 3;
|
||||
@ -55,12 +62,30 @@ class __CreateAccountRouteBodyState extends State<_CreateAccountRouteBody> {
|
||||
_isPasswordConfirmationValid &&
|
||||
_acceptedTOS;
|
||||
|
||||
String get errorMessage => _createAccountResult ==
|
||||
CreateAccountResult.ERROR_EXISTING_EMAIL
|
||||
? tr("An account is already associated to this email address!")
|
||||
: _createAccountResult == CreateAccountResult.ERROR_TOO_MANY_REQUESTS
|
||||
? tr(
|
||||
"Too many accounts have been created from this IP address for now. Please try again later.")
|
||||
: tr(
|
||||
"An error occured while creating your account. Please try again.");
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_isCreating)
|
||||
return Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: ListView(
|
||||
children: <Widget>[
|
||||
_createAccountResult != null
|
||||
? buildErrorCard(errorMessage)
|
||||
: Container(),
|
||||
|
||||
// First name
|
||||
_InputEntry(
|
||||
controller: _firstNameController,
|
||||
@ -119,9 +144,9 @@ class __CreateAccountRouteBodyState extends State<_CreateAccountRouteBody> {
|
||||
: null,
|
||||
),
|
||||
|
||||
// TOSs
|
||||
// TOS
|
||||
CheckboxListTile(
|
||||
title: Text("I have read and accepted the Term Of Service."),
|
||||
title: Text(tr("I have read and accepted the Terms Of Service.")),
|
||||
value: _acceptedTOS,
|
||||
onChanged: (b) {
|
||||
_acceptedTOS = b;
|
||||
@ -129,7 +154,7 @@ class __CreateAccountRouteBodyState extends State<_CreateAccountRouteBody> {
|
||||
},
|
||||
subtitle: _showErrors && !_acceptedTOS
|
||||
? Text(
|
||||
tr("You must accept the Term Of Service to continue."),
|
||||
tr("You must accept the Terms Of Service to continue."),
|
||||
style: TextStyle(color: Colors.redAccent),
|
||||
)
|
||||
: null,
|
||||
@ -160,16 +185,73 @@ class __CreateAccountRouteBodyState extends State<_CreateAccountRouteBody> {
|
||||
});
|
||||
}
|
||||
|
||||
void _submitForm() {
|
||||
void _submitForm() async {
|
||||
if (!_isFormValid) {
|
||||
_showErrors = true;
|
||||
_updateUI();
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isCreating = true;
|
||||
});
|
||||
|
||||
final result = await AccountHelper().createAccount(NewAccount(
|
||||
firstName: _firstNameController.text,
|
||||
lastName: _lastNameController.text,
|
||||
email: _emailController.text,
|
||||
password: _passwordController.text,
|
||||
));
|
||||
|
||||
setState(() {
|
||||
_isCreating = false;
|
||||
});
|
||||
|
||||
if (result != CreateAccountResult.SUCCESS) {
|
||||
_createAccountResult = result;
|
||||
_showCreateAccountError();
|
||||
_updateUI();
|
||||
return;
|
||||
}
|
||||
|
||||
_accountCreated();
|
||||
}
|
||||
|
||||
void _openTOS() {
|
||||
launch(config().termOfServicesURL);
|
||||
launch(config().termsOfServicesURL);
|
||||
}
|
||||
|
||||
void _showCreateAccountError() async {
|
||||
await showCupertinoDialog(
|
||||
context: context,
|
||||
builder: (c) => CupertinoAlertDialog(
|
||||
title: Text(tr("Error while creating your account")),
|
||||
content: Text(errorMessage),
|
||||
actions: <Widget>[
|
||||
CupertinoButton(
|
||||
child: Text(tr("Ok").toUpperCase()),
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _accountCreated() async {
|
||||
await showCupertinoDialog(
|
||||
context: context,
|
||||
builder: (c) => CupertinoAlertDialog(
|
||||
title: Text(tr("Account created")),
|
||||
content: Text(tr(
|
||||
"Your account has been successfully created. You can now login to start to use it.")),
|
||||
actions: <Widget>[
|
||||
CupertinoButton(
|
||||
child: Text(tr("Login")), onPressed: () => Navigator.of(c).pop())
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user