|
| 1 | +import { UmbDocumentNotificationsRepository } from '../repository/document-notifications.repository.js'; |
| 2 | +import type { |
| 3 | + UmbDocumentNotificationsModalData, |
| 4 | + UmbDocumentNotificationsModalValue, |
| 5 | +} from './document-notifications-modal.token.js'; |
| 6 | +import type { UmbEntityUnique } from '@umbraco-cms/backoffice/entity'; |
| 7 | +import type { GetDocumentByIdNotificationsResponse } from '@umbraco-cms/backoffice/external/backend-api'; |
| 8 | +import { css, customElement, html, repeat, state } from '@umbraco-cms/backoffice/external/lit'; |
| 9 | +import { UmbModalBaseElement } from '@umbraco-cms/backoffice/modal'; |
| 10 | +import { UmbTextStyles } from '@umbraco-cms/backoffice/style'; |
| 11 | + |
| 12 | +interface UmbDocumentNotificationSettings extends GetDocumentByIdNotificationsResponse {} |
| 13 | + |
| 14 | +@customElement('umb-document-notifications-modal') |
| 15 | +export class UmbDocumentNotificationsModalElement extends UmbModalBaseElement< |
| 16 | + UmbDocumentNotificationsModalData, |
| 17 | + UmbDocumentNotificationsModalValue |
| 18 | +> { |
| 19 | + #unique?: UmbEntityUnique; |
| 20 | + #documentNotificationsRepository = new UmbDocumentNotificationsRepository(this); |
| 21 | + |
| 22 | + #localizationKeys = [ |
| 23 | + { actionId: 'Umb.Document.Duplicate', key: 'actions_copy' }, |
| 24 | + { actionId: 'Umb.Document.Delete', key: 'actions_delete' }, |
| 25 | + { actionId: 'Umb.Document.Move', key: 'actions_move' }, |
| 26 | + { actionId: 'Umb.Document.Create', key: 'actions_create' }, |
| 27 | + { actionId: 'Umb.Document.PublicAccess', key: 'actions_protect' }, |
| 28 | + { actionId: 'Umb.Document.Publish', key: 'actions_publish' }, |
| 29 | + { actionId: 'Umb.DocumentRecycleBin.Restore', key: 'actions_restore' }, |
| 30 | + { actionId: 'Umb.Document.Permissions', key: 'actions_rights' }, |
| 31 | + { actionId: 'Umb.Document.Rollback', key: 'actions_rollback' }, |
| 32 | + { actionId: 'Umb.Document.Sort', key: 'actions_sort' }, |
| 33 | + { actionId: 'Umb.Document.SendForApproval', key: 'actions_sendtopublish' }, |
| 34 | + { actionId: 'Umb.Document.Update', key: 'actions_update' }, |
| 35 | + ]; |
| 36 | + |
| 37 | + @state() |
| 38 | + private _settings: UmbDocumentNotificationSettings = []; |
| 39 | + |
| 40 | + override firstUpdated() { |
| 41 | + this.#unique = this.data?.unique; |
| 42 | + this.#readNotificationSettings(); |
| 43 | + } |
| 44 | + |
| 45 | + async #readNotificationSettings() { |
| 46 | + if (!this.#unique) return; |
| 47 | + const { data } = await this.#documentNotificationsRepository.readNotifications(this.#unique); |
| 48 | + |
| 49 | + if (!data) return; |
| 50 | + this._settings = data; |
| 51 | + } |
| 52 | + |
| 53 | + async #updateNotificationSettings() { |
| 54 | + if (!this.#unique) return; |
| 55 | + |
| 56 | + const subscribedActionIds = this._settings.filter((x) => x.subscribed).map((x) => x.actionId); |
| 57 | + const { error } = await this.#documentNotificationsRepository.updateNotifications(this.#unique, { |
| 58 | + subscribedActionIds, |
| 59 | + }); |
| 60 | + |
| 61 | + if (error) return; |
| 62 | + this._submitModal(); |
| 63 | + } |
| 64 | + |
| 65 | + async #updateSubscription(actionId: string) { |
| 66 | + this._settings = this._settings.map((setting) => { |
| 67 | + if (setting.actionId === actionId) { |
| 68 | + const subscribed = !setting.subscribed; |
| 69 | + return { ...setting, subscribed }; |
| 70 | + } |
| 71 | + return setting; |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + override render() { |
| 76 | + return html` |
| 77 | + <umb-body-layout headline=${this.localize.term('notifications_notifications')}> |
| 78 | + <uui-box> |
| 79 | + ${repeat( |
| 80 | + this._settings, |
| 81 | + (setting) => setting.actionId, |
| 82 | + (setting) => { |
| 83 | + const localization = this.#localizationKeys.find((x) => x.actionId === setting.actionId); |
| 84 | + return html`<uui-toggle |
| 85 | + id=${setting.actionId} |
| 86 | + @change=${() => this.#updateSubscription(setting.actionId)} |
| 87 | + .label=${localization ? this.localize.term(localization.key) : setting.actionId} |
| 88 | + ?checked=${setting.subscribed}></uui-toggle>`; |
| 89 | + }, |
| 90 | + )} |
| 91 | + </uui-box> |
| 92 | + <umb-footer-layout slot="footer"> |
| 93 | + <uui-button |
| 94 | + slot="actions" |
| 95 | + look="secondary" |
| 96 | + label=${this.localize.term('general_cancel')} |
| 97 | + @click=${this._rejectModal}></uui-button> |
| 98 | + <uui-button |
| 99 | + slot="actions" |
| 100 | + look="primary" |
| 101 | + color="positive" |
| 102 | + label=${this.localize.term('buttons_save')} |
| 103 | + @click=${this.#updateNotificationSettings}></uui-button> |
| 104 | + </umb-footer-layout> |
| 105 | + </umb-body-layout> |
| 106 | + `; |
| 107 | + } |
| 108 | + |
| 109 | + static override styles = [ |
| 110 | + UmbTextStyles, |
| 111 | + css` |
| 112 | + uui-toggle { |
| 113 | + display: block; |
| 114 | + } |
| 115 | + `, |
| 116 | + ]; |
| 117 | +} |
| 118 | + |
| 119 | +export default UmbDocumentNotificationsModalElement; |
| 120 | + |
| 121 | +declare global { |
| 122 | + interface HTMLElementTagNameMap { |
| 123 | + 'umb-document-notifications-modal': UmbDocumentNotificationsModalElement; |
| 124 | + } |
| 125 | +} |
0 commit comments