2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-06-12 02:05:29 +00:00

API forms return the JSON data to the onSuccess function

This commit is contained in:
Oliver
2021-08-10 14:33:08 +10:00
parent 67ccbf64b5
commit 6d0282519d
9 changed files with 107 additions and 31 deletions

View File

@ -7,6 +7,7 @@ import 'package:inventree/api.dart';
import 'package:inventree/app_colors.dart';
import 'package:inventree/inventree/part.dart';
import 'package:inventree/inventree/stock.dart';
import 'package:inventree/widget/dialogs.dart';
import 'package:inventree/widget/fields.dart';
import 'package:inventree/l10.dart';
@ -420,7 +421,7 @@ Map<String, dynamic> extractFields(APIResponse response) {
* @param method is the HTTP method to use to send the form data to the server (e.g. POST / PATCH)
*/
Future<void> launchApiForm(BuildContext context, String title, String url, Map<String, dynamic> fields, {Map<String, dynamic> modelData = const {}, String method = "PATCH", Function? onSuccess, Function? onCancel}) async {
Future<void> launchApiForm(BuildContext context, String title, String url, Map<String, dynamic> fields, {Map<String, dynamic> modelData = const {}, String method = "PATCH", Function(Map<String, dynamic>)? onSuccess, Function? onCancel}) async {
var options = await InvenTreeAPI().options(url);
@ -503,6 +504,7 @@ Future<void> launchApiForm(BuildContext context, String title, String url, Map<S
title,
url,
formFields,
method,
onSuccess: onSuccess,
))
);
@ -517,14 +519,18 @@ class APIFormWidget extends StatefulWidget {
//! API URL
final String url;
//! API method
final String method;
final List<APIFormField> fields;
Function? onSuccess;
Function(Map<String, dynamic>)? onSuccess;
APIFormWidget(
this.title,
this.url,
this.fields,
this.method,
{
Key? key,
this.onSuccess,
@ -532,7 +538,7 @@ class APIFormWidget extends StatefulWidget {
) : super(key: key);
@override
_APIFormWidgetState createState() => _APIFormWidgetState(title, url, fields, onSuccess);
_APIFormWidgetState createState() => _APIFormWidgetState(title, url, fields, method, onSuccess);
}
@ -545,11 +551,13 @@ class _APIFormWidgetState extends State<APIFormWidget> {
String url;
String method;
List<APIFormField> fields;
Function? onSuccess;
Function(Map<String, dynamic>)? onSuccess;
_APIFormWidgetState(this.title, this.url, this.fields, this.onSuccess) : super();
_APIFormWidgetState(this.title, this.url, this.fields, this.method, this.onSuccess) : super();
List<Widget> _buildForm() {
@ -584,30 +592,42 @@ class _APIFormWidgetState extends State<APIFormWidget> {
return widgets;
}
Future<APIResponse> _submit(Map<String, String> data) async {
if (method == "POST") {
return await InvenTreeAPI().post(
url,
body: data
);
} else {
return await InvenTreeAPI().patch(
url,
body: data,
);
}
}
Future<void> _save(BuildContext context) async {
// Package up the form data
Map<String, String> _data = {};
Map<String, String> data = {};
for (var field in fields) {
dynamic value = field.value;
if (value == null) {
_data[field.name] = "";
data[field.name] = "";
} else {
_data[field.name] = value.toString();
data[field.name] = value.toString();
}
}
// TODO: Handle "POST" forms too!!
final response = await InvenTreeAPI().patch(
url,
body: _data,
);
final response = await _submit(data);
if (!response.isValid()) {
// TODO: Display an error message!
showServerError(L10().serverError, L10().responseInvalid);
return;
}
@ -625,7 +645,17 @@ class _APIFormWidgetState extends State<APIFormWidget> {
var successFunc = onSuccess;
if (successFunc != null) {
successFunc();
// Ensure the response is a valid JSON structure
Map<String, dynamic> json = {};
if (response.data != null && response.data is Map) {
for (dynamic key in response.data.keys) {
json[key.toString()] = response.data[key];
}
}
successFunc(json);
}
return;
case 400: