convert the 'edit' and 'change password' buttons into currentUserAction's

This commit is contained in:
Jacob Overgaard
2024-04-10 17:31:01 +02:00
parent abc3273af7
commit aace91f08a
4 changed files with 124 additions and 3 deletions

View File

@@ -12,6 +12,7 @@ export class UmbCurrentUserContext extends UmbContextBase<UmbCurrentUserContext>
#currentUser = new UmbObjectState<UmbCurrentUserModel | undefined>(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;

View File

@@ -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<ArgsMetaType = never>
extends UmbActionBase<UmbCurrentUserActionArgs<ArgsMetaType>>
implements UmbCurrentUserAction<ArgsMetaType>
{
#unique?: string;
constructor(host: UmbControllerHost, args: UmbCurrentUserActionArgs<ArgsMetaType>) {
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,
},
},
});
}
}

View File

@@ -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<ArgsMetaType = never>
extends UmbActionBase<UmbCurrentUserActionArgs<ArgsMetaType>>
implements UmbCurrentUserAction<ArgsMetaType>
{
#init;
#unique?: string;
constructor(host: UmbControllerHost, args: UmbCurrentUserActionArgs<ArgsMetaType>) {
super(host, args);
this.#init = new Promise<void>((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;
}
}

View File

@@ -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<ManifestUserProfileApp> = [
const userProfileApps: Array<ManifestUserProfileApp> = [
{
type: 'userProfileApp',
alias: 'Umb.UserProfileApp.CurrentUser.Profile',
@@ -13,4 +18,38 @@ export const userProfileApps: Array<ManifestUserProfileApp> = [
},
},
];
export const manifests = [...userProfileApps];
const currentUserActions: Array<ManifestCurrentUserActionDefaultKind> = [
{
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];