mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-28 21:46:46 +00:00
Extract field information from the server
This commit is contained in:
parent
83e29777c2
commit
7291dc9272
76
lib/api_form.dart
Normal file
76
lib/api_form.dart
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:InvenTree/api.dart';
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Extract field options from a returned OPTIONS request
|
||||||
|
*/
|
||||||
|
Map<String, dynamic> extractFields(dynamic options) {
|
||||||
|
|
||||||
|
if (options == null) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!options.containsKey("actions")) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
var actions = options["actions"];
|
||||||
|
|
||||||
|
return actions["POST"] ?? actions["PUT"] ?? actions["PATCH"] ?? {};
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Launch an API-driven form,
|
||||||
|
* which uses the OPTIONS metadata (at the provided URL)
|
||||||
|
* to determine how the form elements should be rendered!
|
||||||
|
*/
|
||||||
|
|
||||||
|
Future<bool> launchApiForm(String url, Map<String, dynamic> fields, {String method = "PATCH"}) async {
|
||||||
|
|
||||||
|
dynamic options = await InvenTreeAPI().options(url);
|
||||||
|
|
||||||
|
// null response from server
|
||||||
|
if (options == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var availableFields = extractFields(options);
|
||||||
|
|
||||||
|
if (availableFields.isEmpty) {
|
||||||
|
print("Empty fields {} returned from ${url}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate through the provided fields we wish to display
|
||||||
|
for (String fieldName in fields.keys) {
|
||||||
|
|
||||||
|
// Check that the field is actually available at the API endpoint
|
||||||
|
if (!availableFields.containsKey(fieldName)) {
|
||||||
|
print("Field '${fieldName}' not available at '${url}'");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var remoteField = availableFields[fieldName] ?? {};
|
||||||
|
var localField = fields[fieldName] ?? {};
|
||||||
|
|
||||||
|
// Override defined field parameters, if provided
|
||||||
|
for (String key in localField.keys) {
|
||||||
|
// Special consideration
|
||||||
|
if (key == "filters") {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
String? val = localField[key];
|
||||||
|
|
||||||
|
if (val != null) {
|
||||||
|
remoteField[key] = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print("${fieldName} -> ${remoteField.toString()}");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
@ -126,8 +126,7 @@ class InvenTreeModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return the API detail endpoint for this Model object
|
// Return the API detail endpoint for this Model object
|
||||||
String get url => "${URL}/${pk}/";
|
String get url => "${URL}/${pk}/".replaceAll("//", "/");
|
||||||
|
|
||||||
|
|
||||||
// Search this Model type in the database
|
// Search this Model type in the database
|
||||||
Future<List<InvenTreeModel>> search(BuildContext context, String searchTerm, {Map<String, String> filters = const {}}) async {
|
Future<List<InvenTreeModel>> search(BuildContext context, String searchTerm, {Map<String, String> filters = const {}}) async {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user