Skip to content

Commit

Permalink
Merge pull request #40 from fga-eps-mds/feat#60/recuperar_conta
Browse files Browse the repository at this point in the history
Feat#60/recuperar conta
  • Loading branch information
GabrielCostaDeOliveira authored Dec 13, 2024
2 parents 5ff0185 + 291895f commit c6d4091
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/ui/login/view/LoginView.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class _LoginState extends State<_Login> {
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const RecoverAccount(),
builder: (context) => RecoverAccount(),
),
);
},
Expand Down
16 changes: 16 additions & 0 deletions lib/ui/recover_account/model/RecoverAccountRequest.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'dart:convert';

class RecoverAccountRequest {

final String email;

RecoverAccountRequest(this.email);

factory RecoverAccountRequest.fromJsonString(String jsonString) {
Map<String, dynamic> json = jsonDecode(jsonString);
return RecoverAccountRequest(
json['email'] as String,
);
}

}
17 changes: 17 additions & 0 deletions lib/ui/recover_account/service/RecoverAccountService.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:aranduapp/core/network/BaseApi.dart';
import 'package:aranduapp/ui/recover_account/model/RecoverAccountRequest.dart';

class RecoverAccountService {


static Future<void> forgetPassword(RecoverAccountRequest recoverAccountRequest) async {

await BaseApi.getInstance().post(
path: '/auth/forgot-password',
data: <String, dynamic> {
'email' : recoverAccountRequest.email,
}
);
}

}
76 changes: 73 additions & 3 deletions lib/ui/recover_account/view/RecoverAccount.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,82 @@
import 'package:aranduapp/core/log/Log.dart';
import 'package:aranduapp/ui/recover_account/viewModel/recoverAccountViewModel.dart';
import 'package:aranduapp/ui/shared/ErrorPopUp.dart';
import 'package:aranduapp/ui/shared/TextAndLink.dart';
import 'package:aranduapp/ui/shared/TextEmail.dart';
import 'package:aranduapp/ui/shared/TitleSlogan.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class RecoverAccount extends StatelessWidget{
class RecoverAccount extends StatelessWidget {
const RecoverAccount({super.key});

@override
Widget build(BuildContext context) {
// TODO: implement build
throw UnimplementedError();
return Scaffold(
body: ChangeNotifierProvider(
create: (context) => RecoverAccountViewModel(context),
builder: (context, child) {
return page(context);
}),
);
}
}

Widget page(BuildContext context) {
RecoverAccountViewModel viewModel =
Provider.of<RecoverAccountViewModel>(context);

return SingleChildScrollView(
child: Center(
child: Column(
children: <Widget>[
const SizedBox(height: 80),
const TitleSlogan(),
const Padding(
padding: EdgeInsets.only(top: 70, left: 20, right: 20),
child: Text(
'Coloque o seu e-mail no campo abaixo e clique em enviar. Logo em seguida, você receberá no seu e-mail as instruções para trocar a sua senha.',
),
),
Form(
key: viewModel.formKey,
child: Column(children: <Widget>[
TextEmail(
padding:
const EdgeInsets.only(top: 24, left: 20, right: 20),
controller: viewModel.emailController),
])),
Padding(
padding: const EdgeInsets.only(top: 80),
child: SizedBox(
width: 291,
height: 64,
child: ElevatedButton(
onPressed: () {
viewModel.forgetPassword().then((value) {
Log.d("Deu certo!");
}).catchError((e) => showDialog<Object>(
context: context,
builder: (BuildContext context) =>
ErrorPopUp(content: Text('$e')),
));
},
child: Consumer<RecoverAccountViewModel>(
builder: (context, value, child) => value.isLoading
? const CircularProgressIndicator(value: null)
: const Text('Enviar'),
)),
),
),
TextAndLink(
text: 'Já tem uma conta?',
link: 'Faça login',
action: () {
Navigator.of(context).pop();
},
)
],
),
),
);
}
52 changes: 52 additions & 0 deletions lib/ui/recover_account/viewModel/recoverAccountViewModel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:aranduapp/core/log/Log.dart';
import 'package:aranduapp/ui/recover_account/model/RecoverAccountRequest.dart';
import 'package:aranduapp/ui/recover_account/service/RecoverAccountService.dart';
import 'package:flutter/material.dart';

class RecoverAccountViewModel extends ChangeNotifier {
final BuildContext context;

final formKey;

final emailController;


bool isLoading;

RecoverAccountViewModel(this.context)
: formKey = GlobalKey<FormState>(),
emailController = TextEditingController(),
isLoading = false;



Future<void> forgetPassword() async {

// TODO use mutex to make this
if (isLoading){
return;
}

try {
isLoading = true;
super.notifyListeners();

if (!formKey.currentState!.validate()) {
throw Exception('Valores inválidos');
}

await RecoverAccountService.forgetPassword(RecoverAccountRequest(emailController.text));

} catch (e) {
rethrow;
} finally {
isLoading = false;
super.notifyListeners();
}





}
}

0 comments on commit c6d4091

Please sign in to comment.