From 0b99dab05d79e97b8dd0eb84f3aa52eb17fc523e Mon Sep 17 00:00:00 2001 From: Tanish2002 Date: Tue, 27 Feb 2024 12:11:11 +0530 Subject: [PATCH 1/2] feat: initial code to sync url text with url params --- .../home_page/editor_pane/url_card.dart | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/screens/home_page/editor_pane/url_card.dart b/lib/screens/home_page/editor_pane/url_card.dart index 5fa4b404..72824385 100644 --- a/lib/screens/home_page/editor_pane/url_card.dart +++ b/lib/screens/home_page/editor_pane/url_card.dart @@ -1,3 +1,4 @@ +import 'package:apidash/models/models.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:apidash/providers/providers.dart'; @@ -72,14 +73,26 @@ class URLTextField extends ConsumerWidget { final selectedId = ref.watch(selectedIdStateProvider); return URLField( selectedId: selectedId!, - initialValue: ref - .read(collectionStateNotifierProvider.notifier) - .getRequestModel(selectedId) - ?.url, + initialValue: ref.watch(selectedRequestModelProvider)?.url, onChanged: (value) { - ref - .read(collectionStateNotifierProvider.notifier) - .update(selectedId, url: value); + final uri = Uri.parse(value); + // Update requestParams if query parameters exist + if (uri.queryParameters.isNotEmpty) { + final updatedParams = uri.queryParametersAll.entries + .map((entry) => NameValueModel( + name: entry.key, + value: entry.value.isNotEmpty ? entry.value.join(',') : "")) + .toList(); + + ref + .read(collectionStateNotifierProvider.notifier) + .update(selectedId, url: value, requestParams: updatedParams); + } else { + ref.read(collectionStateNotifierProvider.notifier).update( + selectedId, + url: value, + ); + } }, ); } From 9a4f302a81dc79bed992bc81575ca61ace13c8f5 Mon Sep 17 00:00:00 2001 From: Tanish2002 Date: Tue, 27 Feb 2024 16:51:40 +0530 Subject: [PATCH 2/2] feat(param url sync): add reverse sync --- .../request_pane/request_params.dart | 19 +++++++++++++++---- .../home_page/editor_pane/url_card.dart | 17 ++++++++++------- lib/widgets/textfields.dart | 5 ++++- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/lib/screens/home_page/editor_pane/details_card/request_pane/request_params.dart b/lib/screens/home_page/editor_pane/details_card/request_pane/request_params.dart index 01005df6..b343cc8a 100644 --- a/lib/screens/home_page/editor_pane/details_card/request_pane/request_params.dart +++ b/lib/screens/home_page/editor_pane/details_card/request_pane/request_params.dart @@ -28,8 +28,18 @@ class EditRequestURLParamsState extends ConsumerState { } void _onFieldChange(String selectedId) { + final collection = ref.watch(selectedRequestModelProvider); + final uri = Uri.parse(collection!.url); + + final queryMap = { + for (var i = 0; i < rows.length; i++) + if (isRowEnabledList[i]) rows[i].name: rows[i].value + }; + var updatedUri = + uri.replace(queryParameters: rows.isEmpty ? null : queryMap); ref.read(collectionStateNotifierProvider.notifier).update( selectedId, + url: updatedUri.toString(), requestParams: rows, isParamEnabledList: isRowEnabledList, ); @@ -40,7 +50,8 @@ class EditRequestURLParamsState extends ConsumerState { final selectedId = ref.watch(selectedIdStateProvider); final length = ref.watch(selectedRequestModelProvider .select((value) => value?.requestParams?.length)); - var rP = ref.read(selectedRequestModelProvider)?.requestParams; + var rP = ref.watch(selectedRequestModelProvider)?.requestParams; + var url = ref.watch(selectedRequestModelProvider)?.url; rows = (rP == null || rP.isEmpty) ? [ kNameValueEmptyModel, @@ -59,7 +70,7 @@ class EditRequestURLParamsState extends ConsumerState { cellBuilder: (_, row) { int idx = row.index; return CheckBox( - keyId: "$selectedId-$idx-params-c-$seed", + keyId: "$selectedId-$idx-params-c-$seed-$url", value: isRowEnabledList[idx], onChanged: (value) { setState(() { @@ -78,7 +89,7 @@ class EditRequestURLParamsState extends ConsumerState { cellBuilder: (_, row) { int idx = row.index; return CellField( - keyId: "$selectedId-$idx-params-k-$seed", + keyId: "$selectedId-$idx-params-k-$seed-$url", initialValue: rows[idx].name, hintText: "Add URL Parameter", onChanged: (value) { @@ -105,7 +116,7 @@ class EditRequestURLParamsState extends ConsumerState { cellBuilder: (_, row) { int idx = row.index; return CellField( - keyId: "$selectedId-$idx-params-v-$seed", + keyId: "$selectedId-$idx-params-v-$seed-$url", initialValue: rows[idx].value, hintText: "Add Value", onChanged: (value) { diff --git a/lib/screens/home_page/editor_pane/url_card.dart b/lib/screens/home_page/editor_pane/url_card.dart index 72824385..4c04d0de 100644 --- a/lib/screens/home_page/editor_pane/url_card.dart +++ b/lib/screens/home_page/editor_pane/url_card.dart @@ -73,6 +73,7 @@ class URLTextField extends ConsumerWidget { final selectedId = ref.watch(selectedIdStateProvider); return URLField( selectedId: selectedId!, + requestParams: ref.watch(selectedRequestModelProvider)?.requestParams, initialValue: ref.watch(selectedRequestModelProvider)?.url, onChanged: (value) { final uri = Uri.parse(value); @@ -84,14 +85,16 @@ class URLTextField extends ConsumerWidget { value: entry.value.isNotEmpty ? entry.value.join(',') : "")) .toList(); - ref - .read(collectionStateNotifierProvider.notifier) - .update(selectedId, url: value, requestParams: updatedParams); + ref.read(collectionStateNotifierProvider.notifier).update(selectedId, + url: value, + requestParams: updatedParams, + isParamEnabledList: + List.filled(updatedParams.length, true, growable: true)); } else { - ref.read(collectionStateNotifierProvider.notifier).update( - selectedId, - url: value, - ); + ref.read(collectionStateNotifierProvider.notifier).update(selectedId, + url: value, + requestParams: [kNameValueEmptyModel], + isParamEnabledList: List.filled(1, true, growable: true)); } }, ); diff --git a/lib/widgets/textfields.dart b/lib/widgets/textfields.dart index c288b0a6..78c11972 100644 --- a/lib/widgets/textfields.dart +++ b/lib/widgets/textfields.dart @@ -1,3 +1,4 @@ +import 'package:apidash/models/name_value_model.dart'; import 'package:flutter/material.dart'; import 'package:apidash/consts.dart'; @@ -7,16 +8,18 @@ class URLField extends StatelessWidget { required this.selectedId, this.initialValue, this.onChanged, + this.requestParams, }); final String selectedId; final String? initialValue; final void Function(String)? onChanged; + final List? requestParams; @override Widget build(BuildContext context) { return TextFormField( - key: Key("url-$selectedId"), + key: Key("url-$selectedId-$requestParams"), initialValue: initialValue, style: kCodeStyle, decoration: InputDecoration(