user group action

This commit is contained in:
Niels Lyngsø
2022-12-20 16:16:51 +01:00
parent d572566cdd
commit cedabc16f0
2 changed files with 19 additions and 34 deletions

View File

@@ -2,12 +2,8 @@ import { css, html, LitElement } from 'lit';
import { customElement, state } from 'lit/decorators.js';
import { UUITextStyles } from '@umbraco-ui/uui-css/lib';
import type { UUIButtonState } from '@umbraco-ui/uui';
import type { UmbNotificationDefaultData } from '../../../../core/services/notification/layouts/default';
import type { UmbNotificationService } from '../../../../core/services/notification';
import { UmbUserGroupContext } from '../user-group.context';
import type { UmbWorkspaceUserContext } from '../../user/workspace-user.context';
import { UmbContextConsumerMixin } from '@umbraco-cms/context-api';
import { UmbUserGroupStore } from 'src/core/stores/user/user-group.store';
import { UmbUserStore } from 'src/core/stores/user/user.store';
@customElement('umb-workspace-action-user-group-save')
export class UmbWorkspaceActionUserGroupSaveElement extends UmbContextConsumerMixin(LitElement) {
@@ -16,45 +12,26 @@ export class UmbWorkspaceActionUserGroupSaveElement extends UmbContextConsumerMi
@state()
private _saveButtonState?: UUIButtonState;
private _userGroupStore?: UmbUserGroupStore;
private _userStore?: UmbUserStore;
private _userGroupContext?: UmbUserGroupContext;
private _notificationService?: UmbNotificationService;
private _workspaceContext?: UmbWorkspaceUserContext;
connectedCallback(): void {
super.connectedCallback();
constructor() {
super();
this.consumeAllContexts(
['umbUserGroupStore', 'umbUserStore', 'umbUserGroupContext', 'umbNotificationService'],
(instances) => {
this._userGroupStore = instances['umbUserGroupStore'];
this._userStore = instances['umbUserStore'];
this._userGroupContext = instances['umbUserGroupContext'];
this._notificationService = instances['umbNotificationService'];
this.consumeContext('umbWorkspaceContext', (instance) => {
this._workspaceContext = instance;
}
);
}
private async _handleSave() {
// TODO: What if store is not present, what if node is not loaded....
if (!this._userGroupStore || !this._userGroupContext) return;
if (!this._workspaceContext) return;
try {
this._saveButtonState = 'waiting';
const userGroup = this._userGroupContext.getData();
await this._userGroupStore.save([userGroup]);
if (this._userStore && userGroup.users) {
await this._userStore.updateUserGroup(userGroup.users, userGroup.key);
}
const notificationData: UmbNotificationDefaultData = { message: 'User Group Saved' };
this._notificationService?.peek('positive', { data: notificationData });
this._saveButtonState = 'waiting';
await this._workspaceContext.save().then(() => {
this._saveButtonState = 'success';
} catch (error) {
const notificationData: UmbNotificationDefaultData = { message: 'User Group Save Failed' };
this._notificationService?.peek('danger', { data: notificationData });
}).catch(() => {
this._saveButtonState = 'failed';
}
})
}
render() {

View File

@@ -56,6 +56,14 @@ export class UmbUserGroupStore extends UmbDataStoreBase<UmbUserGroupStoreItemTyp
async save(userGroups: Array<UmbUserGroupStoreItemType>): Promise<void> {
// TODO: use Fetcher API.
// TODO: implement so user group store updates the
/*
if (this._userStore && userGroup.users) {
await this._userStore.updateUserGroup(userGroup.users, userGroup.key);
}
*/
try {
const res = await fetch('/umbraco/backoffice/user-groups/save', {
method: 'POST',