evenement_create.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Automatic FlutterFlow imports
  2. import '/backend/schema/structs/index.dart';
  3. import '/flutter_flow/flutter_flow_theme.dart';
  4. import '/flutter_flow/flutter_flow_util.dart';
  5. import 'index.dart'; // Imports other custom actions
  6. import '/flutter_flow/custom_functions.dart'; // Imports custom functions
  7. import 'package:flutter/material.dart';
  8. // Begin custom action code
  9. // DO NOT REMOVE OR MODIFY THE CODE ABOVE!
  10. import 'dart:async';
  11. import 'dart:convert';
  12. import 'package:http/http.dart' as http;
  13. Future<dynamic> evenementCreate(
  14. String sessionName,
  15. String sessionId,
  16. String token,
  17. String horecagelegenheidNid,
  18. String titel,
  19. String datumStart,
  20. String? datumEind,
  21. String? omschrijving,
  22. String? entreeTid,
  23. String? entreePrijs,
  24. String? toelichtingEntree,
  25. String? ticketsUrl,
  26. String? websiteUrl,
  27. List<String>? categorieTids,
  28. String? logoFid,
  29. List<String>? fotosFids,
  30. ) async {
  31. final horecaNidInt = int.tryParse(horecagelegenheidNid.trim());
  32. if (horecaNidInt == null) {
  33. return {'success': false, 'error': 'Kies eerst een horecagelegenheid'};
  34. }
  35. if (titel.trim().isEmpty) {
  36. return {'success': false, 'error': 'Titel is verplicht'};
  37. }
  38. if (datumStart.trim().isEmpty) {
  39. return {'success': false, 'error': 'Datum is verplicht'};
  40. }
  41. final body = <String, dynamic>{
  42. 'horecagelegenheid_nid': horecaNidInt,
  43. 'titel': titel.trim(),
  44. 'datum_start': datumStart.trim(),
  45. };
  46. if ((datumEind ?? '').trim().isNotEmpty) {
  47. body['datum_eind'] = datumEind!.trim();
  48. }
  49. if ((omschrijving ?? '').trim().isNotEmpty) {
  50. body['omschrijving'] = omschrijving!.trim();
  51. }
  52. if ((entreeTid ?? '').trim().isNotEmpty) {
  53. final tid = int.tryParse(entreeTid!.trim());
  54. if (tid != null) body['entree_tid'] = tid;
  55. }
  56. if ((entreePrijs ?? '').trim().isNotEmpty) {
  57. body['entree_prijs'] = entreePrijs!.trim();
  58. }
  59. if ((toelichtingEntree ?? '').trim().isNotEmpty) {
  60. body['toelichting_entree'] = toelichtingEntree!.trim();
  61. }
  62. if ((ticketsUrl ?? '').trim().isNotEmpty) {
  63. body['tickets_url'] = ticketsUrl!.trim();
  64. }
  65. if ((websiteUrl ?? '').trim().isNotEmpty) {
  66. body['website_url'] = websiteUrl!.trim();
  67. }
  68. if (categorieTids != null && categorieTids.isNotEmpty) {
  69. final tids = categorieTids
  70. .map((e) => int.tryParse(e.trim()))
  71. .whereType<int>()
  72. .toList();
  73. if (tids.isNotEmpty) body['categorie_tids'] = tids;
  74. }
  75. if ((logoFid ?? '').trim().isNotEmpty) {
  76. final fid = int.tryParse(logoFid!.trim());
  77. if (fid != null) body['logo_fid'] = fid;
  78. }
  79. if (fotosFids != null && fotosFids.isNotEmpty) {
  80. final fids =
  81. fotosFids.map((e) => int.tryParse(e.trim())).whereType<int>().toList();
  82. if (fids.isNotEmpty) body['fotos_fids'] = fids;
  83. }
  84. final headers = <String, String>{
  85. 'Accept': 'application/json',
  86. 'Content-Type': 'application/json',
  87. };
  88. if (sessionName.trim().isNotEmpty && sessionId.trim().isNotEmpty) {
  89. headers['Cookie'] = '$sessionName=$sessionId';
  90. }
  91. if (token.trim().isNotEmpty) {
  92. headers['X-CSRF-Token'] = token;
  93. }
  94. final base = FFAppState().apiBaseUrl.trim().isEmpty
  95. ? 'https://uitgaanskrant.com'
  96. : FFAppState().apiBaseUrl.trim();
  97. final url = '$base/en/flutterdrup/evenementen/create.json';
  98. print('========================================');
  99. print('EVENEMENT CREATE');
  100. print('URL : $url');
  101. print('BODY : ${jsonEncode(body)}');
  102. print('========================================');
  103. try {
  104. final response = await http
  105. .post(Uri.parse(url), headers: headers, body: jsonEncode(body))
  106. .timeout(const Duration(seconds: 30));
  107. print('========================================');
  108. print('STATUS : ${response.statusCode}');
  109. print('BODY');
  110. print(response.body);
  111. print('========================================');
  112. dynamic data;
  113. try {
  114. data = jsonDecode(response.body);
  115. } catch (_) {
  116. data = response.body;
  117. }
  118. if (response.statusCode >= 200 && response.statusCode < 300) {
  119. return {
  120. 'success': true,
  121. 'nid': data is Map ? (data['nid']?.toString() ?? '') : '',
  122. 'status': data is Map ? (data['status']?.toString() ?? '') : '',
  123. };
  124. }
  125. if (response.statusCode == 403) {
  126. return {
  127. 'success': false,
  128. 'statusCode': 403,
  129. 'error': 'Deze horecagelegenheid is niet van jou',
  130. };
  131. }
  132. return {
  133. 'success': false,
  134. 'statusCode': response.statusCode,
  135. 'error': data is Map
  136. ? ((data['message'] ?? data['error'] ?? data).toString())
  137. : data.toString(),
  138. };
  139. } on TimeoutException {
  140. return {'success': false, 'error': 'Timeout'};
  141. } catch (e, stack) {
  142. print(e);
  143. print(stack);
  144. return {'success': false, 'error': e.toString()};
  145. }
  146. }
  147. // Set your action name, define your arguments and return parameter,
  148. // and then add the boilerplate code using the `</>` button on the right!