update function update

This commit is contained in:
Valentin CZERYBA 2025-01-03 18:03:24 +01:00
parent 3615c1f476
commit f84f513e67

View File

@ -88,113 +88,35 @@ class _EditProfileState extends State<EditProfile>
} }
Future<void> _updateProfile(BuildContext context) async { Future<void> _updateProfile(BuildContext context) async {
var name = inputName.text;
var place = inputGeo.text;
var description = inputDesc.text;
var startDateFormat = formatDate(startDatepicker.text);
//DateTime startDateCompare = DateTime.parse(startDateFormat);
DateTime dateNow = DateTime.now();
var endDateFormat = formatDate(endDatepicker.text);
var startDate =
"${startDateFormat}T${startTimepicker.text.replaceAll('-', ':')}";
var endDate = "${endDateFormat}T${endTimepicker.text.replaceAll('-', ':')}";
DateTime startDateCompare = DateTime.parse(startDate);
if (startDateCompare.isAfter(dateNow)) {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? ""; var accessToken = prefs.getString("access_token") ?? "";
if (accessToken.isNotEmpty) { var username = inputUserName.text;
try { var firstName = inputFirstName.text;
await dotenv.load(); var name = inputName.text;
final ApiTokenGoogle = dotenv.env['PLACE_API_KEY'] ?? ''; var email = inputEmail.text;
// Searchbox API for geocoding the place (No session token) var password = inputPassword.text;
final searchboxUrl = Uri.parse(
'https://maps.googleapis.com/maps/api/place/textsearch/json?query=${place}&key=${ApiTokenGoogle}');
// Perform the request var birth = DateTime.parse(formatDate(inputBirth.text));
final searchboxResponse = await http.get(searchboxUrl); var urlPut = Uri.parse("${globals.api}/users/me");
if (searchboxResponse.statusCode == 200) {
final data = json.decode(searchboxResponse.body);
print("data : ${data}");
if (data['results'].isNotEmpty) {
place = data['results'][0]['formatted_address'];
final coordinates = data['results'][0]['geometry']['location'];
final longitude = coordinates["lng"]; // Longitude
final latitude = coordinates["lat"]; // Latitude
var urlGet = Uri.parse(
"${globals.api}/events/search?item=${name}&date_event=${startDate}&min_lat=$latitude&max_lat=$latitude"
"&min_lon=$longitude&max_lon=$longitude");
var responseGet = await http.get(urlGet, headers: {
HttpHeaders.cookieHeader: 'access_token=${accessToken}'
});
if (responseGet.statusCode == 200) {
var events = jsonDecode(utf8.decode(responseGet.bodyBytes));
if (events.length > 0) {
if (events[0]["id"] != widget.events!.id) {
showAlertDialog(
context, "Info evenement", "Evenement deja existant");
}
return;
}
}
if (widget.imgPath.isNotEmpty) {
final params = {
'expiration': '15552000',
'key': dotenv.env["IMGBB_API_KEY"],
};
print("Post Img");
final urlPost = Uri.parse('https://api.imgbb.com/1/upload')
.replace(queryParameters: params);
File image = File(widget.imgPath);
Uint8List _bytes = await image.readAsBytes();
String _base64String = base64.encode(_bytes);
final req = http.MultipartRequest('POST', urlPost)
..fields['image'] = _base64String;
final stream = await req.send();
final res = await http.Response.fromStream(stream);
final status = res.statusCode;
print("code status imgbb ${status}");
if (status != 200) {
showAlertDialog(context, "Erreur image", "Image non posté");
return;
}
var body = json.decode(utf8.decode(res.bodyBytes));
imgUrl = body["data"]["url"];
}
var urlPut =
Uri.parse("${globals.api}/events/${widget.events!.id}");
var responsePut = await http.put(urlPut, var responsePut = await http.put(urlPut,
headers: { headers: {
HttpHeaders.cookieHeader: 'access_token=${accessToken}', HttpHeaders.cookieHeader: 'access_token=${accessToken}',
HttpHeaders.acceptHeader: HttpHeaders.acceptHeader: 'application/json, text/plain, */*',
'application/json, text/plain, */*',
HttpHeaders.contentTypeHeader: 'application/json' HttpHeaders.contentTypeHeader: 'application/json'
}, },
body: jsonEncode({ body: jsonEncode({
'name': name, 'name': name,
'place': place, 'username': username,
'start_date': startDate, 'firstName': firstName,
'end_date': endDate, 'password': password,
'organizers': organizers, 'email': email,
'latitude': latitude, 'birth': birth
'longitude': longitude,
'description': description,
"imgUrl": imgUrl,
"tags": tags
})); }));
print(responsePut.statusCode); print(responsePut.statusCode);
if ((responsePut.statusCode == 200) || if (responsePut.statusCode == 200) {
(responsePut.statusCode == 201)) { showEventDialog(context, " Votre utiliseteur modifie");
showEventDialog(context, "Evenement ${name} modifie");
} else { } else {
var text = ""; var text = "";
switch (responsePut.statusCode) { switch (responsePut.statusCode) {
@ -236,22 +158,6 @@ class _EditProfileState extends State<EditProfile>
} }
showAlertDialog(context, "Erreur serveur", text); showAlertDialog(context, "Erreur serveur", text);
} }
} else {
showAlertDialog(
context, "Erreur serveur", "Aucune donnée geographique");
}
} else {
showAlertDialog(context, "Erreur serveur", "Mapbox non accessible");
}
} catch (e) {
showAlertDialog(context, "Erreur serveur", "${e}");
}
} else {
showAlertDialog(context, "Erreur utilisateur", "Champ vide");
}
} else {
showAlertDialog(context, "Erreur evenement", "Evenement non futur");
}
} }
@override @override