From da3659e84a6968a20907dd725ccce4e13e2db6d2 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 5 Jan 2025 17:38:31 +0100 Subject: [PATCH] add edit profile --- covas_mobile/lib/pages/EditProfile.dart | 142 ++++++++++++++++-------- 1 file changed, 98 insertions(+), 44 deletions(-) diff --git a/covas_mobile/lib/pages/EditProfile.dart b/covas_mobile/lib/pages/EditProfile.dart index a5bb462..79b09d3 100644 --- a/covas_mobile/lib/pages/EditProfile.dart +++ b/covas_mobile/lib/pages/EditProfile.dart @@ -9,6 +9,7 @@ import 'dart:io'; import '../classes/events.dart'; import '../classes/MyDrawer.dart'; +import '../main.dart'; import '../classes/alert.dart'; import '../classes/eventAdded.dart'; @@ -104,43 +105,92 @@ class _EditProfileState extends State SharedPreferences prefs = await SharedPreferences.getInstance(); var accessToken = prefs.getString("access_token") ?? ""; - var responsePut = await http.put(urlPut, - headers: { - HttpHeaders.cookieHeader: 'access_token=${accessToken}', - HttpHeaders.acceptHeader: 'application/json, text/plain, */*', - HttpHeaders.contentTypeHeader: 'application/json' - }, - body: jsonEncode({ - 'name': name, - 'username': username, - 'firstName': firstName, - 'password': password, - 'email': email, - 'birth': birth - })); - print(responsePut.statusCode); - if (responsePut.statusCode == 200) { - showEventDialog(context, "Votre utilisateur a été modifié"); - return; + if (accessToken.isNotEmpty) { + var responsePut = await http.put(urlPut, + headers: { + HttpHeaders.cookieHeader: 'access_token=${accessToken}', + HttpHeaders.acceptHeader: 'application/json, text/plain, */*', + HttpHeaders.contentTypeHeader: 'application/json' + }, + body: jsonEncode({ + 'name': name, + 'username': username, + 'firstName': firstName, + 'password': password, + 'email': email, + 'birth': birth + })); + print(responsePut.statusCode); + if (responsePut.statusCode == 200) { + 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); + } else { + Navigator.pushReplacement( + context, MaterialPageRoute(builder: (_) => LoginDemo())); } + } - 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", - }; + Future _getInfoProfile() async { + var urlGet = Uri.parse("${globals.api}/users/me"); - final text = errorMessages[responsePut.statusCode] ?? - "Problème d'authentification inconnu"; - showAlertDialog(context, "Erreur serveur", text); + SharedPreferences prefs = await SharedPreferences.getInstance(); + var accessToken = prefs.getString("access_token") ?? ""; + if (accessToken.isNotEmpty) { + var responseGet = await http.get(urlGet, headers: { + HttpHeaders.cookieHeader: 'access_token=${accessToken}', + HttpHeaders.acceptHeader: 'application/json, text/plain, */*', + HttpHeaders.contentTypeHeader: 'application/json' + }); + print(responseGet.statusCode); + if (responseGet.statusCode == 200) { + var body = json.decode(utf8.decode(responseGet.bodyBytes)); + setState(() { + inputName.text = body["name"]; + inputFirstName.text = body["firstName"]; + inputUserName.text = body["username"]; + inputEmail = body["email"]; + inputBirth.text = + DateFormat("dd/MM/yyyy").format(DateTime.parse(body["birth"])); + }); + 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[responseGet.statusCode] ?? + "Problème d'authentification inconnu"; + showAlertDialog(context, "Erreur serveur", text); + } else { + Navigator.pushReplacement( + context, MaterialPageRoute(builder: (_) => LoginDemo())); + } } @override void initState() { super.initState(); + _getInfoProfile(); } final _formKey = GlobalKey(); @@ -164,8 +214,9 @@ class _EditProfileState extends State child: Column( children: [ Padding( - //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), - padding: EdgeInsets.symmetric(horizontal: 15), + padding: const EdgeInsets.only( + left: 15.0, right: 15.0, top: 15, bottom: 0), + //padding: EdgeInsets.symmetric(horizontal: 15), child: TextFormField( controller: inputUserName, validator: (value) => _validateField(value), @@ -176,12 +227,12 @@ class _EditProfileState extends State ), ), Padding( - //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), - padding: EdgeInsets.symmetric(horizontal: 15), + padding: const EdgeInsets.only( + left: 15.0, right: 15.0, top: 15, 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', @@ -189,12 +240,12 @@ class _EditProfileState extends State ), ), Padding( - //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), - padding: EdgeInsets.symmetric(horizontal: 15), + padding: const EdgeInsets.only( + left: 15.0, right: 15.0, top: 15, 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', @@ -202,8 +253,9 @@ class _EditProfileState extends State ), ), Padding( - //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), - padding: EdgeInsets.symmetric(horizontal: 15), + padding: const EdgeInsets.only( + left: 15.0, right: 15.0, top: 15, bottom: 0), + //padding: EdgeInsets.symmetric(horizontal: 15), child: TextFormField( controller: inputName, validator: (value) => _validateField(value), @@ -214,8 +266,9 @@ class _EditProfileState extends State ), ), Padding( - //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), - padding: EdgeInsets.symmetric(horizontal: 15), + padding: const EdgeInsets.only( + left: 15.0, right: 15.0, top: 15, bottom: 0), + //padding: EdgeInsets.symmetric(horizontal: 15), child: TextFormField( controller: inputFirstName, validator: (value) => _validateField(value), @@ -226,8 +279,9 @@ class _EditProfileState extends State ), ), Padding( - //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), - padding: EdgeInsets.symmetric(horizontal: 15), + padding: const EdgeInsets.only( + left: 15.0, right: 15.0, top: 15, bottom: 0), + //padding: EdgeInsets.symmetric(horizontal: 15), child: TextFormField( controller: inputName, validator: (value) => _validateField(value), @@ -247,7 +301,7 @@ class _EditProfileState extends State validator: (value) => _validateField(value), decoration: InputDecoration( border: OutlineInputBorder(), - labelText: 'Date de nassance', + labelText: 'Date de naissance', hintText: 'Cliquez ici pour selectionner une date'), onTap: () => onTapFunctionDatePicker(context: context)), ),