From aace91f08ad1d12a705c463efbee0812d090a629 Mon Sep 17 00:00:00 2001 From: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com> Date: Wed, 10 Apr 2024 17:31:01 +0200 Subject: [PATCH] convert the 'edit' and 'change password' buttons into currentUserAction's --- .../user/current-user/current-user.context.ts | 1 + .../change-password-current-user.action.ts | 43 ++++++++++++++++++ .../profile/edit-current-user.action.ts | 38 ++++++++++++++++ .../user/current-user/profile/manifests.ts | 45 +++++++++++++++++-- 4 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/change-password-current-user.action.ts create mode 100644 src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/edit-current-user.action.ts diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/current-user.context.ts b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/current-user.context.ts index 5de00944d5..2faa82b6ff 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/current-user.context.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/current-user.context.ts @@ -12,6 +12,7 @@ export class UmbCurrentUserContext extends UmbContextBase #currentUser = new UmbObjectState(undefined); readonly currentUser = this.#currentUser.asObservable(); + readonly unique = this.#currentUser.asObservablePart((user) => user?.unique); readonly languageIsoCode = this.#currentUser.asObservablePart((user) => user?.languageIsoCode); #authContext?: typeof UMB_AUTH_CONTEXT.TYPE; diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/change-password-current-user.action.ts b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/change-password-current-user.action.ts new file mode 100644 index 0000000000..1b24d3e922 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/change-password-current-user.action.ts @@ -0,0 +1,43 @@ +import { UMB_CURRENT_USER_CONTEXT } from '../current-user.context.js'; +import { UmbActionBase } from '@umbraco-cms/backoffice/action'; +import type { UmbCurrentUserAction, UmbCurrentUserActionArgs } from '@umbraco-cms/backoffice/extension-registry'; +import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; +import { UMB_CHANGE_PASSWORD_MODAL, UMB_MODAL_MANAGER_CONTEXT } from '@umbraco-cms/backoffice/modal'; + +export class UmbChangePasswordCurrentUserAction + extends UmbActionBase> + implements UmbCurrentUserAction +{ + #unique?: string; + + constructor(host: UmbControllerHost, args: UmbCurrentUserActionArgs) { + super(host, args); + + this.consumeContext(UMB_CURRENT_USER_CONTEXT, (context) => { + this.observe( + context.unique, + (unique) => { + this.#unique = unique; + }, + 'umbEditCurrentUserActionObserver', + ); + }); + } + + async getHref() { + return undefined; + } + + async execute() { + if (!this.#unique) return; + + const modalManager = await this.getContext(UMB_MODAL_MANAGER_CONTEXT); + modalManager.open(this, UMB_CHANGE_PASSWORD_MODAL, { + data: { + user: { + unique: this.#unique, + }, + }, + }); + } +} diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/edit-current-user.action.ts b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/edit-current-user.action.ts new file mode 100644 index 0000000000..ad02bc5285 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/edit-current-user.action.ts @@ -0,0 +1,38 @@ +import { UMB_CURRENT_USER_CONTEXT } from '../current-user.context.js'; +import { UmbActionBase } from '@umbraco-cms/backoffice/action'; +import type { UmbCurrentUserAction, UmbCurrentUserActionArgs } from '@umbraco-cms/backoffice/extension-registry'; +import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; + +export class UmbEditCurrentUserAction + extends UmbActionBase> + implements UmbCurrentUserAction +{ + #init; + #unique?: string; + + constructor(host: UmbControllerHost, args: UmbCurrentUserActionArgs) { + super(host, args); + + this.#init = new Promise((res) => { + this.consumeContext(UMB_CURRENT_USER_CONTEXT, (context) => { + this.observe( + context.unique, + (unique) => { + this.#unique = unique; + res(); + }, + 'umbEditCurrentUserActionObserver', + ); + }); + }); + } + + async getHref() { + await this.#init; + return `section/user-management/view/users/user/edit/${this.#unique}`; + } + + async execute() { + return; + } +} diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/manifests.ts b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/manifests.ts index 632490be5f..70cc917a96 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/manifests.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/manifests.ts @@ -1,6 +1,11 @@ -import type { ManifestUserProfileApp } from '@umbraco-cms/backoffice/extension-registry'; +import { UmbChangePasswordCurrentUserAction } from './change-password-current-user.action.js'; +import { UmbEditCurrentUserAction } from './edit-current-user.action.js'; +import type { + ManifestCurrentUserActionDefaultKind, + ManifestUserProfileApp, +} from '@umbraco-cms/backoffice/extension-registry'; -export const userProfileApps: Array = [ +const userProfileApps: Array = [ { type: 'userProfileApp', alias: 'Umb.UserProfileApp.CurrentUser.Profile', @@ -13,4 +18,38 @@ export const userProfileApps: Array = [ }, }, ]; -export const manifests = [...userProfileApps]; + +const currentUserActions: Array = [ + { + type: 'currentUserAction', + kind: 'default', + alias: 'Umb.CurrentUser.Button.Edit', + name: 'Current User Edit Button', + weight: 1000, + api: UmbEditCurrentUserAction, + meta: { + label: '#general_edit', + icon: 'edit', + }, + conditions: [ + { + alias: 'Umb.Condition.SectionUserPermission', + match: 'Umb.Section.Users', + }, + ], + }, + { + type: 'currentUserAction', + kind: 'default', + alias: 'Umb.CurrentUser.Button.ChangePassword', + name: 'Current User Change Password Button', + weight: 900, + api: UmbChangePasswordCurrentUserAction, + meta: { + label: '#general_changePassword', + icon: 'lock', + }, + }, +]; + +export const manifests = [...userProfileApps, ...currentUserActions];