From e4041020f1c288d279f9162380f609ed426a439e Mon Sep 17 00:00:00 2001 From: Dan Cojocaru Date: Mon, 20 Jun 2022 01:20:23 +0300 Subject: [PATCH] Warn user when project/component name exists --- lib/dialogs/new_project.dart | 5 ++++- lib/pages/edit_component.dart | 9 ++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/dialogs/new_project.dart b/lib/dialogs/new_project.dart index 5cb6701..d8270d8 100644 --- a/lib/dialogs/new_project.dart +++ b/lib/dialogs/new_project.dart @@ -20,7 +20,7 @@ class NewProjectDialog extends HookWidget { final newProjectAction = useMemoized(() { if (newDialogNameController.text.isEmpty) return null; return () { - projectsState.newProject(newDialogNameController.text); + projectsState.newProject(newDialogNameController.text.trim()); Navigator.pop(context); }; }, [newDialogNameController.text]); @@ -217,6 +217,9 @@ class NewProjectDialog extends HookWidget { icon: const Icon(Icons.done), onPressed: newProjectAction, ), + errorText: projectsState.projects.map((p) => p.projectName).contains(newDialogNameController.text.trim()) + ? 'A project with the same name already exists' + : null, ), onSubmitted: newProjectAction == null ? null : (_) => newProjectAction(), ), diff --git a/lib/pages/edit_component.dart b/lib/pages/edit_component.dart index 80c1e68..ec3d113 100644 --- a/lib/pages/edit_component.dart +++ b/lib/pages/edit_component.dart @@ -168,9 +168,12 @@ class EditComponentPage extends HookWidget { sliver: SliverToBoxAdapter( child: TextField( controller: componentNameEditingController, - decoration: const InputDecoration( - border: OutlineInputBorder(), + decoration: InputDecoration( + border: const OutlineInputBorder(), labelText: 'Component name', + errorText: projectState.index.components.where((c) => c.componentId != ce().componentId).map((c) => c.componentName).contains(componentNameEditingController.text.trim()) + ? 'A component with the same name already exists' + : null, ), ), ), @@ -577,7 +580,7 @@ class EditComponentPage extends HookWidget { floatingActionButton: !dirty ? null : FloatingActionButton( onPressed: () async { if (componentNameEditingController.text.isNotEmpty) { - await projectState.editComponent(ce().copyWith(componentName: componentNameEditingController.text)); + await projectState.editComponent(ce().copyWith(componentName: componentNameEditingController.text.trim())); } await projectState.editComponent(ce().copyWith( inputs: inputs.value,