2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-28 13:36:50 +00:00

Enhance existing api_forms functionality

- Allow form to be created without an actual API endpoint
- Useful for creating client-side forms without any server interaction
This commit is contained in:
Oliver Walters 2022-03-25 23:36:56 +11:00
parent 63bf88ac66
commit 02a9bbe3c6
3 changed files with 47 additions and 25 deletions

View File

@ -258,6 +258,9 @@ class InvenTreeAPI {
return plugins; return plugins;
} }
// Test if the provided plugin mixin is supported by any active plugins
bool supportsMixin(String mixin) => getPlugins(mixin: mixin).isNotEmpty;
// Getter for server version information // Getter for server version information
String get version => _version; String get version => _version;
@ -511,6 +514,8 @@ class InvenTreeAPI {
return; return;
} }
print("Requesting plugin information");
// Request a list of plugins from the server // Request a list of plugins from the server
final List<InvenTreeModel> results = await InvenTreePlugin().list(); final List<InvenTreeModel> results = await InvenTreePlugin().list();
@ -522,11 +527,8 @@ class InvenTreeAPI {
} }
} }
} }
print("Discovered ${_plugins.length} active plugins!");
} }
bool checkPermission(String role, String permission) { bool checkPermission(String role, String permission) {
/* /*
* Check if the user has the given role.permission assigned * Check if the user has the given role.permission assigned

View File

@ -778,8 +778,6 @@ Map<String, dynamic> extractFieldDefinition(Map<String, dynamic> data, String lo
String el = path.last; String el = path.last;
if (!_data.containsKey(el)) { if (!_data.containsKey(el)) {
print("Could not find field definition for ${lookup}");
print("- Final field path ${el} missing from data");
return {}; return {};
} else { } else {
@ -824,6 +822,10 @@ Future<void> launchApiForm(
IconData icon = FontAwesomeIcons.save, IconData icon = FontAwesomeIcons.save,
}) async { }) async {
// List of fields defined by the server
Map<String, dynamic> serverFields = {};
if (url.isNotEmpty) {
var options = await InvenTreeAPI().options(url); var options = await InvenTreeAPI().options(url);
// Invalid response from server // Invalid response from server
@ -831,8 +833,7 @@ Future<void> launchApiForm(
return; return;
} }
// List of fields defined by the server serverFields = extractFields(options);
Map<String, dynamic> serverFields = extractFields(options);
if (serverFields.isEmpty) { if (serverFields.isEmpty) {
// User does not have permission to perform this action // User does not have permission to perform this action
@ -843,6 +844,7 @@ Future<void> launchApiForm(
return; return;
} }
}
// Construct a list of APIFormField objects // Construct a list of APIFormField objects
List<APIFormField> formFields = []; List<APIFormField> formFields = [];
@ -868,8 +870,7 @@ Future<void> launchApiForm(
// Skip fields with empty definitions // Skip fields with empty definitions
if (field.definition.isEmpty) { if (field.definition.isEmpty) {
print("ERROR: Empty field definition for field '${fieldName}'"); print("Warning: Empty field definition for field '${fieldName}'");
continue;
} }
// Add instance value to the field // Add instance value to the field
@ -1170,6 +1171,23 @@ class _APIFormWidgetState extends State<APIFormWidget> {
} }
} }
// Run custom onSuccess function
var successFunc = onSuccess;
// An "empty" URL means we don't want to submit the form anywhere
// Perhaps we just want to process the data?
if (url.isEmpty) {
// Hide the form
Navigator.pop(context);
if (successFunc != null) {
// Return the raw "submitted" data, rather than the server response
successFunc(data);
}
return;
}
final response = await _submit(data); final response = await _submit(data);
if (!response.isValid()) { if (!response.isValid()) {
@ -1187,9 +1205,6 @@ class _APIFormWidgetState extends State<APIFormWidget> {
// TODO: Display a snackBar // TODO: Display a snackBar
// Run custom onSuccess function
var successFunc = onSuccess;
if (successFunc != null) { if (successFunc != null) {
// Ensure the response is a valid JSON structure // Ensure the response is a valid JSON structure

View File

@ -124,6 +124,11 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> {
// Clear the existing labels list // Clear the existing labels list
labels.clear(); labels.clear();
// If the server does not support label printing, don't bother!
if (!InvenTreeAPI().supportsMixin("labels")) {
return;
}
InvenTreeAPI().get( InvenTreeAPI().get(
"/label/stock/", "/label/stock/",
params: { params: {