Skip to content

Commit

Permalink
fix: resolve issue with adding duplicate genus species causing error …
Browse files Browse the repository at this point in the history
…if page not reloaded
  • Loading branch information
MDeLuise committed Oct 22, 2024
1 parent e322f65 commit cd5bff5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
11 changes: 6 additions & 5 deletions frontend/lib/plant_add/add_plant_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ class _AddPlantPageState extends State<AddPlantPage> {
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));
Expand All @@ -76,14 +77,14 @@ class _AddPlantPageState extends State<AddPlantPage> {
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<int> _createSpecies() async {
Future<SpeciesDTO> _createSpecies() async {
try {
final response =
await widget.env.http.post("botanical-info", widget.species.toMap());
Expand All @@ -95,7 +96,7 @@ class _AddPlantPageState extends State<AddPlantPage> {
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);
Expand Down
6 changes: 5 additions & 1 deletion frontend/lib/search/species_details_bottom_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ class SpeciesDetailsBottomActionBar extends StatelessWidget {
species: species,
),
),
),
).then((speciesCreated) {
if (speciesCreated != null) {
updateSpeciesLocally(speciesCreated);
}
}),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Expand Down
2 changes: 1 addition & 1 deletion frontend/lib/search/species_details_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class SpeciesDetailsPage extends StatefulWidget {

class _SpeciesDetailsPageState extends State<SpeciesDetailsPage> {
void _updateSpeciesLocally(SpeciesDTO species) {
fetchAndSetPlants(context, widget.env);
widget.updateSpeciesLocally(species);
fetchAndSetPlants(context, widget.env);
}

@override
Expand Down

0 comments on commit cd5bff5

Please sign in to comment.