Skip to content

Commit

Permalink
Merge pull request #25 from fga-eps-mds/feat#59/Criação-View-Cadastro
Browse files Browse the repository at this point in the history
Página de Registro em MVVM
  • Loading branch information
GabrielCostaDeOliveira authored Dec 6, 2024
2 parents 6447a50 + 239092b commit 936e5ff
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/ui/register_account/model/RegisterRequest.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'dart:convert';
class RegisterRequest {
final String email;
final String firstName;
final String lastName;
final String password;
final String confPassword;
RegisterRequest({
required this.email,
required this.firstName,
required this.lastName,
required this.password,
required this.confPassword,
});
Map<String, dynamic> toJson() {
return {
'email': email,
'firstName': firstName,
'lastName': lastName,
'password': password,
'confPassword': confPassword,
};
}
factory RegisterRequest.fromJsonString(String jsonString) {
final json = jsonDecode(jsonString);
return RegisterRequest(
email: json['email'],
firstName: json['firstName'],
lastName: json['lastName'],
password: json['password'],
confPassword: json['confPassword'],
);
}
}
16 changes: 16 additions & 0 deletions lib/ui/register_account/model/RegisterResponse.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'dart:convert';
class RegisterResponse {
final String authToken;
final String refreshToken;
RegisterResponse({
required this.authToken,
required this.refreshToken,
});
factory RegisterResponse.fromJsonString(String jsonString) {
final json = jsonDecode(jsonString);
return RegisterResponse(
authToken: json['auth_token'],
refreshToken: json['refresh_token'],
);
}
}
14 changes: 14 additions & 0 deletions lib/ui/register_account/service/RegisterService.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:dio/dio.dart';
import '../model/RegisterRequest.dart';
class RegisterService {
static final Dio _dio = Dio();
static Future<void> register(RegisterRequest request) async {
final response = await _dio.post(
'https://example.com/register',
data: request.toJson(),
);
if (response.statusCode != 200) {
throw Exception('Erro ao criar conta: ${response.data['message']}');
}
}
}
46 changes: 46 additions & 0 deletions lib/ui/register_account/viewModel/RegisterViewModel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:flutter/material.dart';
import '../model/RegisterRequest.dart';
import '../service/RegisterService.dart';
class RegisterAccountViewModel extends ChangeNotifier {
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
final TextEditingController emailController = TextEditingController();
final TextEditingController firstNameController = TextEditingController();
final TextEditingController lastNameController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
final TextEditingController confPasswordController = TextEditingController();
bool isLoading = false;
bool isTermsAccepted = false; // Adicionado
// Método para alternar a aceitação dos termos
void toggleTermsAccepted(bool value) {
isTermsAccepted = value;
notifyListeners();
}
Future<void> register() async {
if (isLoading) return;
// Valida se os termos foram aceitos
if (!isTermsAccepted) {
throw Exception('Você deve aceitar os termos de privacidade e políticas de uso.');
}
try {
isLoading = true;
notifyListeners();
// Valida os campos do formulário
if (!formKey.currentState!.validate()) {
throw Exception('Por favor, preencha todos os campos corretamente');
}
// Criação do objeto de requisição
final request = RegisterRequest(
email: emailController.text,
firstName: firstNameController.text,
lastName: lastNameController.text,
password: passwordController.text,
confPassword: confPasswordController.text,
);
// Chamada do serviço de registro
await RegisterService.register(request);
} finally {
isLoading = false;
notifyListeners();
}
}
}

0 comments on commit 936e5ff

Please sign in to comment.