mirror of
https://github.com/inventree/inventree-app.git
synced 2026-11-27 15:59:09 +00:00
API form fixes (#937)
* API form fixes - Await form completion - Improved error code handling - Additional testing * improve error message display in forms * add text
This commit is contained in:
1 parent
e9601116b4
commit
ed9590cddf
8 files changed
+585
-31
No files matched your search
+28
-11
@@ -748,7 +748,8 @@ class InvenTreeAPI {
|
||||
|
||||
debug("Connecting to ${apiUrl}");
|
||||
|
||||
APIResponse response = await get(url, expectedStatusCode: 200);
|
||||
// Note: Unexpected status codes are reported below
|
||||
APIResponse response = await get(url);
|
||||
|
||||
if (!response.successful()) {
|
||||
debug("Server returned invalid response: ${response.statusCode}");
|
||||
@@ -856,10 +857,16 @@ class InvenTreeAPI {
|
||||
_globalSettings.clear();
|
||||
_userSettings.clear();
|
||||
|
||||
// Clear data received from the server
|
||||
// (so it is not shown while connecting to a different server)
|
||||
roles.clear();
|
||||
permissions.clear();
|
||||
_plugins.clear();
|
||||
serverInfo.clear();
|
||||
userInfo = {};
|
||||
notification_counter = 0;
|
||||
clearStatusCodeData();
|
||||
|
||||
_connectionStatusChanged();
|
||||
}
|
||||
|
||||
@@ -929,6 +936,7 @@ class InvenTreeAPI {
|
||||
*/
|
||||
Future<bool> _fetchRoles() async {
|
||||
roles.clear();
|
||||
permissions.clear();
|
||||
|
||||
debug("API: Requesting user role data");
|
||||
String actualRoleUrl = supportsNewUserEndpoints
|
||||
@@ -1906,7 +1914,9 @@ class InvenTreeAPI {
|
||||
String url, {
|
||||
Map<String, String> params = const {},
|
||||
Map<String, String> headers = const {},
|
||||
int? expectedStatusCode = 200,
|
||||
// If provided, the user is alerted when a different status code is returned
|
||||
// (by default, callers handle unexpected responses, e.g. 404, themselves)
|
||||
int? expectedStatusCode,
|
||||
}) async {
|
||||
HttpClientRequest? request = await apiRequest(
|
||||
url,
|
||||
@@ -1924,7 +1934,7 @@ class InvenTreeAPI {
|
||||
);
|
||||
}
|
||||
|
||||
return completeRequest(request);
|
||||
return completeRequest(request, statusCode: expectedStatusCode);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -2186,9 +2196,14 @@ class InvenTreeAPI {
|
||||
fields,
|
||||
icon: TablerIcons.location_search,
|
||||
onSuccess: (Map<String, dynamic> data) async {
|
||||
plugin_name = (data["plugin"] ?? "") as String;
|
||||
plugin_name = (data["plugin"] ?? "").toString();
|
||||
},
|
||||
);
|
||||
|
||||
// Form was cancelled (no plugin selected)
|
||||
if (plugin_name.isEmpty) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> body = {"plugin": plugin_name};
|
||||
@@ -2201,13 +2216,15 @@ class InvenTreeAPI {
|
||||
body["location"] = location.toString();
|
||||
}
|
||||
|
||||
post("locate/", body: body, expectedStatusCode: 200).then((
|
||||
APIResponse response,
|
||||
) {
|
||||
if (response.successful()) {
|
||||
showSnackIcon(L10().requestSuccessful, success: true);
|
||||
}
|
||||
});
|
||||
final APIResponse response = await post(
|
||||
"locate/",
|
||||
body: body,
|
||||
expectedStatusCode: 200,
|
||||
);
|
||||
|
||||
if (response.successful()) {
|
||||
showSnackIcon(L10().requestSuccessful, success: true);
|
||||
}
|
||||
}
|
||||
|
||||
// Keep an internal map of status codes
|
||||
|
||||
+32
-9
@@ -1123,6 +1123,11 @@ Map<String, dynamic> formResponseData(APIResponse response) {
|
||||
return Map<String, dynamic>.from(response.asMap());
|
||||
}
|
||||
|
||||
/*
|
||||
* Launch an API form.
|
||||
* The returned future completes once the form is closed
|
||||
* (after onSuccess has been called, if the form was submitted).
|
||||
*/
|
||||
Future<void> launchApiForm(
|
||||
BuildContext context,
|
||||
String title,
|
||||
@@ -1208,7 +1213,9 @@ Future<void> launchApiForm(
|
||||
hideLoadingOverlay();
|
||||
|
||||
// Now, launch a new widget!
|
||||
Navigator.push(
|
||||
// Wait until the form is closed (submitted or cancelled),
|
||||
// so that callers can await the outcome of the form
|
||||
await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => APIFormWidget(
|
||||
@@ -1371,6 +1378,21 @@ class APIFormWidgetState extends State<APIFormWidget> {
|
||||
return widgets;
|
||||
}
|
||||
|
||||
/*
|
||||
* Notify the user that the form submission failed.
|
||||
* Tapping the info icon displays the full response from the server.
|
||||
*/
|
||||
void showErrorSnack(String message, APIResponse response) {
|
||||
showSnackIcon(
|
||||
message,
|
||||
success: false,
|
||||
icon: TablerIcons.info_circle,
|
||||
onAction: () {
|
||||
showResponseDataDialog(message, response);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<APIResponse> _submit(Map<String, dynamic> data) async {
|
||||
// If a file upload is required, we have to handle the submission differently
|
||||
if (widget.fileField.isNotEmpty) {
|
||||
@@ -1577,7 +1599,8 @@ class APIFormWidgetState extends State<APIFormWidget> {
|
||||
return;
|
||||
case 400:
|
||||
// Form submission / validation error
|
||||
showSnackIcon(L10().formError, success: false);
|
||||
print("error: ${response.toString()}");
|
||||
showErrorSnack(L10().formError, response);
|
||||
|
||||
// Update field errors
|
||||
for (var field in formFields) {
|
||||
@@ -1587,19 +1610,19 @@ class APIFormWidgetState extends State<APIFormWidget> {
|
||||
extractNonFieldErrors(response);
|
||||
checkInvalidErrors(response);
|
||||
case 401:
|
||||
showSnackIcon("401: " + L10().response401, success: false);
|
||||
showErrorSnack("401: " + L10().response401, response);
|
||||
case 403:
|
||||
showSnackIcon("403: " + L10().response403, success: false);
|
||||
showErrorSnack("403: " + L10().response403, response);
|
||||
case 404:
|
||||
showSnackIcon("404: " + L10().response404, success: false);
|
||||
showErrorSnack("404: " + L10().response404, response);
|
||||
case 405:
|
||||
showSnackIcon("405: " + L10().response405, success: false);
|
||||
showErrorSnack("405: " + L10().response405, response);
|
||||
case 500:
|
||||
showSnackIcon("500: " + L10().response500, success: false);
|
||||
showErrorSnack("500: " + L10().response500, response);
|
||||
default:
|
||||
showSnackIcon(
|
||||
showErrorSnack(
|
||||
"${response.statusCode}: " + L10().responseInvalid,
|
||||
success: false,
|
||||
response,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -550,6 +550,12 @@
|
||||
"destroyed": "Destroyed",
|
||||
"@destroyed": {},
|
||||
|
||||
"copiedToClipboard": "Copied to clipboard",
|
||||
"@copiedToClipboard": {},
|
||||
|
||||
"copyToClipboard": "Copy",
|
||||
"@copyToClipboard": {},
|
||||
|
||||
"details": "Details",
|
||||
"@details": {
|
||||
"description": "details"
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import "dart:convert";
|
||||
|
||||
import "package:flutter/material.dart";
|
||||
import "package:flutter/services.dart";
|
||||
import "package:flutter_tabler_icons/flutter_tabler_icons.dart";
|
||||
import "package:one_context/one_context.dart";
|
||||
|
||||
@@ -8,6 +11,7 @@ import "package:inventree/helpers.dart";
|
||||
import "package:inventree/l10.dart";
|
||||
|
||||
import "package:inventree/preferences.dart";
|
||||
import "package:inventree/widget/snacks.dart";
|
||||
|
||||
/*
|
||||
* Launch a dialog allowing the user to select from a list of options
|
||||
@@ -290,6 +294,87 @@ Future<void> showErrorDialog(
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* Format the data returned in an API response for display
|
||||
* (JSON data is pretty-printed)
|
||||
*/
|
||||
String formatResponseData(dynamic data) {
|
||||
if (data is Map || data is List) {
|
||||
try {
|
||||
return const JsonEncoder.withIndent(" ").convert(data);
|
||||
} catch (_) {
|
||||
// Fall through to the string representation
|
||||
}
|
||||
}
|
||||
|
||||
return data?.toString() ?? "";
|
||||
}
|
||||
|
||||
/*
|
||||
* Construct a view of the full data returned in an API response
|
||||
*/
|
||||
Widget buildResponseDataContent(APIResponse response) {
|
||||
return ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 400),
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"${L10().statusCode}: ${response.statusCode}",
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
if (response.url.isNotEmpty) Text(response.url),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
L10().responseData,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
SelectableText(
|
||||
formatResponseData(response.data),
|
||||
style: const TextStyle(fontFamily: "monospace", fontSize: 12),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Display the full data returned in an API response (e.g. form errors)
|
||||
*/
|
||||
Future<void> showResponseDataDialog(String title, APIResponse response) async {
|
||||
if (!hasContext()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final String data = formatResponseData(response.data);
|
||||
|
||||
OneContext().showDialog(
|
||||
builder: (context) => AlertDialog(
|
||||
icon: Icon(TablerIcons.info_circle, color: COLOR_DANGER),
|
||||
title: Text(title),
|
||||
content: buildResponseDataContent(response),
|
||||
actions: [
|
||||
TextButton(
|
||||
child: Text(L10().copyToClipboard),
|
||||
onPressed: () {
|
||||
Clipboard.setData(ClipboardData(text: data));
|
||||
showSnackIcon(L10().copiedToClipboard, success: true);
|
||||
},
|
||||
),
|
||||
TextButton(
|
||||
child: Text(L10().ok),
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Display a message indicating the nature of a server / API error
|
||||
*/
|
||||
|
||||
+36
-11
@@ -12,6 +12,9 @@ OverlayEntry? _currentSnackOverlay;
|
||||
|
||||
/*
|
||||
* Display a configurable 'snackbar' at the top of the screen
|
||||
*
|
||||
* If both [icon] and [onAction] are provided, tapping the icon runs the action
|
||||
* (and the text action button is only shown if [actionText] is provided)
|
||||
*/
|
||||
void showSnackIcon(
|
||||
String text, {
|
||||
@@ -50,17 +53,22 @@ void showSnackIcon(
|
||||
}
|
||||
}
|
||||
|
||||
final String actionLabel = actionText ?? L10().details;
|
||||
// An action icon replaces the default text action button
|
||||
final bool iconAction = onAction != null && icon != null;
|
||||
final String? actionLabel = (iconAction && actionText == null)
|
||||
? null
|
||||
: actionText ?? L10().details;
|
||||
final Duration duration = Duration(seconds: onAction == null ? 5 : 10);
|
||||
|
||||
late OverlayEntry entry;
|
||||
entry = OverlayEntry(
|
||||
builder: (ctx) => _TopSnackBar(
|
||||
builder: (ctx) => TopSnackBar(
|
||||
text: text,
|
||||
icon: icon,
|
||||
backgroundColor: backgroundColor,
|
||||
onAction: onAction,
|
||||
actionText: actionLabel,
|
||||
iconAction: iconAction,
|
||||
duration: duration,
|
||||
onDismiss: () {
|
||||
entry.remove();
|
||||
@@ -75,8 +83,12 @@ void showSnackIcon(
|
||||
Overlay.of(context).insert(entry);
|
||||
}
|
||||
|
||||
class _TopSnackBar extends StatefulWidget {
|
||||
const _TopSnackBar({
|
||||
/*
|
||||
* Snackbar widget displayed by showSnackIcon()
|
||||
* (public for testing, as snackbars are not displayed in tests)
|
||||
*/
|
||||
class TopSnackBar extends StatefulWidget {
|
||||
const TopSnackBar({
|
||||
required this.text,
|
||||
required this.backgroundColor,
|
||||
required this.duration,
|
||||
@@ -84,6 +96,7 @@ class _TopSnackBar extends StatefulWidget {
|
||||
this.icon,
|
||||
this.onAction,
|
||||
this.actionText,
|
||||
this.iconAction = false,
|
||||
});
|
||||
|
||||
final String text;
|
||||
@@ -91,6 +104,10 @@ class _TopSnackBar extends StatefulWidget {
|
||||
final Color backgroundColor;
|
||||
final Function()? onAction;
|
||||
final String? actionText;
|
||||
|
||||
// Tapping the icon runs the action
|
||||
final bool iconAction;
|
||||
|
||||
final Duration duration;
|
||||
final VoidCallback onDismiss;
|
||||
|
||||
@@ -98,7 +115,7 @@ class _TopSnackBar extends StatefulWidget {
|
||||
_TopSnackBarState createState() => _TopSnackBarState();
|
||||
}
|
||||
|
||||
class _TopSnackBarState extends State<_TopSnackBar>
|
||||
class _TopSnackBarState extends State<TopSnackBar>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late final AnimationController _controller;
|
||||
late final Animation<Offset> _slideAnimation;
|
||||
@@ -130,6 +147,11 @@ class _TopSnackBarState extends State<_TopSnackBar>
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _runAction() {
|
||||
_dismiss();
|
||||
widget.onAction?.call();
|
||||
}
|
||||
|
||||
void _dismiss() {
|
||||
if (_dismissed) return;
|
||||
_dismissed = true;
|
||||
@@ -170,7 +192,7 @@ class _TopSnackBarState extends State<_TopSnackBar>
|
||||
style: const TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
if (widget.icon != null)
|
||||
if (widget.icon != null && !widget.iconAction)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 8),
|
||||
child: Icon(widget.icon, color: Colors.white),
|
||||
@@ -179,12 +201,15 @@ class _TopSnackBarState extends State<_TopSnackBar>
|
||||
),
|
||||
),
|
||||
),
|
||||
if (widget.onAction != null)
|
||||
if (widget.iconAction)
|
||||
IconButton(
|
||||
icon: Icon(widget.icon, color: Colors.white),
|
||||
tooltip: widget.actionText ?? L10().details,
|
||||
onPressed: _runAction,
|
||||
),
|
||||
if (widget.onAction != null && widget.actionText != null)
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
_dismiss();
|
||||
widget.onAction!();
|
||||
},
|
||||
onPressed: _runAction,
|
||||
child: Text(
|
||||
widget.actionText ?? "",
|
||||
style: const TextStyle(color: Colors.white),
|
||||
|
||||
Reference in new issue
Block a user