resolved issue #178(switch from GET->POST if payload exists)

This commit is contained in:
Jeet Dalal
2024-08-11 13:40:47 +05:30
parent 448a88232b
commit fcc940ec2e
6 changed files with 124 additions and 5 deletions

View File

@ -1,3 +1,6 @@
import 'dart:developer';
import 'package:apidash/models/models.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:apidash/providers/providers.dart';
@ -17,6 +20,25 @@ class EditRequestBody extends ConsumerWidget {
final contentType = ref.watch(selectedRequestModelProvider
.select((value) => value?.httpRequestModel?.bodyContentType));
void changeToPostMethod() {
RequestModel? model = ref
.read(collectionStateNotifierProvider.notifier)
.getRequestModel(selectedId);
if (model!.httpRequestModel!.method == HTTPVerb.get) {
ref
.read(collectionStateNotifierProvider.notifier)
.update(selectedId, method: HTTPVerb.post);
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text(
"Switched to POST method",
style: TextStyle(color: Colors.white),
),
backgroundColor: Colors.black,
));
}
}
return Column(
children: [
const SizedBox(
@ -33,8 +55,11 @@ class EditRequestBody extends ConsumerWidget {
),
Expanded(
child: switch (contentType) {
ContentType.formdata =>
const Padding(padding: kPh4, child: FormDataWidget()),
ContentType.formdata => Padding(
padding: kPh4,
child: FormDataWidget(
changeMethodToPost: changeToPostMethod,
)),
// TODO: Fix JsonTextFieldEditor & plug it here
ContentType.json => Padding(
padding: kPt5o10,
@ -43,6 +68,7 @@ class EditRequestBody extends ConsumerWidget {
fieldKey: "$selectedId-json-body-editor",
initialValue: requestModel?.httpRequestModel?.body,
onChanged: (String value) {
changeToPostMethod();
ref
.read(collectionStateNotifierProvider.notifier)
.update(selectedId, body: value);
@ -56,6 +82,7 @@ class EditRequestBody extends ConsumerWidget {
fieldKey: "$selectedId-body-editor",
initialValue: requestModel?.httpRequestModel?.body,
onChanged: (String value) {
changeToPostMethod();
ref
.read(collectionStateNotifierProvider.notifier)
.update(selectedId, body: value);

View File

@ -9,7 +9,8 @@ import 'package:apidash/utils/utils.dart';
import 'package:apidash/consts.dart';
class FormDataWidget extends ConsumerStatefulWidget {
const FormDataWidget({super.key});
final Function changeMethodToPost;
const FormDataWidget({required this.changeMethodToPost, super.key});
@override
ConsumerState<FormDataWidget> createState() => _FormDataBodyState();
}
@ -27,6 +28,7 @@ class _FormDataBodyState extends ConsumerState<FormDataWidget> {
}
void _onFieldChange(String selectedId) {
widget.changeMethodToPost();
ref.read(collectionStateNotifierProvider.notifier).update(
selectedId,
formData: formRows.sublist(0, formRows.length - 1),
@ -41,6 +43,7 @@ class _FormDataBodyState extends ConsumerState<FormDataWidget> {
.select((value) => value?.httpRequestModel?.formData?.length));
var rF = ref.read(selectedRequestModelProvider)?.httpRequestModel?.formData;
bool isFormDataEmpty = rF == null || rF.isEmpty;
formRows = isFormDataEmpty
? [
kFormDataEmptyModel,