display edit event
This commit is contained in:
parent
0dc098554c
commit
c58127342b
@ -7,17 +7,36 @@ class Events {
|
||||
String? description;
|
||||
double? latitude;
|
||||
double? longitude;
|
||||
|
||||
Events({this.place, this.id, this.name, this.startDate});
|
||||
List<String>? tags;
|
||||
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) {
|
||||
id = json['id'];
|
||||
name = json['name'];
|
||||
place = json['place'];
|
||||
startDate = json["start_date"];
|
||||
endDate = json['end_date'];
|
||||
description = json['description'];
|
||||
latitude = json['latitude'];
|
||||
longitude = json['longitude'];
|
||||
id = json['id'] as String?;
|
||||
name = json['name'] as String?;
|
||||
place = json['place'] as String?;
|
||||
startDate = json['start_date'] as String?;
|
||||
endDate = json['end_date'] as String?;
|
||||
description = json['description'] as String?;
|
||||
latitude = (json['latitude'] as num?)?.toDouble(); // Safely cast to double
|
||||
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?;
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import 'package:textfield_tags/textfield_tags.dart';
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import '../classes/events.dart';
|
||||
|
||||
import 'ItemMenu.dart';
|
||||
import '../classes/alert.dart';
|
||||
@ -19,29 +20,20 @@ void main() {
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
Map<String, dynamic> events = {};
|
||||
String id = "";
|
||||
List<String> organizers = [];
|
||||
List<String> tags = [];
|
||||
Events? events;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: EditEvent(id: id, organizers: organizers, tags: tags),
|
||||
home: EditEvent(events: events),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class EditEvent extends StatefulWidget {
|
||||
const EditEvent(
|
||||
{Key? key,
|
||||
required this.id,
|
||||
required this.tags,
|
||||
required this.organizers})
|
||||
: super(key: key);
|
||||
final String id;
|
||||
final List<String> tags;
|
||||
final List<String> organizers;
|
||||
const EditEvent({Key? key, required this.events}) : super(key: key);
|
||||
final Events? events;
|
||||
|
||||
@override
|
||||
_EditEventState createState() => _EditEventState();
|
||||
@ -67,7 +59,6 @@ class _EditEventState extends State<EditEvent>
|
||||
List<Map<String, dynamic>> suggestions = [];
|
||||
String geographicalZone = "";
|
||||
String imgUrl = "";
|
||||
Map<String, dynamic> events = {};
|
||||
|
||||
List<String> initialTags = [];
|
||||
|
||||
@ -154,97 +145,6 @@ class _EditEventState extends State<EditEvent>
|
||||
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 {
|
||||
var name = inputName.text;
|
||||
var place = inputGeo.text;
|
||||
@ -431,10 +331,30 @@ class _EditEventState extends State<EditEvent>
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
initialTags = List<String>.from(widget.tags as List);
|
||||
initialOrga = List<String>.from(widget.organizers as List);
|
||||
inputName.text = widget.events!.name ?? "";
|
||||
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();
|
||||
_getEventInfos();
|
||||
}
|
||||
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
@ -106,23 +106,27 @@ class _ItemMenuState extends State<ItemMenu> with ShowErrorDialog {
|
||||
stderr.writeln('Response Get status: ${responseGet.statusCode}');
|
||||
if (responseGet.statusCode == 200) {
|
||||
stderr.writeln('Username : ${responseGet.body}');
|
||||
var events = jsonDecode(utf8.decode(responseGet.bodyBytes));
|
||||
id = events["id"];
|
||||
formerName = events["name"];
|
||||
formerMap = "${events["place"]}";
|
||||
formerDesc = events["description"];
|
||||
formerTags = List<String>.from(events['tags'] as List);
|
||||
formerOrga = List<String>.from(events['organizers'] as List);
|
||||
final startDate = DateTime.parse(events["start_date"]);
|
||||
events =
|
||||
Events.fromJson(jsonDecode(utf8.decode(responseGet.bodyBytes)));
|
||||
id = events!.id ?? "";
|
||||
formerName = events!.name ?? "";
|
||||
formerMap = "${events!.place}" ?? "";
|
||||
formerDesc = events!.description ?? "";
|
||||
|
||||
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 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 timeE = DateFormat.Hm().format(endDate);
|
||||
if (events["imgUrl"] != null) {
|
||||
formerImage = events["imgUrl"];
|
||||
if (events!.imgUrl != null) {
|
||||
formerImage = events!.imgUrl ?? "";
|
||||
}
|
||||
|
||||
formerDate = "${date} ${time} à ${dateE} ${timeE}";
|
||||
@ -404,9 +408,7 @@ class _ItemMenuState extends State<ItemMenu> with ShowErrorDialog {
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) =>
|
||||
EditEvent(id: id, tags: tags, organizers: organizers)),
|
||||
MaterialPageRoute(builder: (_) => EditEvent(events: events)),
|
||||
);
|
||||
},
|
||||
backgroundColor: Colors.blue,
|
||||
|
Loading…
x
Reference in New Issue
Block a user