change place input + fix endpoint #22

Merged
v4l3n71n merged 1 commits from hotfix/search into main 2024-11-24 21:45:26 +00:00

View File

@ -50,7 +50,7 @@ class _MyHomePageState extends State<ListItemMenu> {
TextEditingController inputItem = TextEditingController(); TextEditingController inputItem = TextEditingController();
bool showDateFields = false; // State to toggle date fields bool showDateFields = false; // State to toggle date fields
bool showArrow = true;
// Fetching events from API // Fetching events from API
static Future<List<Events>> getPosts() async { static Future<List<Events>> getPosts() async {
PermissionStatus status = await Permission.location.status; PermissionStatus status = await Permission.location.status;
@ -258,6 +258,9 @@ class _MyHomePageState extends State<ListItemMenu> {
'geometry'], // Include geometry for latitude/longitude 'geometry'], // Include geometry for latitude/longitude
}) })
.toList(); .toList();
if (suggestions.isNotEmpty) {
showArrow = false;
}
}); });
} else { } else {
throw Exception('Failed to load suggestions'); throw Exception('Failed to load suggestions');
@ -272,7 +275,7 @@ class _MyHomePageState extends State<ListItemMenu> {
double latitude = prefs.getDouble("city_lat") ?? 0.0; double latitude = prefs.getDouble("city_lat") ?? 0.0;
double longitude = prefs.getDouble("city_long") ?? 0.0; double longitude = prefs.getDouble("city_long") ?? 0.0;
String stringParameter = ""; String stringParameter = "";
String dateParameter = "current_datetime"; String endpoint = "events";
if ((latitude != 0.0) && (longitude != 0.0)) { if ((latitude != 0.0) && (longitude != 0.0)) {
// Calculate the boundaries // Calculate the boundaries
double radiusInKm = 50; double radiusInKm = 50;
@ -283,19 +286,22 @@ class _MyHomePageState extends State<ListItemMenu> {
double maxLat = latitude + latDistance; double maxLat = latitude + latDistance;
double minLon = longitude - lonDistance; double minLon = longitude - lonDistance;
double maxLon = longitude + lonDistance; double maxLon = longitude + lonDistance;
endpoint = "events/search";
stringParameter = "min_lat=$minLat&max_lat=$maxLat" stringParameter = "min_lat=$minLat&max_lat=$maxLat"
"&min_lon=$minLon&max_lon=$maxLon"; "&min_lon=$minLon&max_lon=$maxLon";
} }
DateTime currentDate = DateTime.now(); DateTime currentDate = DateTime.now();
dateParameter = "&current_datetime=${currentDate.toString()}"; String dateParameter = "current_datetime=${currentDate.toString()}";
if (startDatepicker.text.isNotEmpty) { if (startDatepicker.text.isNotEmpty) {
var date = DateTime.parse(formatDate(startDatepicker.text)); var date = DateTime.parse(formatDate(startDatepicker.text));
dateParameter = "&date_event=" + date.toString(); dateParameter = "date_event=" + date.toString();
endpoint = "events/search";
} }
if (endDatepicker.text.isNotEmpty) { if (endDatepicker.text.isNotEmpty) {
var date = DateTime.parse(formatDate(endDatepicker.text)); var date = DateTime.parse(formatDate(endDatepicker.text));
dateParameter = "&date_event=" + date.toString(); dateParameter = "date_event=" + date.toString();
endpoint = "events/search";
} }
if ((startDatepicker.text.isNotEmpty) && if ((startDatepicker.text.isNotEmpty) &&
(endDatepicker.text.isNotEmpty)) { (endDatepicker.text.isNotEmpty)) {
@ -305,13 +311,20 @@ class _MyHomePageState extends State<ListItemMenu> {
startDate.toString() + startDate.toString() +
"&end_date=" + "&end_date=" +
endDate.toString(); endDate.toString();
endpoint = "events/search";
} }
stringParameter = stringParameter + dateParameter;
if (inputItem.text.isNotEmpty) { if (inputItem.text.isNotEmpty) {
stringParameter = stringParameter + "&item=${inputItem.text}"; stringParameter = stringParameter + "&item=${inputItem.text}";
endpoint = "events/search";
}
if (stringParameter.isNotEmpty) {
stringParameter = "$stringParameter&$dateParameter";
} else {
stringParameter = dateParameter;
} }
print("stringParameter : ${stringParameter}"); print("stringParameter : ${stringParameter}");
var url = Uri.parse("${globals.api}/events/search?${stringParameter}"); var url = Uri.parse("${globals.api}/${endpoint}?${stringParameter}");
final response = await http.get(url, headers: { final response = await http.get(url, headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
HttpHeaders.cookieHeader: "access_token=$accessToken" HttpHeaders.cookieHeader: "access_token=$accessToken"
@ -408,10 +421,9 @@ class _MyHomePageState extends State<ListItemMenu> {
inputGeo.clear(); // Clear the text field inputGeo.clear(); // Clear the text field
geographicalZone = ''; // Reset the geographical zone state geographicalZone = ''; // Reset the geographical zone state
suggestions.clear(); // Optionally clear suggestions suggestions.clear(); // Optionally clear suggestions
fetchPostsByLocation();
/// Clear the filtered posts /// Clear the filtered posts
}); });
fetchPostsByLocation();
}, },
), ),
), ),
@ -424,7 +436,7 @@ class _MyHomePageState extends State<ListItemMenu> {
), ),
if (suggestions.isNotEmpty) if (suggestions.isNotEmpty)
Container( Container(
height: 200, height: 175,
decoration: BoxDecoration( decoration: BoxDecoration(
border: Border.all(color: Colors.blue), border: Border.all(color: Colors.blue),
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),
@ -445,6 +457,7 @@ class _MyHomePageState extends State<ListItemMenu> {
geographicalZone = suggestions[index]['place_name']; geographicalZone = suggestions[index]['place_name'];
inputGeo.text = geographicalZone; inputGeo.text = geographicalZone;
suggestions.clear(); suggestions.clear();
showArrow = true;
}); });
SharedPreferences prefs = SharedPreferences prefs =
await SharedPreferences.getInstance(); await SharedPreferences.getInstance();
@ -506,23 +519,29 @@ class _MyHomePageState extends State<ListItemMenu> {
body: Column( body: Column(
children: [ children: [
_buildItemZoneSearchField(), _buildItemZoneSearchField(),
if (showDateFields)
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(child: _buildDateField("start")),
Flexible(child: _buildDateField("end"))
]),
if (showDateFields) _buildGeographicalZoneSearchField(), if (showDateFields) _buildGeographicalZoneSearchField(),
if (showDateFields) _buildDateField("start"), if (showArrow)
if (showDateFields) _buildDateField("end"), IconButton(
IconButton( onPressed: () {
onPressed: () { setState(() {
setState(() { showDateFields = !showDateFields; // Toggle visibility
showDateFields = !showDateFields; // Toggle visibility });
}); },
}, icon: Icon(
icon: Icon( showDateFields
showDateFields ? Icons.keyboard_arrow_up
? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down,
: Icons.keyboard_arrow_down, color: Colors.blue,
color: Colors.blue, ),
tooltip: showDateFields ? 'Show Date Fields' : 'Hide Date Fields',
), ),
tooltip: showDateFields ? 'Show Date Fields' : 'Hide Date Fields',
),
Expanded( Expanded(
child: FutureBuilder<List<Events>>( child: FutureBuilder<List<Events>>(
future: postsFuture, future: postsFuture,