337 lines
12 KiB
Dart
Raw Normal View History

2025-01-03 19:15:35 +01:00
import 'package:covas_mobile/classes/MyDrawer.dart';
2025-01-02 23:22:25 +01:00
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 'dart:convert';
import 'dart:io';
2025-01-03 19:15:35 +01:00
import '../classes/MyDrawer.dart';
2025-01-05 17:38:31 +01:00
import '../main.dart';
2025-01-02 23:22:25 +01:00
import '../classes/alert.dart';
import '../classes/eventAdded.dart';
import '../variable/globals.dart' as globals;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: EditProfile(),
);
}
}
class EditProfile extends StatefulWidget {
const EditProfile({super.key});
@override
_EditProfileState createState() => _EditProfileState();
}
class _EditProfileState extends State<EditProfile>
with ShowAlertDialog, ShowEventDialog {
2025-01-03 17:22:14 +01:00
TextEditingController inputUserName = TextEditingController();
2025-01-02 23:22:25 +01:00
2025-01-03 17:22:14 +01:00
TextEditingController inputName = TextEditingController();
2025-01-02 23:22:25 +01:00
2025-01-03 17:22:14 +01:00
TextEditingController inputFirstName = TextEditingController();
TextEditingController inputEmail = TextEditingController();
TextEditingController inputBirth = TextEditingController();
TextEditingController inputPassword = TextEditingController();
2025-01-03 19:15:35 +01:00
TextEditingController inputPasswordConfirmed = TextEditingController();
2025-01-02 23:22:25 +01:00
2025-01-03 17:22:14 +01:00
onTapFunctionDatePicker({required BuildContext context}) async {
DateTime initialDate = DateTime.parse(formatDate(inputBirth.text));
2025-01-02 23:22:25 +01:00
DateTime? pickedDate = await showDatePicker(
context: context,
2025-01-03 17:22:14 +01:00
firstDate: DateTime(1900),
initialDate: initialDate,
2025-01-02 23:22:25 +01:00
lastDate: DateTime(2104));
if (pickedDate == null) return;
2025-01-03 17:22:14 +01:00
inputBirth.text = DateFormat("dd/MM/yyyy").format(pickedDate);
2025-01-02 23:22:25 +01:00
}
convertNulltoEmptyString(var check) {
if (check == null) {
return "";
}
return check;
}
convertNulltoArray(List<String> check) {
if (check == null) {
return [];
}
return check;
}
String formatDate(String date) {
var splitedDate = date.split("/");
var day = splitedDate[0];
var month = splitedDate[1];
var year = splitedDate[2];
return "${year}-${month}-${day}";
}
2025-01-03 15:06:08 +01:00
Future<void> _updateProfile(BuildContext context) async {
2025-01-03 18:03:24 +01:00
var username = inputUserName.text;
var firstName = inputFirstName.text;
var name = inputName.text;
var email = inputEmail.text;
var password = inputPassword.text;
2025-01-03 19:15:35 +01:00
var confirmedPassword = inputPasswordConfirmed.text;
2025-01-03 18:03:24 +01:00
var birth = DateTime.parse(formatDate(inputBirth.text));
2025-01-03 19:15:35 +01:00
2025-01-05 21:02:39 +01:00
if ((password.isNotEmpty) && (confirmedPassword.isNotEmpty)) {
if (password != confirmedPassword) {
showAlertDialog(context, "Erreur", "Mot de passe different");
return;
}
2025-01-03 19:15:35 +01:00
}
2025-01-03 18:03:24 +01:00
var urlPut = Uri.parse("${globals.api}/users/me");
2025-01-03 19:15:35 +01:00
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
2025-01-05 17:38:31 +01:00
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,
2025-01-06 23:40:17 +01:00
'roles': '',
'birth': birth.toString()
2025-01-05 17:38:31 +01:00
}));
print(responsePut.statusCode);
if (responsePut.statusCode == 200) {
showEventDialog(context, "Votre utilisateur a été modifié");
2025-01-06 23:40:17 +01:00
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (_) => EditProfile()));
2025-01-05 17:38:31 +01:00
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()));
2025-01-02 23:22:25 +01:00
}
2025-01-05 17:38:31 +01:00
}
Future<void> _getInfoProfile() async {
var urlGet = Uri.parse("${globals.api}/users/me");
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"];
2025-01-05 17:53:14 +01:00
inputEmail.text = body["email"];
2025-01-05 17:38:31 +01:00
inputBirth.text =
DateFormat("dd/MM/yyyy").format(DateTime.parse(body["birth"]));
});
return;
}
2025-01-03 19:15:35 +01:00
2025-01-05 17:38:31 +01:00
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()));
}
2025-01-02 23:22:25 +01:00
}
@override
void initState() {
super.initState();
2025-01-05 17:38:31 +01:00
_getInfoProfile();
2025-01-02 23:22:25 +01:00
}
final _formKey = GlobalKey<FormState>();
String? _validateField(String? value) {
return value!.isEmpty ? 'Champ requis' : null;
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
2025-01-09 21:48:24 +01:00
title: Text("Update profile"),
2025-01-02 23:22:25 +01:00
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
2025-01-03 19:15:35 +01:00
drawer: MyDrawer(),
2025-01-02 23:22:25 +01:00
body: Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
2025-01-03 17:22:14 +01:00
Padding(
2025-01-05 17:38:31 +01:00
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
2025-01-03 17:22:14 +01:00
child: TextFormField(
controller: inputUserName,
validator: (value) => _validateField(value),
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Pseudo',
hintText: 'Modifier le pseudo'),
),
),
2025-01-02 23:22:25 +01:00
Padding(
2025-01-05 17:38:31 +01:00
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
2025-01-02 23:22:25 +01:00
child: TextFormField(
2025-01-03 19:15:35 +01:00
controller: inputPassword,
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Mot de passe',
hintText: 'Entrez le mot de passe'),
),
),
Padding(
2025-01-05 17:38:31 +01:00
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
2025-01-03 19:15:35 +01:00
child: TextFormField(
controller: inputPasswordConfirmed,
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
2025-01-05 17:53:14 +01:00
labelText: 'Confirmez le mot de passe',
2025-01-03 19:15:35 +01:00
hintText: 'Confirmez le mot de passe'),
),
),
Padding(
2025-01-05 17:38:31 +01:00
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
2025-01-03 19:15:35 +01:00
child: TextFormField(
2025-01-02 23:22:25 +01:00
controller: inputName,
validator: (value) => _validateField(value),
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Nom',
2025-01-03 17:22:14 +01:00
hintText: 'Modifier le nom'),
),
),
Padding(
2025-01-05 17:38:31 +01:00
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
2025-01-03 17:22:14 +01:00
child: TextFormField(
controller: inputFirstName,
validator: (value) => _validateField(value),
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Prénom',
hintText: 'Modifier le prénom'),
),
),
Padding(
2025-01-05 17:38:31 +01:00
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
2025-01-03 17:22:14 +01:00
child: TextFormField(
2025-01-05 17:53:14 +01:00
controller: inputEmail,
2025-01-03 17:22:14 +01:00
validator: (value) => _validateField(value),
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Email',
hintText: 'Modifier l\'adresse mail'),
2025-01-02 23:22:25 +01:00
),
),
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
child: TextFormField(
2025-01-03 17:22:14 +01:00
controller: inputBirth,
2025-01-02 23:22:25 +01:00
readOnly: true,
validator: (value) => _validateField(value),
decoration: InputDecoration(
border: OutlineInputBorder(),
2025-01-05 17:38:31 +01:00
labelText: 'Date de naissance',
2025-01-02 23:22:25 +01:00
hintText: 'Cliquez ici pour selectionner une date'),
2025-01-03 17:22:14 +01:00
onTap: () => onTapFunctionDatePicker(context: context)),
2025-01-02 23:22:25 +01:00
),
SizedBox(
height: 30,
),
Container(
height: 50,
width: 250,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(20)),
child: TextButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
2025-01-03 15:06:08 +01:00
_updateProfile(context);
2025-01-02 23:22:25 +01:00
}
},
child: Text(
2025-01-06 23:40:17 +01:00
'Modifier le profil',
2025-01-02 23:22:25 +01:00
style: TextStyle(color: Colors.white, fontSize: 25),
),
),
)
],
),
),
));
}
}