Improved user warnings in WI #402
This commit is contained in:
		| @@ -1,8 +1,8 @@ | ||||
| export class Mutations { | ||||
|   static updateUser = ` | ||||
|     mutation updateUser($id: ID, $xp: Int, $levelId: ID) { | ||||
|     mutation updateUser($id: ID, $xp: Int, $levelId: ID, $userWarnings: [UserWarningInput]) { | ||||
|       user { | ||||
|         updateUser(input: { id: $id, xp: $xp, levelId: $levelId }) { | ||||
|         updateUser(input: { id: $id, xp: $xp, levelId: $levelId, userWarnings: $userWarnings }) { | ||||
|           id | ||||
|           name | ||||
|           xp | ||||
| @@ -10,6 +10,10 @@ export class Mutations { | ||||
|             id | ||||
|             name | ||||
|           } | ||||
|           userWarnings { | ||||
|             id | ||||
|             description | ||||
|           } | ||||
|         } | ||||
|       } | ||||
|     } | ||||
| @@ -314,4 +318,50 @@ export class Mutations { | ||||
|       } | ||||
|     } | ||||
|   `; | ||||
|  | ||||
|  | ||||
|  | ||||
|   static createUserWarning = ` | ||||
|     mutation createUserWarning($name: String, $description: String, $attribute: String, $operator: String, $value: String, $serverId: ID) { | ||||
|       userWarning { | ||||
|         createUserWarning(input: { name: $name, description: $description, attribute: $attribute, operator: $operator, value: $value, serverId: $serverId}) { | ||||
|           id | ||||
|           name | ||||
|           description | ||||
|           attribute | ||||
|           operator | ||||
|           value | ||||
|           server { | ||||
|             id | ||||
|           } | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|   `; | ||||
|  | ||||
|   static updateUserWarning = ` | ||||
|     mutation updateUserWarning($id: ID, $name: String, $description: String, $attribute: String, $operator: String, $value: String) { | ||||
|       userWarning { | ||||
|         updateUserWarning(input: { id: $id, name: $name, description: $description, attribute: $attribute, operator: $operator, value: $value}) { | ||||
|           id | ||||
|           name | ||||
|           description | ||||
|           attribute | ||||
|           operator | ||||
|           value | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|   `; | ||||
|  | ||||
|   static deleteUserWarning = ` | ||||
|     mutation deleteUserWarning($id: ID) { | ||||
|       userWarning { | ||||
|         deleteUserWarning(id: $id) { | ||||
|           id | ||||
|           name | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|   `; | ||||
| } | ||||
|   | ||||
| @@ -360,6 +360,10 @@ export class Queries { | ||||
|         userWarningCount | ||||
|         userWarnings { | ||||
|           id | ||||
|           user { | ||||
|             id | ||||
|             name | ||||
|           } | ||||
|           description | ||||
|           author { | ||||
|             id | ||||
|   | ||||
| @@ -6,6 +6,7 @@ import { Achievement } from "../data/achievement.model"; | ||||
| import { TechnicianConfig } from "../config/technician-config.model"; | ||||
| import { ServerConfig } from "../config/server-config.model"; | ||||
| import { ShortRoleName } from "../data/short_role_name.model"; | ||||
| import { UserWarning } from "../data/user_warning.model"; | ||||
|  | ||||
| export interface GraphQLResult { | ||||
|   data: { | ||||
| @@ -77,3 +78,11 @@ export interface ShortRoleNameMutationResult { | ||||
|     deleteShortRoleName?: ShortRoleName | ||||
|   }; | ||||
| } | ||||
|  | ||||
| export interface UserWarningMutationResult { | ||||
|   userWarning: { | ||||
|     createUserWarning?: UserWarning | ||||
|     updateUserWarning?: UserWarning | ||||
|     deleteUserWarning?: UserWarning | ||||
|   }; | ||||
| } | ||||
|   | ||||
| @@ -135,10 +135,10 @@ | ||||
|             <td> | ||||
|               <p-cellEditor> | ||||
|                 <ng-template pTemplate="input"> | ||||
|                   {{value.author.name}} | ||||
|                   {{value.author?.name}} | ||||
|                 </ng-template> | ||||
|                 <ng-template pTemplate="output"> | ||||
|                   {{value.author.name}} | ||||
|                   {{value.author?.name}} | ||||
|                 </ng-template> | ||||
|               </p-cellEditor> | ||||
|             </td> | ||||
| @@ -262,5 +262,12 @@ | ||||
|         </div> | ||||
|       </div> | ||||
|     </p-panel> | ||||
|  | ||||
|     <div class="content-divider"></div> | ||||
|  | ||||
|     <div class="content-row"> | ||||
|       <button pButton icon="pi pi-save" label="{{'common.save' | translate}}" class="btn login-form-submit-btn" | ||||
|               (click)="updateUser()"></button> | ||||
|     </div> | ||||
|   </div> | ||||
| </div> | ||||
|   | ||||
| @@ -10,9 +10,13 @@ import { AuthService } from "src/app/services/auth/auth.service"; | ||||
| import { ToastService } from "src/app/services/toast/toast.service"; | ||||
| import { TranslateService } from "@ngx-translate/core"; | ||||
| import { Server } from "../../../../models/data/server.model"; | ||||
| import { forkJoin, Subject } from "rxjs"; | ||||
| import { takeUntil } from "rxjs/operators"; | ||||
| import { forkJoin, Subject, throwError } from "rxjs"; | ||||
| import { catchError, takeUntil } from "rxjs/operators"; | ||||
| import { Table } from "primeng/table"; | ||||
| import { UserWarning } from "../../../../models/data/user_warning.model"; | ||||
| import { LevelMutationResult, UpdateUserMutationResult, UserWarningMutationResult } from "../../../../models/graphql/result.model"; | ||||
| import { Mutations } from "../../../../models/graphql/mutations.model"; | ||||
| import { ConfirmationDialogService } from "../../../../services/confirmation-dialog/confirmation-dialog.service"; | ||||
|  | ||||
| @Component({ | ||||
|   selector: "app-profile", | ||||
| @@ -23,6 +27,10 @@ export class ProfileComponent implements OnInit, OnDestroy { | ||||
|  | ||||
|   user: User = { createdAt: "", modifiedAt: "" }; | ||||
|   private server: Server = {}; | ||||
|   private author?: UserDTO; | ||||
|   private clonedUserWarnings: UserWarning[] = []; | ||||
|   public isEditingNewUserWarning: boolean = false; | ||||
|   public isEditing: boolean = false; | ||||
|  | ||||
|   private unsubscriber = new Subject<void>(); | ||||
|  | ||||
| @@ -33,11 +41,16 @@ export class ProfileComponent implements OnInit, OnDestroy { | ||||
|     private data: DataService, | ||||
|     private auth: AuthService, | ||||
|     private toast: ToastService, | ||||
|     private translate: TranslateService | ||||
|     private translate: TranslateService, | ||||
|     private toastService: ToastService | ||||
|   ) { | ||||
|   } | ||||
|  | ||||
|   public ngOnInit(): void { | ||||
|     this.loadProfile(); | ||||
|   } | ||||
|  | ||||
|   private loadProfile() { | ||||
|     this.route.params.pipe(takeUntil(this.unsubscriber)).subscribe(params => { | ||||
|       this.data.getServerFromRoute(this.route).then(async (server) => { | ||||
|         if (!params["memberId"] || params["memberId"] == "undefined") { | ||||
| @@ -55,6 +68,7 @@ export class ProfileComponent implements OnInit, OnDestroy { | ||||
|           await this.router.navigate(["/server", server.id]); | ||||
|           return; | ||||
|         } | ||||
|         this.author = user; | ||||
|  | ||||
|         this.data.query<UserListQuery>(Queries.userProfile, { | ||||
|             serverId: this.server.id, | ||||
| @@ -85,6 +99,32 @@ export class ProfileComponent implements OnInit, OnDestroy { | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   public updateUser() { | ||||
|     this.spinner.showSpinner(); | ||||
|     this.spinner.showSpinner(); | ||||
|     this.data.mutation<UpdateUserMutationResult>(Mutations.updateUser, { | ||||
|         id: this.user.id, | ||||
|         xp: this.user.xp, | ||||
|         levelId: this.user.level?.id, | ||||
|         userWarnings: this.user.userWarnings?.map(userWarning => { | ||||
|           return { | ||||
|             id: userWarning.id, | ||||
|             user: userWarning.user?.id ?? this.user.id, | ||||
|             description: userWarning.description, | ||||
|             author: userWarning.author?.id ?? this.author?.id | ||||
|           } | ||||
|         }) | ||||
|       } | ||||
|     ).pipe(catchError(err => { | ||||
|       this.spinner.hideSpinner(); | ||||
|       return throwError(err); | ||||
|     })).subscribe(_ => { | ||||
|       this.spinner.hideSpinner(); | ||||
|       this.toastService.success(this.translate.instant("view.server.members.message.user_changed"), this.translate.instant("view.server.members.message.user_changed_d", { name: this.user.name })); | ||||
|       this.loadProfile(); | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   public ngOnDestroy(): void { | ||||
|     this.unsubscriber.next(); | ||||
|     this.unsubscriber.complete(); | ||||
| @@ -141,16 +181,42 @@ export class ProfileComponent implements OnInit, OnDestroy { | ||||
|   } | ||||
|  | ||||
|   addNewUserWarning(table: Table) { | ||||
|     const newWarning: UserWarning = { | ||||
|       description: "", | ||||
|       user: this.user | ||||
|     }; | ||||
|  | ||||
|     this.user.userWarnings = [newWarning, ...this.user.userWarnings ?? []]; | ||||
|  | ||||
|     table.initRowEdit(newWarning); | ||||
|  | ||||
|     const index = this.user.userWarnings.findIndex(l => l.id == newWarning.id); | ||||
|     this.onRowEditInit(table, newWarning, index); | ||||
|  | ||||
|     this.isEditingNewUserWarning = true; | ||||
|   } | ||||
|  | ||||
|   public onRowEditInit(table: Table, user: User, index: number): void { | ||||
|     this.clonedUserWarnings[index] = { ...user }; | ||||
|   } | ||||
|  | ||||
|   deleteUserWarning(index: number) { | ||||
|     this.user.userWarnings?.splice(index, 1); | ||||
|   } | ||||
|  | ||||
|   editSaveUserWarning(value: any, index: number) { | ||||
|     this.isEditingNewUserWarning = false; | ||||
|     if (!value.value || !this.user.userWarnings || this.user.userWarnings[index] == this.clonedUserWarnings[index]) { | ||||
|       return; | ||||
|     } | ||||
|  | ||||
|     delete this.clonedUserWarnings[index]; | ||||
|   } | ||||
|  | ||||
|   editCancelUserWarning(index: number) { | ||||
|     if (this.user.userWarnings) { | ||||
|       this.user.userWarnings[index] = this.clonedUserWarnings[index]; | ||||
|     } | ||||
|     delete this.clonedUserWarnings[index]; | ||||
|   } | ||||
|  | ||||
|   protected readonly visualViewport = visualViewport; | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user