Browse Source

feat(admin-ui): Enable setting Role channel on update

Relates to #12
Michael Bromley 6 years ago
parent
commit
8379a82904

+ 2 - 1
packages/admin-ui/src/app/common/generated-types.ts

@@ -534,10 +534,10 @@ export type CreatePromotionInput = {
 };
 
 export type CreateRoleInput = {
-  channelIds?: Maybe<Array<Scalars['ID']>>,
   code: Scalars['String'],
   description: Scalars['String'],
   permissions: Array<Permission>,
+  channelIds?: Maybe<Array<Scalars['ID']>>,
 };
 
 export type CreateShippingMethodInput = {
@@ -3456,6 +3456,7 @@ export type UpdateRoleInput = {
   code?: Maybe<Scalars['String']>,
   description?: Maybe<Scalars['String']>,
   permissions?: Maybe<Array<Permission>>,
+  channelIds?: Maybe<Array<Scalars['ID']>>,
 };
 
 export type UpdateShippingMethodInput = {

+ 3 - 1
packages/admin-ui/src/app/settings/components/permission-grid/permission-grid.component.ts

@@ -49,6 +49,8 @@ export class PermissionGridComponent {
     ];
 
     setPermission(permission: string, value: boolean) {
-        this.permissionChange.emit({ permission, value });
+        if (!this.readonly) {
+            this.permissionChange.emit({ permission, value });
+        }
     }
 }

+ 16 - 2
packages/admin-ui/src/app/settings/components/role-detail/role-detail.component.html

@@ -16,6 +16,7 @@
             <button
                 class="btn btn-primary"
                 (click)="save()"
+                *vdrIfPermissions="'UpdateAdministrator'"
                 [disabled]="(detailForm.invalid || detailForm.pristine) && !permissionsChanged"
             >
                 {{ 'common.update' | translate }}
@@ -30,15 +31,28 @@
             id="description"
             type="text"
             formControlName="description"
+            [readonly]="!('UpdateAdministrator' | hasPermission)"
             (input)="updateCode($event.target.value)"
         />
     </vdr-form-field>
-    <vdr-form-field [label]="'common.code' | translate" for="code" [readOnlyToggle]="true">
-        <input id="code" type="text" formControlName="code" />
+    <vdr-form-field [label]="'common.code' | translate" for="code" [readOnlyToggle]="'UpdateAdministrator' | hasPermission">
+        <input id="code" type="text" formControlName="code" [readonly]="!('UpdateAdministrator' | hasPermission)" />
+    </vdr-form-field>
+    <vdr-form-field [label]="'settings.channel' | translate">
+        <ng-select
+            [items]="channels$ | async"
+            bindLabel="code"
+            bindValue="id"
+            appendTo="body"
+            [addTag]="false"
+            [multiple]="true"
+            formControlName="channelIds"
+        ></ng-select>
     </vdr-form-field>
     <label>{{ 'settings.permissions' | translate }}</label>
     <vdr-permission-grid
         [permissions]="permissions"
         (permissionChange)="setPermission($event)"
+        [readonly]="!('UpdateAdministrator' | hasPermission)"
     ></vdr-permission-grid>
 </form>

+ 16 - 1
packages/admin-ui/src/app/settings/components/role-detail/role-detail.component.ts

@@ -2,12 +2,13 @@ import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnIni
 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
 import { ActivatedRoute, Router } from '@angular/router';
 import { Observable } from 'rxjs';
-import { mergeMap, take } from 'rxjs/operators';
+import { map, mergeMap, take, tap } from 'rxjs/operators';
 import { normalizeString } from 'shared/normalize-string';
 
 import { BaseDetailComponent } from '../../../common/base-detail.component';
 import {
     CreateRoleInput,
+    CurrentUserChannel,
     LanguageCode,
     Permission,
     Role,
@@ -26,6 +27,7 @@ import { ServerConfigService } from '../../../data/server-config';
 })
 export class RoleDetailComponent extends BaseDetailComponent<Role> implements OnInit, OnDestroy {
     role$: Observable<Role>;
+    channels$: Observable<CurrentUserChannel[]>;
     detailForm: FormGroup;
     permissions: { [K in Permission]: boolean };
     permissionsChanged = false;
@@ -46,12 +48,22 @@ export class RoleDetailComponent extends BaseDetailComponent<Role> implements On
         this.detailForm = this.formBuilder.group({
             code: ['', Validators.required],
             description: ['', Validators.required],
+            channelIds: [],
         });
     }
 
     ngOnInit() {
         this.init();
         this.role$ = this.entity$;
+        this.channels$ = this.dataService.client.userStatus().single$.pipe(
+            map(data => (data.userStatus ? data.userStatus.channels : [])),
+            tap(channels => {
+                const channelIdControl = this.detailForm.get('channelId');
+                if (channelIdControl) {
+                    channelIdControl.patchValue(channels[0].id);
+                }
+            }),
+        );
     }
 
     ngOnDestroy(): void {
@@ -76,6 +88,7 @@ export class RoleDetailComponent extends BaseDetailComponent<Role> implements On
             code: formValue.code,
             description: formValue.description,
             permissions: this.getSelectedPermissions(),
+            channelIds: [formValue.channelId],
         };
         this.dataService.administrator.createRole(role).subscribe(
             data => {
@@ -104,6 +117,7 @@ export class RoleDetailComponent extends BaseDetailComponent<Role> implements On
                         code: formValue.code,
                         description: formValue.description,
                         permissions: this.getSelectedPermissions(),
+                        channelIds: formValue.channelIds,
                     };
                     return this.dataService.administrator.updateRole(role);
                 }),
@@ -127,6 +141,7 @@ export class RoleDetailComponent extends BaseDetailComponent<Role> implements On
         this.detailForm.patchValue({
             description: role.description,
             code: role.code,
+            channelIds: role.channels.map(c => c.id),
         });
         for (const permission of Object.keys(this.permissions)) {
             this.permissions[permission] = role.permissions.includes(permission as Permission);

+ 1 - 0
packages/admin-ui/src/i18n-messages/en.json

@@ -537,6 +537,7 @@
     "add-products-to-test-order": "Add products to the test order",
     "administrator": "Administrator",
     "catalog": "Catalog",
+    "channel": "Channel",
     "channel-token": "Channel token",
     "create": "Create",
     "create-new-channel": "Create new channel",