web/admin: prompt preview (#5078)

* add initial prompt preview

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* improve error handling

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* don't flood api with requests when fields are changeed

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* add tests

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

---------

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
Jens L
2023-03-25 22:31:48 +01:00
committed by GitHub
parent d6fa19a97f
commit 6437fbc814
12 changed files with 406 additions and 55 deletions

View File

@ -12,6 +12,7 @@ import {
import * as yamlMode from "@codemirror/legacy-modes/mode/yaml";
import { Compartment, EditorState, Extension } from "@codemirror/state";
import { oneDark } from "@codemirror/theme-one-dark";
import { ViewUpdate } from "@codemirror/view";
import { EditorView, drawSelection, keymap, lineNumbers } from "@codemirror/view";
import { EVENT_THEME_CHANGE } from "@goauthentik/common/constants";
import { AKElement } from "@goauthentik/elements/Base";
@ -148,6 +149,16 @@ export class CodeMirrorTextarea<T> extends AKElement {
lineNumbers(),
drawSelection(),
EditorView.lineWrapping,
EditorView.updateListener.of((v: ViewUpdate) => {
if (!v.docChanged) {
return;
}
this.dispatchEvent(
new CustomEvent("change", {
detail: v,
}),
);
}),
EditorState.readOnly.of(this.readOnly),
EditorState.tabSize.of(2),
this.theme.of(this.activeTheme === UiThemeEnum.Dark ? this.themeDark : this.themeLight),

View File

@ -18,7 +18,7 @@ import PFInputGroup from "@patternfly/patternfly/components/InputGroup/input-gro
import PFSwitch from "@patternfly/patternfly/components/Switch/switch.css";
import PFBase from "@patternfly/patternfly/patternfly-base.css";
import { ResponseError, ValidationError } from "@goauthentik/api";
import { ResponseError, ValidationError, ValidationErrorFromJSON } from "@goauthentik/api";
export class PreventFormSubmit {
// Stub class which can be returned by form elements to prevent the form from submitting
@ -102,9 +102,6 @@ export abstract class Form<T> extends AKElement {
});
}
/**
* Reset the inner iron-form
*/
resetForm(): void {
const form = this.shadowRoot?.querySelector<HTMLFormElement>("form");
form?.reset();
@ -235,7 +232,7 @@ export abstract class Form<T> extends AKElement {
if (ex instanceof ResponseError) {
let msg = ex.response.statusText;
if (ex.response.status > 399 && ex.response.status < 500) {
const errorMessage: ValidationError = await ex.response.json();
const errorMessage = ValidationErrorFromJSON(await ex.response.json());
if (!errorMessage) return errorMessage;
if (errorMessage instanceof Error) {
throw errorMessage;
@ -257,8 +254,8 @@ export abstract class Form<T> extends AKElement {
element.invalid = false;
}
});
if ("non_field_errors" in errorMessage) {
this.nonFieldErrors = errorMessage["non_field_errors"];
if (errorMessage.nonFieldErrors) {
this.nonFieldErrors = errorMessage.nonFieldErrors;
}
// Only change the message when we have `detail`.
// Everything else is handled in the form.