display edit event

This commit is contained in:
Valentin CZERYBA 2024-12-13 23:49:56 +01:00
parent 0dc098554c
commit c58127342b
3 changed files with 74 additions and 133 deletions

View File

@ -7,17 +7,36 @@ class Events {
String? description; String? description;
double? latitude; double? latitude;
double? longitude; double? longitude;
List<String>? tags;
Events({this.place, this.id, this.name, this.startDate}); List<String>? organizers;
String? imgUrl;
Events(
{this.place,
this.id,
this.name,
this.startDate,
this.description,
this.endDate,
this.tags,
this.latitude,
this.longitude,
this.organizers,
this.imgUrl});
Events.fromJson(Map<String, dynamic> json) { Events.fromJson(Map<String, dynamic> json) {
id = json['id']; id = json['id'] as String?;
name = json['name']; name = json['name'] as String?;
place = json['place']; place = json['place'] as String?;
startDate = json["start_date"]; startDate = json['start_date'] as String?;
endDate = json['end_date']; endDate = json['end_date'] as String?;
description = json['description']; description = json['description'] as String?;
latitude = json['latitude']; latitude = (json['latitude'] as num?)?.toDouble(); // Safely cast to double
longitude = json['longitude']; longitude =
(json['longitude'] as num?)?.toDouble(); // Safely cast to double
tags = (json['tags'] as List<dynamic>?)
?.cast<String>(); // Convert List<dynamic> to List<String>
organizers = (json['organizers'] as List<dynamic>?)
?.cast<String>(); // Convert List<dynamic> to List<String>
imgUrl = json['imgUrl'] as String?;
} }
} }

View File

@ -7,6 +7,7 @@ import 'package:textfield_tags/textfield_tags.dart';
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import '../classes/events.dart';
import 'ItemMenu.dart'; import 'ItemMenu.dart';
import '../classes/alert.dart'; import '../classes/alert.dart';
@ -19,29 +20,20 @@ void main() {
} }
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
Map<String, dynamic> events = {}; Events? events;
String id = "";
List<String> organizers = [];
List<String> tags = [];
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,
home: EditEvent(id: id, organizers: organizers, tags: tags), home: EditEvent(events: events),
); );
} }
} }
class EditEvent extends StatefulWidget { class EditEvent extends StatefulWidget {
const EditEvent( const EditEvent({Key? key, required this.events}) : super(key: key);
{Key? key, final Events? events;
required this.id,
required this.tags,
required this.organizers})
: super(key: key);
final String id;
final List<String> tags;
final List<String> organizers;
@override @override
_EditEventState createState() => _EditEventState(); _EditEventState createState() => _EditEventState();
@ -67,7 +59,6 @@ class _EditEventState extends State<EditEvent>
List<Map<String, dynamic>> suggestions = []; List<Map<String, dynamic>> suggestions = [];
String geographicalZone = ""; String geographicalZone = "";
String imgUrl = ""; String imgUrl = "";
Map<String, dynamic> events = {};
List<String> initialTags = []; List<String> initialTags = [];
@ -154,97 +145,6 @@ class _EditEventState extends State<EditEvent>
return "${year}-${month}-${day}"; return "${year}-${month}-${day}";
} }
Future<void> _getEventInfos() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
String formerName = "";
String formerMap = "";
String formerImage = "";
String formerDesc = "";
if (accessToken.isNotEmpty) {
var urlGet = Uri.parse("${globals.api}/events/${widget.id}");
var responseGet = await http.get(urlGet,
headers: {HttpHeaders.cookieHeader: 'access_token=${accessToken}'});
stderr.writeln('Response Get status: ${responseGet.statusCode}');
if (responseGet.statusCode == 200) {
stderr.writeln('Username : ${responseGet.body}');
events = jsonDecode(utf8.decode(responseGet.bodyBytes));
formerName = convertNulltoEmptyString(events["name"]);
formerMap = convertNulltoEmptyString(events["place"]);
formerDesc = convertNulltoEmptyString(events["description"]);
startDate = DateTime.parse(events["start_date"]);
endDate = DateTime.parse(events["end_date"]);
if (events["imgUrl"] != null) {
formerImage = events["imgUrl"];
}
} else {
var text = "";
switch (responseGet.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 = "Vous n'avez pas l'autorisation de faire cette action";
}
break;
case 410:
{
text = "Token invalide";
}
break;
case 500:
{
text = "Probleme interne du serveur";
}
break;
default:
{
text = "Probleme d'authentification inconnu";
}
break;
}
showErrorDialog(context, text);
}
} else {
showErrorDialog(context, "Cache invalide");
}
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
inputName.text = formerName;
startDatepicker.text =
DateFormat("dd/MM/yyyy").format(startDate).toString();
startTimepicker.text = DateFormat("HH:mm").format(startDate).toString();
endDatepicker.text = DateFormat("dd/MM/yyyy").format(endDate).toString();
endTimepicker.text = DateFormat("HH:mm").format(endDate).toString();
inputGeo.text = formerMap;
imgUrl = formerImage;
inputDesc.text = formerDesc;
});
}
Future<void> _updateEvent(BuildContext context) async { Future<void> _updateEvent(BuildContext context) async {
var name = inputName.text; var name = inputName.text;
var place = inputGeo.text; var place = inputGeo.text;
@ -431,10 +331,30 @@ class _EditEventState extends State<EditEvent>
@override @override
void initState() { void initState() {
initialTags = List<String>.from(widget.tags as List); inputName.text = widget.events!.name ?? "";
initialOrga = List<String>.from(widget.organizers as List); startDatepicker.text = DateFormat("dd/MM/yyyy")
.format(DateTime.parse(
widget.events!.startDate ?? DateTime.now().toString()))
.toString();
startTimepicker.text = DateFormat("HH:mm")
.format(DateTime.parse(
widget.events!.startDate ?? DateTime.now().toString()))
.toString();
endDatepicker.text = DateFormat("dd/MM/yyyy")
.format(
DateTime.parse(widget.events!.endDate ?? DateTime.now().toString()))
.toString();
endTimepicker.text = DateFormat("HH:mm")
.format(
DateTime.parse(widget.events!.endDate ?? DateTime.now().toString()))
.toString();
inputGeo.text = widget.events!.place ?? "";
imgUrl = widget.events!.imgUrl ?? "";
inputDesc.text = widget.events!.description ?? "";
initialTags = List<String>.from(widget.events!.tags as List);
initialOrga = List<String>.from(widget.events!.organizers as List);
super.initState(); super.initState();
_getEventInfos();
} }
final _formKey = GlobalKey<FormState>(); final _formKey = GlobalKey<FormState>();

View File

@ -106,23 +106,27 @@ class _ItemMenuState extends State<ItemMenu> with ShowErrorDialog {
stderr.writeln('Response Get status: ${responseGet.statusCode}'); stderr.writeln('Response Get status: ${responseGet.statusCode}');
if (responseGet.statusCode == 200) { if (responseGet.statusCode == 200) {
stderr.writeln('Username : ${responseGet.body}'); stderr.writeln('Username : ${responseGet.body}');
var events = jsonDecode(utf8.decode(responseGet.bodyBytes)); events =
id = events["id"]; Events.fromJson(jsonDecode(utf8.decode(responseGet.bodyBytes)));
formerName = events["name"]; id = events!.id ?? "";
formerMap = "${events["place"]}"; formerName = events!.name ?? "";
formerDesc = events["description"]; formerMap = "${events!.place}" ?? "";
formerTags = List<String>.from(events['tags'] as List); formerDesc = events!.description ?? "";
formerOrga = List<String>.from(events['organizers'] as List);
final startDate = DateTime.parse(events["start_date"]); formerTags = List<String>.from(events!.tags as List);
formerOrga = List<String>.from(events!.organizers as List);
final startDate =
DateTime.parse(events!.startDate ?? DateTime.now().toString());
final date = DateFormat.yMd().format(startDate); final date = DateFormat.yMd().format(startDate);
final time = DateFormat.Hm().format(startDate); final time = DateFormat.Hm().format(startDate);
final endDate = DateTime.parse(events["end_date"]); final endDate =
DateTime.parse(events!.endDate ?? DateTime.now().toString());
final dateE = DateFormat.yMd().format(endDate); final dateE = DateFormat.yMd().format(endDate);
final timeE = DateFormat.Hm().format(endDate); final timeE = DateFormat.Hm().format(endDate);
if (events["imgUrl"] != null) { if (events!.imgUrl != null) {
formerImage = events["imgUrl"]; formerImage = events!.imgUrl ?? "";
} }
formerDate = "${date} ${time} à ${dateE} ${timeE}"; formerDate = "${date} ${time} à ${dateE} ${timeE}";
@ -404,9 +408,7 @@ class _ItemMenuState extends State<ItemMenu> with ShowErrorDialog {
onPressed: () { onPressed: () {
Navigator.push( Navigator.push(
context, context,
MaterialPageRoute( MaterialPageRoute(builder: (_) => EditEvent(events: events)),
builder: (_) =>
EditEvent(id: id, tags: tags, organizers: organizers)),
); );
}, },
backgroundColor: Colors.blue, backgroundColor: Colors.blue,