2021-08-12 19:14:51 +00:00
|
|
|
import { t } from "@lingui/macro";
|
2021-09-21 09:19:26 +00:00
|
|
|
import { CSSResult, html, TemplateResult } from "lit";
|
|
|
|
import { customElement, property } from "lit/decorators";
|
2021-08-12 19:14:51 +00:00
|
|
|
import { EVENT_REFRESH } from "../../constants";
|
|
|
|
import { ModalButton } from "../buttons/ModalButton";
|
|
|
|
import { MessageLevel } from "../messages/Message";
|
|
|
|
import { showMessage } from "../messages/MessageContainer";
|
|
|
|
import "../buttons/SpinnerButton";
|
2021-08-15 19:32:28 +00:00
|
|
|
import { UsedBy, UsedByActionEnum } from "@goauthentik/api";
|
2021-08-12 19:14:51 +00:00
|
|
|
import PFList from "@patternfly/patternfly/components/List/list.css";
|
2021-09-21 09:19:26 +00:00
|
|
|
import { until } from "lit/directives/until";
|
2021-08-12 19:14:51 +00:00
|
|
|
import { Table, TableColumn } from "../table/Table";
|
|
|
|
import { AKResponse } from "../../api/Client";
|
|
|
|
import { PFSize } from "../Spinner";
|
|
|
|
|
2021-08-12 20:03:13 +00:00
|
|
|
type BulkDeleteMetadata = { key: string; value: string }[];
|
2021-08-12 19:14:51 +00:00
|
|
|
|
|
|
|
@customElement("ak-delete-objects-table")
|
2021-08-12 20:03:13 +00:00
|
|
|
export class DeleteObjectsTable<T> extends Table<T> {
|
2021-08-12 19:14:51 +00:00
|
|
|
expandable = true;
|
|
|
|
paginated = false;
|
|
|
|
|
|
|
|
@property({ attribute: false })
|
2021-08-12 20:03:13 +00:00
|
|
|
objects: T[] = [];
|
|
|
|
|
|
|
|
@property({ attribute: false })
|
|
|
|
metadata!: (item: T) => BulkDeleteMetadata;
|
2021-08-12 19:14:51 +00:00
|
|
|
|
|
|
|
@property({ attribute: false })
|
2021-08-12 20:03:13 +00:00
|
|
|
usedBy?: (item: T) => Promise<UsedBy[]>;
|
2021-08-12 19:14:51 +00:00
|
|
|
|
|
|
|
static get styles(): CSSResult[] {
|
|
|
|
return super.styles.concat(PFList);
|
|
|
|
}
|
|
|
|
|
2021-08-12 20:03:13 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
apiEndpoint(page: number): Promise<AKResponse<T>> {
|
2021-08-12 19:14:51 +00:00
|
|
|
return Promise.resolve({
|
|
|
|
pagination: {
|
|
|
|
count: this.objects.length,
|
|
|
|
current: 1,
|
|
|
|
totalPages: 1,
|
|
|
|
startIndex: 1,
|
|
|
|
endIndex: this.objects.length,
|
|
|
|
},
|
|
|
|
results: this.objects,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
columns(): TableColumn[] {
|
2021-08-12 20:03:13 +00:00
|
|
|
return this.metadata(this.objects[0]).map((element) => {
|
|
|
|
return new TableColumn(element.key);
|
|
|
|
});
|
2021-08-12 19:14:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 20:03:13 +00:00
|
|
|
row(item: T): TemplateResult[] {
|
|
|
|
return this.metadata(item).map((element) => {
|
|
|
|
return html`${element.value}`;
|
|
|
|
});
|
2021-08-12 19:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderToolbarContainer(): TemplateResult {
|
|
|
|
return html``;
|
|
|
|
}
|
|
|
|
|
2021-08-12 20:03:13 +00:00
|
|
|
renderExpanded(item: T): TemplateResult {
|
|
|
|
return html`<td role="cell" colspan="2">
|
|
|
|
<div class="pf-c-table__expandable-row-content">
|
|
|
|
${this.usedBy
|
|
|
|
? until(
|
|
|
|
this.usedBy(item).then((usedBy) => {
|
|
|
|
return this.renderUsedBy(usedBy);
|
|
|
|
}),
|
|
|
|
html`<ak-spinner size=${PFSize.XLarge}></ak-spinner>`,
|
|
|
|
)
|
|
|
|
: html``}
|
|
|
|
</div>
|
|
|
|
</td>`;
|
2021-08-12 19:14:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 20:03:13 +00:00
|
|
|
renderUsedBy(usedBy: UsedBy[]): TemplateResult {
|
2021-08-12 19:14:51 +00:00
|
|
|
if (usedBy.length < 1) {
|
2021-09-18 22:14:50 +00:00
|
|
|
return html`<span>${t`Not used by any other object.`}</span>`;
|
2021-08-12 19:14:51 +00:00
|
|
|
}
|
2021-08-12 20:03:13 +00:00
|
|
|
return html`<ul class="pf-c-list">
|
|
|
|
${usedBy.map((ub) => {
|
|
|
|
let consequence = "";
|
|
|
|
switch (ub.action) {
|
|
|
|
case UsedByActionEnum.Cascade:
|
|
|
|
consequence = t`object will be DELETED`;
|
|
|
|
break;
|
|
|
|
case UsedByActionEnum.CascadeMany:
|
|
|
|
consequence = t`connection will be deleted`;
|
|
|
|
break;
|
|
|
|
case UsedByActionEnum.SetDefault:
|
|
|
|
consequence = t`reference will be reset to default value`;
|
|
|
|
break;
|
|
|
|
case UsedByActionEnum.SetNull:
|
|
|
|
consequence = t`reference will be set to an empty value`;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return html`<li>${t`${ub.name} (${consequence})`}</li>`;
|
|
|
|
})}
|
|
|
|
</ul>`;
|
2021-08-12 19:14:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@customElement("ak-forms-delete-bulk")
|
2021-08-12 20:03:13 +00:00
|
|
|
export class DeleteBulkForm extends ModalButton {
|
2021-08-12 19:14:51 +00:00
|
|
|
@property({ attribute: false })
|
2021-08-12 20:03:13 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
objects: any[] = [];
|
2021-08-12 19:14:51 +00:00
|
|
|
|
|
|
|
@property()
|
|
|
|
objectLabel?: string;
|
|
|
|
|
|
|
|
@property({ attribute: false })
|
2021-08-12 20:03:13 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
metadata: (item: any) => BulkDeleteMetadata = (item: any) => {
|
|
|
|
const rec = item as Record<string, unknown>;
|
|
|
|
const meta = [];
|
|
|
|
if (Object.prototype.hasOwnProperty.call(rec, "name")) {
|
|
|
|
meta.push({ key: t`Name`, value: rec.name as string });
|
|
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(rec, "pk")) {
|
|
|
|
meta.push({ key: t`ID`, value: rec.pk as string });
|
|
|
|
}
|
|
|
|
return meta;
|
|
|
|
};
|
|
|
|
|
|
|
|
@property({ attribute: false })
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
usedBy?: (item: any) => Promise<UsedBy[]>;
|
2021-08-12 19:14:51 +00:00
|
|
|
|
|
|
|
@property({ attribute: false })
|
2021-08-12 20:03:13 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
delete!: (item: any) => Promise<any>;
|
2021-08-12 19:14:51 +00:00
|
|
|
|
|
|
|
confirm(): Promise<void> {
|
|
|
|
return Promise.all(
|
|
|
|
this.objects.map((item) => {
|
|
|
|
return this.delete(item);
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.then(() => {
|
|
|
|
this.onSuccess();
|
|
|
|
this.open = false;
|
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent(EVENT_REFRESH, {
|
|
|
|
bubbles: true,
|
|
|
|
composed: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
this.onError(e);
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onSuccess(): void {
|
|
|
|
showMessage({
|
|
|
|
message: t`Successfully deleted ${this.objects.length} ${this.objectLabel}`,
|
|
|
|
level: MessageLevel.success,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onError(e: Error): void {
|
|
|
|
showMessage({
|
|
|
|
message: t`Failed to delete ${this.objectLabel}: ${e.toString()}`,
|
|
|
|
level: MessageLevel.error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
renderModalInner(): TemplateResult {
|
|
|
|
return html`<section class="pf-c-page__main-section pf-m-light">
|
|
|
|
<div class="pf-c-content">
|
|
|
|
<h1 class="pf-c-title pf-m-2xl">${t`Delete ${this.objectLabel}`}</h1>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
<section class="pf-c-page__main-section pf-m-light">
|
|
|
|
<form class="pf-c-form pf-m-horizontal">
|
2021-09-18 22:14:50 +00:00
|
|
|
<p class="pf-c-title">
|
2021-08-12 19:14:51 +00:00
|
|
|
${t`Are you sure you want to delete ${this.objects.length} ${this.objectLabel}?`}
|
|
|
|
</p>
|
|
|
|
</form>
|
|
|
|
</section>
|
|
|
|
<section class="pf-c-page__main-section">
|
2021-08-12 20:03:13 +00:00
|
|
|
<ak-delete-objects-table
|
|
|
|
.objects=${this.objects}
|
|
|
|
.usedBy=${this.usedBy}
|
|
|
|
.metadata=${this.metadata}
|
|
|
|
>
|
2021-08-12 19:14:51 +00:00
|
|
|
</ak-delete-objects-table>
|
|
|
|
</section>
|
|
|
|
<footer class="pf-c-modal-box__footer">
|
|
|
|
<ak-spinner-button
|
|
|
|
.callAction=${() => {
|
|
|
|
return this.confirm();
|
|
|
|
}}
|
|
|
|
class="pf-m-danger"
|
|
|
|
>
|
|
|
|
${t`Delete`} </ak-spinner-button
|
|
|
|
>
|
|
|
|
<ak-spinner-button
|
|
|
|
.callAction=${async () => {
|
|
|
|
this.open = false;
|
|
|
|
}}
|
|
|
|
class="pf-m-secondary"
|
|
|
|
>
|
|
|
|
${t`Cancel`}
|
|
|
|
</ak-spinner-button>
|
|
|
|
</footer>`;
|
|
|
|
}
|
|
|
|
}
|