From 82c31acf99c52c8b0255026da4080226b76e7028 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 3 Jan 2025 19:15:35 +0100 Subject: [PATCH] editprofile complete --- covas_mobile/lib/pages/EditProfile.dart | 103 +++++++++++++----------- 1 file changed, 54 insertions(+), 49 deletions(-) diff --git a/covas_mobile/lib/pages/EditProfile.dart b/covas_mobile/lib/pages/EditProfile.dart index 786acfd..a5bb462 100644 --- a/covas_mobile/lib/pages/EditProfile.dart +++ b/covas_mobile/lib/pages/EditProfile.dart @@ -1,16 +1,14 @@ -import 'package:covas_mobile/pages/CameraEdit.dart'; +import 'package:covas_mobile/classes/MyDrawer.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; import 'package:intl/intl.dart'; -import 'package:flutter_dotenv/flutter_dotenv.dart'; -import 'package:textfield_tags/textfield_tags.dart'; import 'dart:convert'; import 'dart:io'; -import 'dart:typed_data'; import '../classes/events.dart'; +import '../classes/MyDrawer.dart'; import '../classes/alert.dart'; import '../classes/eventAdded.dart'; @@ -50,6 +48,7 @@ class _EditProfileState extends State TextEditingController inputEmail = TextEditingController(); TextEditingController inputBirth = TextEditingController(); TextEditingController inputPassword = TextEditingController(); + TextEditingController inputPasswordConfirmed = TextEditingController(); onTapFunctionDatePicker({required BuildContext context}) async { DateTime initialDate = DateTime.parse(formatDate(inputBirth.text)); @@ -88,18 +87,23 @@ class _EditProfileState extends State } Future _updateProfile(BuildContext context) async { - SharedPreferences prefs = await SharedPreferences.getInstance(); - var accessToken = prefs.getString("access_token") ?? ""; - var username = inputUserName.text; var firstName = inputFirstName.text; var name = inputName.text; var email = inputEmail.text; var password = inputPassword.text; - + var confirmedPassword = inputPasswordConfirmed.text; var birth = DateTime.parse(formatDate(inputBirth.text)); + + if (password != confirmedPassword) { + showAlertDialog(context, "Erreur", "Mot de passe different"); + return; + } + var urlPut = Uri.parse("${globals.api}/users/me"); + SharedPreferences prefs = await SharedPreferences.getInstance(); + var accessToken = prefs.getString("access_token") ?? ""; var responsePut = await http.put(urlPut, headers: { HttpHeaders.cookieHeader: 'access_token=${accessToken}', @@ -116,48 +120,22 @@ class _EditProfileState extends State })); print(responsePut.statusCode); if (responsePut.statusCode == 200) { - showEventDialog(context, " Votre utiliseteur modifie"); - } else { - var text = ""; - switch (responsePut.statusCode) { - case 400: - { - text = "Requête mal construite"; - } - break; - case 406: - { - text = "Mot de passe incorrect"; - } - break; - case 404: - { - text = "Utilisateur inconnu"; - } - break; - case 403: - { - text = "Utilisateur desactive"; - } - break; - case 410: - { - text = "Token invalide"; - } - break; - case 500: - { - text = "Probleme interne du serveur"; - } - break; - default: - { - text = "Probleme d'authentification inconnu"; - } - break; - } - showAlertDialog(context, "Erreur serveur", text); + showEventDialog(context, "Votre utilisateur a été modifié"); + return; } + + final errorMessages = { + 400: "Requête mal construite", + 406: "Mot de passe incorrect", + 404: "Utilisateur inconnu", + 403: "Utilisateur désactivé", + 410: "Token invalide", + 500: "Problème interne du serveur", + }; + + final text = errorMessages[responsePut.statusCode] ?? + "Problème d'authentification inconnu"; + showAlertDialog(context, "Erreur serveur", text); } @override @@ -179,6 +157,7 @@ class _EditProfileState extends State backgroundColor: Colors.blue, foregroundColor: Colors.white, ), + drawer: MyDrawer(), body: Form( key: _formKey, child: SingleChildScrollView( @@ -196,6 +175,32 @@ class _EditProfileState extends State hintText: 'Modifier le pseudo'), ), ), + Padding( + //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), + padding: EdgeInsets.symmetric(horizontal: 15), + child: TextFormField( + controller: inputPassword, + obscureText: true, + validator: (value) => _validateField(value), + decoration: InputDecoration( + border: OutlineInputBorder(), + labelText: 'Mot de passe', + hintText: 'Entrez le mot de passe'), + ), + ), + Padding( + //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), + padding: EdgeInsets.symmetric(horizontal: 15), + child: TextFormField( + controller: inputPasswordConfirmed, + obscureText: true, + validator: (value) => _validateField(value), + decoration: InputDecoration( + border: OutlineInputBorder(), + labelText: 'Mot de passe', + hintText: 'Confirmez le mot de passe'), + ), + ), Padding( //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), padding: EdgeInsets.symmetric(horizontal: 15),