import { customElement, property, state } from "lit/decorators.js"; import { ClientConsumer } from "../ClientConsumer"; import { css, CSSResultGroup, html, nothing } from "lit"; import { FileInfo, FolderInfo } from "@labir/server"; import { BtnSizes, BtnVariants } from "../../../ui/Btn"; import { ifDefined } from "lit/directives/if-defined.js"; import { T } from "../../../translations/Languages"; import { t } from "i18next"; import { booleanConverter } from "../../../utils/converters/booleanConverter"; @customElement("file-delete-dialog") export class FileDeleteDialog extends ClientConsumer { @property({ type: String, converter: booleanConverter(false) }) public showLabel: boolean = false; @property({ type: String }) public label: string = t(T.deletefile); @property({ type: String, reflect: false }) public variant?: BtnVariants; @property({ type: String, reflect: true }) public size: BtnSizes = "sm"; @property({ type: String }) public plain: boolean = false; @property({ type: Object }) public file!: FileInfo; @property({ type: Object }) public folder!: FolderInfo; @property({ type: Function }) public onDelete?: (file: FileInfo) => void = () => { }; @state() protected isOpen: boolean = false; @state() protected isDeleting: boolean = false; @state() protected error?: string; public static styles?: CSSResultGroup | undefined = css` .content { padding: var(--thermal-gap); } .warning { background: var(--thermal-danger-light, #fee); border: 1px solid var(--thermal-danger, #f00); border-radius: var(--thermal-radius); padding: var(--thermal-gap); margin-bottom: var(--thermal-gap); color: var(--thermal-danger-dark, #800); } .file-info { background: var(--thermal-background); border-radius: var(--thermal-radius); padding: calc(var(--thermal-gap) * 0.5); margin: calc(var(--thermal-gap) * 0.5) 0; } .file-name { font-weight: bold; font-size: calc(var(--thermal-fs) * 1.1); } .file-description { color: var(--thermal-slate); font-size: calc(var(--thermal-fs) * 0.9); margin-top: calc(var(--thermal-gap) * 0.25); } .error { background: var(--thermal-danger-light, #fee); border: 1px solid var(--thermal-danger, #f00); border-radius: var(--thermal-radius); padding: calc(var(--thermal-gap) * 0.5); margin-top: var(--thermal-gap); color: var(--thermal-danger-dark, #800); font-size: calc(var(--thermal-fs) * 0.9); } `; protected async handleDelete(): Promise { if (!this.client || this.isDeleting) return false; this.error = undefined; try { const request = this.client.routes.post.deleteFile(this.folder.path, this.file.fileName); const result = await request.execute(); if (result.success) { this.isOpen = false; this.onDelete?.(this.file); return true; } else { this.error = result.message; } } catch (error) { this.error = "Došlo k neočekávané chybě při mazání souboru."; } return false; } protected mayDelete(): boolean { return !!( this.folder && (this.folder.may_manage_files_in || this.folder.may_manage_folders_in) ); } protected render(): unknown { if (!this.mayDelete()) { return nothing; } const label = t(T.deletefile); return html` { this.error = undefined; return await this.handleDelete(); }} > ${this.showLabel ? label : nothing }
Pozor! Tato akce je nevratná.

Opravdu chcete smazat soubor ${this.file.fileName}? ${this.file.label ? html`
(${this.file.label})` : nothing}

${this.error ? html`
${this.error}
` : nothing}
`; } }