diff --git a/lib/importer/curl/curl.dart b/lib/importer/curl/curl.dart index 1a0f990b..e4c47df9 100644 --- a/lib/importer/curl/curl.dart +++ b/lib/importer/curl/curl.dart @@ -11,6 +11,8 @@ class CurlFileImport { final headers = mapToRows(curl.headers); final params = mapToRows(curl.uri.queryParameters); final body = curl.data; + // TODO: formdata with file paths must be set to empty as + // there will be permission issue while trying to access the path final formData = curl.formData; // Determine content type based on form data and headers final ContentType contentType = curl.form diff --git a/lib/importer/import_dialog.dart b/lib/importer/import_dialog.dart new file mode 100644 index 00000000..fa2b82fd --- /dev/null +++ b/lib/importer/import_dialog.dart @@ -0,0 +1,50 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:apidash/providers/providers.dart'; +import 'package:apidash/widgets/widgets.dart'; +import 'importer.dart'; + +void importToCollectionPane( + BuildContext context, + WidgetRef ref, + ScaffoldMessengerState sm, +) { + // TODO: The dialog must have a feature to paste contents in a text field + // Also, a mechanism can be added where on importing a file it shows the + // contents in the text field and then the user presses ok to add it to collection + showImportDialog( + context: context, + importFormat: ref.watch(importFormatStateProvider), + onImportFormatChange: (format) { + if (format != null) { + ref.read(importFormatStateProvider.notifier).state = format; + } + }, + onFileDropped: (file) { + final importFormatType = ref.read(importFormatStateProvider); + sm.hideCurrentSnackBar(); + file.readAsString().then( + (content) { + kImporter + .getHttpRequestModel(importFormatType, content) + .then((importedRequestModel) { + if (importedRequestModel != null) { + ref + .read(collectionStateNotifierProvider.notifier) + .addRequestModel(importedRequestModel); + if (!context.mounted) return; + Navigator.of(context).pop(); + } else { + var err = "Unable to parse ${file.name}"; + sm.showSnackBar(getSnackBar(err, small: false)); + } + }); + }, + onError: (e) { + var err = "Unable to import ${file.name}"; + sm.showSnackBar(getSnackBar(err, small: false)); + }, + ); + }, + ); +} diff --git a/lib/importer/importer.dart b/lib/importer/importer.dart index 607bc816..23fa735f 100644 --- a/lib/importer/importer.dart +++ b/lib/importer/importer.dart @@ -13,3 +13,5 @@ class Importer { } } } + +final kImporter = Importer(); diff --git a/lib/screens/home_page/collection_pane.dart b/lib/screens/home_page/collection_pane.dart index 75708db4..aaffe9bc 100644 --- a/lib/screens/home_page/collection_pane.dart +++ b/lib/screens/home_page/collection_pane.dart @@ -1,16 +1,14 @@ import 'package:apidash_design_system/apidash_design_system.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:apidash/importer/import_dialog.dart'; import 'package:apidash/providers/providers.dart'; -import 'package:apidash/importer/importer.dart'; import 'package:apidash/extensions/extensions.dart'; import 'package:apidash/widgets/widgets.dart'; import 'package:apidash/models/models.dart'; import 'package:apidash/consts.dart'; import '../common_widgets/common_widgets.dart'; -final kImporter = Importer(); - class CollectionPane extends ConsumerWidget { const CollectionPane({ super.key, @@ -38,40 +36,7 @@ class CollectionPane extends ConsumerWidget { ref.read(collectionStateNotifierProvider.notifier).add(); }, onImport: () { - showImportDialog( - context: context, - importFormat: ref.watch(importFormatStateProvider), - onImportFormatChange: (format) { - if (format != null) { - ref.read(importFormatStateProvider.notifier).state = format; - } - }, - onFileDropped: (file) { - final importFormatType = ref.read(importFormatStateProvider); - sm.hideCurrentSnackBar(); - file.readAsString().then( - (content) { - kImporter - .getHttpRequestModel(importFormatType, content) - .then((importedRequestModel) { - if (importedRequestModel != null) { - ref - .read(collectionStateNotifierProvider.notifier) - .addRequestModel(importedRequestModel); - } else { - var err = "Unable to parse ${file.name}"; - sm.showSnackBar(getSnackBar(err, small: false)); - } - }); - }, - onError: (e) { - var err = "Unable to import ${file.name}"; - sm.showSnackBar(getSnackBar(err, small: false)); - }, - ); - Navigator.of(context).pop(); - }, - ); + importToCollectionPane(context, ref, sm); }, ), kVSpacer10,