From 45a46650a21006dc54ccc6468e99bddfe919bc59 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Thu, 24 Apr 2025 10:48:13 -0500 Subject: [PATCH] [ui] Implement provider test button #49 --- .../lib/screens/add_mount_screen.dart | 117 ++++++++++-------- web/repertory/lib/widgets/mount_settings.dart | 6 + 2 files changed, 70 insertions(+), 53 deletions(-) diff --git a/web/repertory/lib/screens/add_mount_screen.dart b/web/repertory/lib/screens/add_mount_screen.dart index f2841ba6..dd042f3f 100644 --- a/web/repertory/lib/screens/add_mount_screen.dart +++ b/web/repertory/lib/screens/add_mount_screen.dart @@ -21,6 +21,8 @@ class _AddMountScreenState extends State { Mount? _mount; final _mountNameController = TextEditingController(); String _mountType = ""; + bool _allowAdd = false; + final Map> _settings = { "": {}, "Encrypt": createDefaultSettings("Encrypt"), @@ -123,6 +125,7 @@ class _AddMountScreenState extends State { child: MountSettingsWidget( isAdd: true, mount: _mount!, + onChanged: () => setState(() => _allowAdd = false), settings: _settings[_mountType]!, showAdvanced: false, ), @@ -132,68 +135,70 @@ class _AddMountScreenState extends State { const SizedBox(height: constants.padding), Row( children: [ - ElevatedButton.icon( - label: const Text('Add'), - icon: const Icon(Icons.add), - onPressed: () async { - final mountList = Provider.of(context); + if (_allowAdd) ...[ + ElevatedButton.icon( + label: const Text('Add'), + icon: const Icon(Icons.add), + onPressed: () async { + final mountList = Provider.of(context); - List failed = []; - if (!validateSettings( - _settings[_mountType]!, - failed, - )) { - for (var key in failed) { - displayErrorMessage( - context, - "Setting '$key' is not valid", - ); + List failed = []; + if (!validateSettings( + _settings[_mountType]!, + failed, + )) { + for (var key in failed) { + displayErrorMessage( + context, + "Setting '$key' is not valid", + ); + } + return; } - return; - } - if (mountList.hasConfigName( - _mountNameController.text, - )) { - return displayErrorMessage( - context, - "Configuration name '${_mountNameController.text}' already exists", - ); - } - - if (_mountType == "Sia" || _mountType == "S3") { - final bucket = - _settings[_mountType]!["${_mountType}Config"]["Bucket"] - as String; - if (mountList.hasBucketName(_mountType, bucket)) { + if (mountList.hasConfigName( + _mountNameController.text, + )) { return displayErrorMessage( context, - "Bucket '$bucket' already exists", + "Configuration name '${_mountNameController.text}' already exists", ); } - } - final success = await mountList.add( - _mountType, - _mountType == 'Remote' - ? '${_settings[_mountType]!['RemoteConfig']['HostNameOrIp']}_${_settings[_mountType]!['RemoteConfig']['ApiPort']}' - : _mountNameController.text, - _settings[_mountType]!, - ); + if (_mountType == "Sia" || _mountType == "S3") { + final bucket = + _settings[_mountType]!["${_mountType}Config"]["Bucket"] + as String; + if (mountList.hasBucketName(_mountType, bucket)) { + return displayErrorMessage( + context, + "Bucket '$bucket' already exists", + ); + } + } - if (!success || !context.mounted) { - return; - } + final success = await mountList.add( + _mountType, + _mountType == 'Remote' + ? '${_settings[_mountType]!['RemoteConfig']['HostNameOrIp']}_${_settings[_mountType]!['RemoteConfig']['ApiPort']}' + : _mountNameController.text, + _settings[_mountType]!, + ); - Navigator.pop(context); - }, - ), - const SizedBox(width: constants.padding), - ElevatedButton.icon( - label: const Text('Test'), - icon: const Icon(Icons.check), - onPressed: _handleProviderTest, - ), + if (!success || !context.mounted) { + return; + } + + Navigator.pop(context); + }, + ), + ], + if (!_allowAdd) + ElevatedButton.icon( + label: const Text('Test'), + icon: const Icon(Icons.check), + onPressed: _handleProviderTest, + ), ], ), ], @@ -237,14 +242,20 @@ class _AddMountScreenState extends State { return; } + if (_allowAdd) { + return; + } + final success = await _mount!.test(); if (!mounted) { return; } + setState(() => _allowAdd = success); + displayErrorMessage( context, - success ? "Success!" : "Provider settings are invalid!", + success ? "Success" : "Provider settings are invalid!", ); } diff --git a/web/repertory/lib/widgets/mount_settings.dart b/web/repertory/lib/widgets/mount_settings.dart index 16aee9b5..058a741c 100644 --- a/web/repertory/lib/widgets/mount_settings.dart +++ b/web/repertory/lib/widgets/mount_settings.dart @@ -18,9 +18,11 @@ class MountSettingsWidget extends StatefulWidget { final bool showAdvanced; final Mount mount; final Map settings; + final Function? onChanged; const MountSettingsWidget({ super.key, this.isAdd = false, + this.onChanged, required this.mount, required this.settings, required this.showAdvanced, @@ -1020,6 +1022,10 @@ class _MountSettingsWidgetState extends State { return; } + if (widget.onChanged != null) { + widget.onChanged!(); + } + super.setState(fn); } }