web/elements: only render form once instance is loaded (#5049)
* web/elements: only render form once instance is loaded Signed-off-by: Jens Langhammer <jens@goauthentik.io> * use radio for transport Signed-off-by: Jens Langhammer <jens@goauthentik.io> * only wait for instance to be loaded if set Signed-off-by: Jens Langhammer <jens@goauthentik.io> * add hook to load additional data in form Signed-off-by: Jens Langhammer <jens@goauthentik.io> * make send an abstract function instead of attribute Signed-off-by: Jens Langhammer <jens@goauthentik.io> * ensure form is updated after data is loaded Signed-off-by: Jens Langhammer <jens@goauthentik.io> * remove until for select and multi-selects in forms Signed-off-by: Jens Langhammer <jens@goauthentik.io> * don't use until for file uploads Signed-off-by: Jens Langhammer <jens@goauthentik.io> * remove last until from form Signed-off-by: Jens Langhammer <jens@goauthentik.io> * remove deprecated import Signed-off-by: Jens Langhammer <jens@goauthentik.io> * prevent form double load, add error handling for PreventFormSubmit Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix double creation of inner element in proxy form Signed-off-by: Jens Langhammer <jens@goauthentik.io> * make PreventFormSubmit work correctly Signed-off-by: Jens Langhammer <jens@goauthentik.io> --------- Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
		@ -9,7 +9,6 @@ import { t } from "@lingui/macro";
 | 
			
		||||
import { TemplateResult, html } from "lit";
 | 
			
		||||
import { customElement } from "lit/decorators.js";
 | 
			
		||||
import { ifDefined } from "lit/directives/if-defined.js";
 | 
			
		||||
import { until } from "lit/directives/until.js";
 | 
			
		||||
 | 
			
		||||
import {
 | 
			
		||||
    CoreApi,
 | 
			
		||||
@ -17,17 +16,26 @@ import {
 | 
			
		||||
    EventsApi,
 | 
			
		||||
    Group,
 | 
			
		||||
    NotificationRule,
 | 
			
		||||
    PaginatedNotificationTransportList,
 | 
			
		||||
    SeverityEnum,
 | 
			
		||||
} from "@goauthentik/api";
 | 
			
		||||
 | 
			
		||||
@customElement("ak-event-rule-form")
 | 
			
		||||
export class RuleForm extends ModelForm<NotificationRule, string> {
 | 
			
		||||
    eventTransports?: PaginatedNotificationTransportList;
 | 
			
		||||
 | 
			
		||||
    loadInstance(pk: string): Promise<NotificationRule> {
 | 
			
		||||
        return new EventsApi(DEFAULT_CONFIG).eventsRulesRetrieve({
 | 
			
		||||
            pbmUuid: pk,
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async load(): Promise<void> {
 | 
			
		||||
        this.eventTransports = await new EventsApi(DEFAULT_CONFIG).eventsTransportsList({
 | 
			
		||||
            ordering: "name",
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    getSuccessMessage(): string {
 | 
			
		||||
        if (this.instance) {
 | 
			
		||||
            return t`Successfully updated rule.`;
 | 
			
		||||
@ -86,28 +94,14 @@ export class RuleForm extends ModelForm<NotificationRule, string> {
 | 
			
		||||
            </ak-form-element-horizontal>
 | 
			
		||||
            <ak-form-element-horizontal label=${t`Transports`} ?required=${true} name="transports">
 | 
			
		||||
                <select class="pf-c-form-control" multiple>
 | 
			
		||||
                    ${until(
 | 
			
		||||
                        new EventsApi(DEFAULT_CONFIG)
 | 
			
		||||
                            .eventsTransportsList({
 | 
			
		||||
                                ordering: "name",
 | 
			
		||||
                            })
 | 
			
		||||
                            .then((transports) => {
 | 
			
		||||
                                return transports.results.map((transport) => {
 | 
			
		||||
                                    const selected = Array.from(
 | 
			
		||||
                                        this.instance?.transports || [],
 | 
			
		||||
                                    ).some((su) => {
 | 
			
		||||
                                        return su == transport.pk;
 | 
			
		||||
                                    });
 | 
			
		||||
                                    return html`<option
 | 
			
		||||
                                        value=${ifDefined(transport.pk)}
 | 
			
		||||
                                        ?selected=${selected}
 | 
			
		||||
                                    >
 | 
			
		||||
                                        ${transport.name}
 | 
			
		||||
                                    </option>`;
 | 
			
		||||
                                });
 | 
			
		||||
                            }),
 | 
			
		||||
                        html`<option>${t`Loading...`}</option>`,
 | 
			
		||||
                    )}
 | 
			
		||||
                    ${this.eventTransports?.results.map((transport) => {
 | 
			
		||||
                        const selected = Array.from(this.instance?.transports || []).some((su) => {
 | 
			
		||||
                            return su == transport.pk;
 | 
			
		||||
                        });
 | 
			
		||||
                        return html`<option value=${ifDefined(transport.pk)} ?selected=${selected}>
 | 
			
		||||
                            ${transport.name}
 | 
			
		||||
                        </option>`;
 | 
			
		||||
                    })}
 | 
			
		||||
                </select>
 | 
			
		||||
                <p class="pf-c-form__helper-text">
 | 
			
		||||
                    ${t`Select which transports should be used to notify the user. If none are selected, the notification will only be shown in the authentik UI.`}
 | 
			
		||||
@ -120,7 +114,7 @@ export class RuleForm extends ModelForm<NotificationRule, string> {
 | 
			
		||||
                <ak-radio
 | 
			
		||||
                    .options=${[
 | 
			
		||||
                        {
 | 
			
		||||
                            label: "Alert",
 | 
			
		||||
                            label: t`Alert`,
 | 
			
		||||
                            value: SeverityEnum.Alert,
 | 
			
		||||
                            default: true,
 | 
			
		||||
                        },
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,7 @@ import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
 | 
			
		||||
import { first } from "@goauthentik/common/utils";
 | 
			
		||||
import "@goauthentik/elements/forms/HorizontalFormElement";
 | 
			
		||||
import { ModelForm } from "@goauthentik/elements/forms/ModelForm";
 | 
			
		||||
import "@goauthentik/elements/forms/Radio";
 | 
			
		||||
import "@goauthentik/elements/forms/SearchSelect";
 | 
			
		||||
 | 
			
		||||
import { t } from "@lingui/macro";
 | 
			
		||||
@ -56,35 +57,6 @@ export class TransportForm extends ModelForm<NotificationTransport, string> {
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    renderTransportModes(): TemplateResult {
 | 
			
		||||
        return html`
 | 
			
		||||
            <option
 | 
			
		||||
                value=${NotificationTransportModeEnum.Local}
 | 
			
		||||
                ?selected=${this.instance?.mode === NotificationTransportModeEnum.Local}
 | 
			
		||||
            >
 | 
			
		||||
                ${t`Local (notifications will be created within authentik)`}
 | 
			
		||||
            </option>
 | 
			
		||||
            <option
 | 
			
		||||
                value=${NotificationTransportModeEnum.Email}
 | 
			
		||||
                ?selected=${this.instance?.mode === NotificationTransportModeEnum.Email}
 | 
			
		||||
            >
 | 
			
		||||
                ${t`Email`}
 | 
			
		||||
            </option>
 | 
			
		||||
            <option
 | 
			
		||||
                value=${NotificationTransportModeEnum.Webhook}
 | 
			
		||||
                ?selected=${this.instance?.mode === NotificationTransportModeEnum.Webhook}
 | 
			
		||||
            >
 | 
			
		||||
                ${t`Webhook (generic)`}
 | 
			
		||||
            </option>
 | 
			
		||||
            <option
 | 
			
		||||
                value=${NotificationTransportModeEnum.WebhookSlack}
 | 
			
		||||
                ?selected=${this.instance?.mode === NotificationTransportModeEnum.WebhookSlack}
 | 
			
		||||
            >
 | 
			
		||||
                ${t`Webhook (Slack/Discord)`}
 | 
			
		||||
            </option>
 | 
			
		||||
        `;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    onModeChange(mode: string | undefined): void {
 | 
			
		||||
        if (
 | 
			
		||||
            mode === NotificationTransportModeEnum.Webhook ||
 | 
			
		||||
@ -107,15 +79,32 @@ export class TransportForm extends ModelForm<NotificationTransport, string> {
 | 
			
		||||
                />
 | 
			
		||||
            </ak-form-element-horizontal>
 | 
			
		||||
            <ak-form-element-horizontal label=${t`Mode`} ?required=${true} name="mode">
 | 
			
		||||
                <select
 | 
			
		||||
                    class="pf-c-form-control"
 | 
			
		||||
                    @change=${(ev: Event) => {
 | 
			
		||||
                        const current = (ev.target as HTMLInputElement).value;
 | 
			
		||||
                        this.onModeChange(current);
 | 
			
		||||
                <ak-radio
 | 
			
		||||
                    @change=${(ev: CustomEvent<NotificationTransportModeEnum>) => {
 | 
			
		||||
                        this.onModeChange(ev.detail);
 | 
			
		||||
                    }}
 | 
			
		||||
                    .options=${[
 | 
			
		||||
                        {
 | 
			
		||||
                            label: t`Local (notifications will be created within authentik)`,
 | 
			
		||||
                            value: NotificationTransportModeEnum.Local,
 | 
			
		||||
                            default: true,
 | 
			
		||||
                        },
 | 
			
		||||
                        {
 | 
			
		||||
                            label: t`Email`,
 | 
			
		||||
                            value: NotificationTransportModeEnum.Email,
 | 
			
		||||
                        },
 | 
			
		||||
                        {
 | 
			
		||||
                            label: t`Webhook (generic)`,
 | 
			
		||||
                            value: NotificationTransportModeEnum.Webhook,
 | 
			
		||||
                        },
 | 
			
		||||
                        {
 | 
			
		||||
                            label: t`Webhook (Slack/Discord)`,
 | 
			
		||||
                            value: NotificationTransportModeEnum.WebhookSlack,
 | 
			
		||||
                        },
 | 
			
		||||
                    ]}
 | 
			
		||||
                    .value=${this.instance?.mode}
 | 
			
		||||
                >
 | 
			
		||||
                    ${this.renderTransportModes()}
 | 
			
		||||
                </select>
 | 
			
		||||
                </ak-radio>
 | 
			
		||||
            </ak-form-element-horizontal>
 | 
			
		||||
            <ak-form-element-horizontal
 | 
			
		||||
                ?hidden=${!this.showWebhook}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user