176 lines
4.4 KiB
TypeScript
176 lines
4.4 KiB
TypeScript
import { CommonModule } from '@angular/common';
|
|
import { Component, inject } from '@angular/core';
|
|
import {
|
|
FormControl,
|
|
FormGroup,
|
|
ReactiveFormsModule,
|
|
Validators,
|
|
} from '@angular/forms';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import {
|
|
MAT_DIALOG_DATA,
|
|
MatDialogModule,
|
|
MatDialogRef,
|
|
} from '@angular/material/dialog';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatInputModule } from '@angular/material/input';
|
|
import { MatSelectModule } from '@angular/material/select';
|
|
import {
|
|
AccountProfile,
|
|
InseAccountManagementModelOrganizationProfile,
|
|
UserRole,
|
|
} from '../../api';
|
|
|
|
export type AccountDialogMode = 'create' | 'edit';
|
|
|
|
export interface AccountDialogData {
|
|
mode: AccountDialogMode;
|
|
account?: AccountProfile;
|
|
organizations: InseAccountManagementModelOrganizationProfile[];
|
|
canSelectOrganization: boolean;
|
|
roles: UserRole[];
|
|
}
|
|
|
|
export interface AccountPayload {
|
|
email?: string;
|
|
name?: string;
|
|
roles?: UserRole[];
|
|
organization_id?: string;
|
|
password?: string;
|
|
deleted?: boolean;
|
|
activated?: boolean;
|
|
}
|
|
|
|
export interface CreateAccountPayload {
|
|
email: string;
|
|
name: string;
|
|
roles: UserRole[];
|
|
organization_id: string;
|
|
password: string;
|
|
}
|
|
|
|
export type AccountDialogResult =
|
|
| { mode: 'create'; payload: CreateAccountPayload }
|
|
| { mode: 'edit'; accountId: string; payload: AccountPayload };
|
|
|
|
@Component({
|
|
selector: 'app-account-dialog',
|
|
imports: [
|
|
CommonModule,
|
|
ReactiveFormsModule,
|
|
MatButtonModule,
|
|
MatDialogModule,
|
|
MatFormFieldModule,
|
|
MatIconModule,
|
|
MatInputModule,
|
|
MatSelectModule,
|
|
],
|
|
templateUrl: './account-dialog.component.html',
|
|
styleUrl: './account-dialog.component.scss',
|
|
standalone: true,
|
|
})
|
|
export class AccountDialogComponent {
|
|
private readonly dialogRef =
|
|
inject<
|
|
MatDialogRef<AccountDialogComponent, AccountDialogResult | undefined>
|
|
>(MatDialogRef);
|
|
protected readonly data = inject<AccountDialogData>(MAT_DIALOG_DATA);
|
|
|
|
protected readonly form = new FormGroup({
|
|
email: new FormControl<string>(this.data.account?.email ?? '', [
|
|
Validators.required,
|
|
Validators.email,
|
|
]),
|
|
name: new FormControl<string>(this.data.account?.name ?? '', [
|
|
Validators.required,
|
|
]),
|
|
roles: new FormControl<UserRole[]>(this.data.account?.roles ?? ['USER'], [
|
|
Validators.required,
|
|
]),
|
|
organizationId: new FormControl<string>(
|
|
this.data.account?.organization_id ??
|
|
(this.data.organizations.length === 1
|
|
? this.data.organizations[0].id
|
|
: ''),
|
|
[Validators.required],
|
|
),
|
|
password: new FormControl<string>('', [
|
|
...(this.data.mode === 'create' ? [Validators.required] : []),
|
|
Validators.minLength(8),
|
|
]),
|
|
});
|
|
|
|
protected submit(): void {
|
|
if (this.form.invalid) {
|
|
this.form.markAllAsTouched();
|
|
return;
|
|
}
|
|
|
|
const value = this.form.getRawValue();
|
|
|
|
if (this.data.mode === 'create') {
|
|
this.dialogRef.close({
|
|
mode: 'create',
|
|
payload: {
|
|
email: value.email ?? '',
|
|
name: value.name ?? '',
|
|
roles: value.roles ?? ['USER'],
|
|
organization_id: value.organizationId ?? '',
|
|
password: value.password ?? '',
|
|
},
|
|
});
|
|
return;
|
|
}
|
|
|
|
const account = this.data.account!;
|
|
const payload: AccountPayload = {};
|
|
|
|
const email = (value.email ?? '').trim();
|
|
const name = (value.name ?? '').trim();
|
|
const roles = value.roles ?? account.roles;
|
|
const organizationId = value.organizationId ?? account.organization_id;
|
|
const password = value.password?.trim() ?? '';
|
|
|
|
if (email !== account.email) {
|
|
payload.email = email;
|
|
}
|
|
|
|
if (name !== account.name) {
|
|
payload.name = name;
|
|
}
|
|
|
|
if (!this.sameRoles(roles, account.roles)) {
|
|
payload.roles = roles;
|
|
}
|
|
|
|
if (password) {
|
|
payload.password = password;
|
|
}
|
|
|
|
if (
|
|
this.data.canSelectOrganization &&
|
|
organizationId &&
|
|
organizationId !== account.organization_id
|
|
) {
|
|
payload.organization_id = organizationId;
|
|
}
|
|
|
|
this.dialogRef.close({
|
|
mode: 'edit',
|
|
accountId: account.id,
|
|
payload,
|
|
});
|
|
}
|
|
|
|
protected close(): void {
|
|
this.dialogRef.close();
|
|
}
|
|
|
|
private sameRoles(left: UserRole[], right: UserRole[]): boolean {
|
|
return (
|
|
left.length === right.length && left.every((role) => right.includes(role))
|
|
);
|
|
}
|
|
}
|