Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Implement Synchronization between the "URL Text Field" and "URL Parameters" #212

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ class EditRequestURLParamsState extends ConsumerState<EditRequestURLParams> {
}

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,
);
Expand All @@ -40,7 +50,8 @@ class EditRequestURLParamsState extends ConsumerState<EditRequestURLParams> {
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,
Expand All @@ -59,7 +70,7 @@ class EditRequestURLParamsState extends ConsumerState<EditRequestURLParams> {
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(() {
Expand All @@ -78,7 +89,7 @@ class EditRequestURLParamsState extends ConsumerState<EditRequestURLParams> {
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) {
Expand All @@ -105,7 +116,7 @@ class EditRequestURLParamsState extends ConsumerState<EditRequestURLParams> {
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) {
Expand Down
30 changes: 23 additions & 7 deletions lib/screens/home_page/editor_pane/url_card.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -72,14 +73,29 @@ class URLTextField extends ConsumerWidget {
final selectedId = ref.watch(selectedIdStateProvider);
return URLField(
selectedId: selectedId!,
initialValue: ref
.read(collectionStateNotifierProvider.notifier)
.getRequestModel(selectedId)
?.url,
requestParams: ref.watch(selectedRequestModelProvider)?.requestParams,
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,
isParamEnabledList:
List.filled(updatedParams.length, true, growable: true));
} else {
ref.read(collectionStateNotifierProvider.notifier).update(selectedId,
url: value,
requestParams: [kNameValueEmptyModel],
isParamEnabledList: List.filled(1, true, growable: true));
}
},
);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/widgets/textfields.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:apidash/models/name_value_model.dart';
import 'package:flutter/material.dart';
import 'package:apidash/consts.dart';

Expand All @@ -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<NameValueModel>? requestParams;

@override
Widget build(BuildContext context) {
return TextFormField(
key: Key("url-$selectedId"),
key: Key("url-$selectedId-$requestParams"),
initialValue: initialValue,
style: kCodeStyle,
decoration: InputDecoration(
Expand Down