From cd5bff551f161957bc5288aeb57c87ded4862182 Mon Sep 17 00:00:00 2001 From: MDeLuise <66636702+MDeLuise@users.noreply.github.com> Date: Tue, 22 Oct 2024 15:41:20 +0200 Subject: [PATCH] fix: resolve issue with adding duplicate genus species causing error if page not reloaded --- frontend/lib/plant_add/add_plant_page.dart | 11 ++++++----- frontend/lib/search/species_details_bottom_bar.dart | 6 +++++- frontend/lib/search/species_details_page.dart | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/frontend/lib/plant_add/add_plant_page.dart b/frontend/lib/plant_add/add_plant_page.dart index 018465c8..85dab7b9 100644 --- a/frontend/lib/plant_add/add_plant_page.dart +++ b/frontend/lib/plant_add/add_plant_page.dart @@ -58,10 +58,11 @@ class _AddPlantPageState extends State { void _createPlant() async { try { int speciesId = widget.species.id ?? -1; + SpeciesDTO? updatedSpecies; if (widget.species.id == null) { - speciesId = await _createSpecies(); + updatedSpecies = await _createSpecies(); } - _toCreate.speciesId = speciesId; + _toCreate.speciesId = updatedSpecies?.id ?? speciesId; _toCreate.info.state = "PURCHASED"; final response = await widget.env.http.post("plant", _toCreate.toMap()); final responseBody = json.decode(utf8.decode(response.bodyBytes)); @@ -76,14 +77,14 @@ class _AddPlantPageState extends State { if (!mounted) return; widget.env.toastManager.showToast(context, ToastNotificationType.success, AppLocalizations.of(context).plantCreatedSuccessfully); - Navigator.pop(context, true); + Navigator.pop(context, updatedSpecies); } catch (e, st) { widget.env.logger.error(e, st); throw AppException.withInnerException(e as Exception); } } - Future _createSpecies() async { + Future _createSpecies() async { try { final response = await widget.env.http.post("botanical-info", widget.species.toMap()); @@ -95,7 +96,7 @@ class _AddPlantPageState extends State { throw AppException(AppLocalizations.of(context).errorCreatingSpecies); } widget.env.logger.info("Species successfully created"); - return responseBody["id"]; + return SpeciesDTO.fromJson(responseBody); } catch (e, st) { widget.env.logger.error(e, st); throw AppException.withInnerException(e as Exception); diff --git a/frontend/lib/search/species_details_bottom_bar.dart b/frontend/lib/search/species_details_bottom_bar.dart index 4602aa82..7a0f2d71 100644 --- a/frontend/lib/search/species_details_bottom_bar.dart +++ b/frontend/lib/search/species_details_bottom_bar.dart @@ -101,7 +101,11 @@ class SpeciesDetailsBottomActionBar extends StatelessWidget { species: species, ), ), - ), + ).then((speciesCreated) { + if (speciesCreated != null) { + updateSpeciesLocally(speciesCreated); + } + }), child: Row( mainAxisSize: MainAxisSize.min, children: [ diff --git a/frontend/lib/search/species_details_page.dart b/frontend/lib/search/species_details_page.dart index 1032118e..4546b9c9 100644 --- a/frontend/lib/search/species_details_page.dart +++ b/frontend/lib/search/species_details_page.dart @@ -25,8 +25,8 @@ class SpeciesDetailsPage extends StatefulWidget { class _SpeciesDetailsPageState extends State { void _updateSpeciesLocally(SpeciesDTO species) { - fetchAndSetPlants(context, widget.env); widget.updateSpeciesLocally(species); + fetchAndSetPlants(context, widget.env); } @override