From c3937a3e305dd5342363fd55784eb247168703a8 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 30 Jul 2024 23:46:15 +0200 Subject: [PATCH] redirect to item list menu --- covas_mobile/lib/classes/eventAdded.dart | 7 +- covas_mobile/lib/pages/UpdateEventImage.dart | 71 ++++++++------------ 2 files changed, 34 insertions(+), 44 deletions(-) diff --git a/covas_mobile/lib/classes/eventAdded.dart b/covas_mobile/lib/classes/eventAdded.dart index 68a09fa..d2c75b7 100644 --- a/covas_mobile/lib/classes/eventAdded.dart +++ b/covas_mobile/lib/classes/eventAdded.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; -import '../main.dart'; +import '../pages/ListItemMenu.dart'; mixin ShowEventDialog on State { void showEventDialog(BuildContext context, String text) { @@ -16,7 +16,10 @@ mixin ShowEventDialog on State { textStyle: TextStyle(fontSize: 15, fontWeight: FontWeight.normal)), onPressed: () { - Navigator.of(context).pop("Yes, Of course!"); // Return value + Navigator.push( + context, + MaterialPageRoute( + builder: (_) => ListItemMenu())); // Return value }), ], ); diff --git a/covas_mobile/lib/pages/UpdateEventImage.dart b/covas_mobile/lib/pages/UpdateEventImage.dart index ca699c5..af00c93 100644 --- a/covas_mobile/lib/pages/UpdateEventImage.dart +++ b/covas_mobile/lib/pages/UpdateEventImage.dart @@ -9,6 +9,7 @@ import 'dart:io'; import 'ListItemMenu.dart'; import '../classes/alert.dart'; +import '../classes/eventAdded.dart'; import '../variable/globals.dart' as globals; @@ -36,60 +37,46 @@ class UpdateeventImage extends StatefulWidget { } class _UpdateeventImageState extends State - with ShowErrorDialog { + with ShowErrorDialog, ShowEventDialog { TextEditingController inputName = TextEditingController(); TextEditingController inputPlace = TextEditingController(); TextEditingController inputDate = TextEditingController(); - Future _login(BuildContext context) async { + Future _updateEvent(BuildContext context) async { var url = Uri.parse("${globals.api}/token"); - var pseudo = inputName.text; - var password = inputPlace.text; - print("get login"); - print(pseudo.isNotEmpty); - print(password.isNotEmpty); - if ((pseudo.isNotEmpty) && (password.isNotEmpty)) { - print(url); + var name = inputName.text; + var place = inputPlace.text; + SharedPreferences prefs = await SharedPreferences.getInstance(); + var accessToken = prefs.getString("access_token") ?? ""; + List send = ["toto"]; + if (accessToken.isNotEmpty) { try { //String credentials = "${pseudo}:${password}"; //Codec stringToBase64 = utf8.fuse(base64); //String encoded = stringToBase64.encode(credentials); - var response = await http.post(url, - // headers: { - // HttpHeaders.authorizationHeader: 'Basic $encoded', - //} + var urlPut = Uri.parse("${globals.api}/events"); + var responsePut = await http.put(urlPut, headers: { - 'accept': 'application/json', - 'Content-Type': 'application/x-www-form-urlencoded', + HttpHeaders.cookieHeader: 'access_token=${accessToken}', + HttpHeaders.acceptHeader: 'application/json, text/plain, */*', + HttpHeaders.contentTypeHeader: 'application/json' }, - body: { - "username": "${pseudo}", - "password": "${password}" - }); - print(response.statusCode); - if ((response.statusCode == 200) || (response.statusCode == 201)) { - SharedPreferences prefs = await SharedPreferences.getInstance(); - - var cookies = response.headers["set-cookie"].toString().split(";"); - for (var cookie in cookies) { - var cookiesMany = cookie.split(","); - for (var cookie2 in cookiesMany) { - switch (cookie2.split("=")[0]) { - case "access_token": - { - prefs.setString("access_token", cookie2.split("=")[1]); - } - break; - default: - break; - } - } - } - Navigator.push( - context, MaterialPageRoute(builder: (_) => ListItemMenu())); + body: jsonEncode({ + 'name': name, + 'place': place, + 'start_date': widget.events["date"], + 'end_date': widget.events['date'], + 'organizers': send, + 'latitude': '0.0', + 'longitude': '0.0', + })); + print(responsePut.statusCode); + if ((responsePut.statusCode == 200) || + (responsePut.statusCode == 201)) { + showEventDialog(context, "Evenement ${name} ajoute"); } else { var text = ""; - switch (response.statusCode) { + switch (responsePut.statusCode) { case 400: { text = "RequĂȘte mal construite"; @@ -204,7 +191,7 @@ class _UpdateeventImageState extends State color: Colors.blue, borderRadius: BorderRadius.circular(20)), child: TextButton( onPressed: () { - _login(context); + _updateEvent(context); }, child: Text( 'Ajouter',