2023-01-01 16:30:09 +01:00
|
|
|
import { ViewChild, ViewContainerRef, Component, OnDestroy, OnInit } from "@angular/core";
|
|
|
|
import { Subject, takeUntil } from "rxjs";
|
2018-06-21 17:43:50 +02:00
|
|
|
|
2023-01-01 16:30:09 +01:00
|
|
|
import { ModalService } from "@bitwarden/angular/services/modal.service";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
|
|
|
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
|
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { KeyConnectorService } from "@bitwarden/common/abstractions/keyConnector.service";
|
|
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
|
|
|
import { StateService } from "@bitwarden/common/abstractions/state.service";
|
2022-10-18 19:01:42 +02:00
|
|
|
import { UpdateProfileRequest } from "@bitwarden/common/models/request/update-profile.request";
|
|
|
|
import { ProfileResponse } from "@bitwarden/common/models/response/profile.response";
|
2018-06-21 17:43:50 +02:00
|
|
|
|
2023-01-01 16:30:09 +01:00
|
|
|
import { ChangeAvatarComponent } from "./change-avatar.component";
|
|
|
|
|
2018-06-21 17:43:50 +02:00
|
|
|
@Component({
|
|
|
|
selector: "app-profile",
|
|
|
|
templateUrl: "profile.component.html",
|
|
|
|
})
|
2023-01-01 16:30:09 +01:00
|
|
|
export class ProfileComponent implements OnInit, OnDestroy {
|
2018-06-21 17:43:50 +02:00
|
|
|
loading = true;
|
|
|
|
profile: ProfileResponse;
|
2018-11-15 05:13:50 +01:00
|
|
|
fingerprint: string;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2018-06-21 17:43:50 +02:00
|
|
|
formPromise: Promise<any>;
|
2023-01-01 16:30:09 +01:00
|
|
|
@ViewChild("avatarModalTemplate", { read: ViewContainerRef, static: true })
|
|
|
|
avatarModalRef: ViewContainerRef;
|
|
|
|
private destroy$ = new Subject<void>();
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-12-14 17:10:26 +01:00
|
|
|
constructor(
|
|
|
|
private apiService: ApiService,
|
|
|
|
private i18nService: I18nService,
|
|
|
|
private platformUtilsService: PlatformUtilsService,
|
|
|
|
private cryptoService: CryptoService,
|
|
|
|
private logService: LogService,
|
|
|
|
private keyConnectorService: KeyConnectorService,
|
2023-01-01 16:30:09 +01:00
|
|
|
private stateService: StateService,
|
|
|
|
private modalService: ModalService
|
2021-12-14 17:10:26 +01:00
|
|
|
) {}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2018-06-21 17:43:50 +02:00
|
|
|
async ngOnInit() {
|
|
|
|
this.profile = await this.apiService.getProfile();
|
|
|
|
this.loading = false;
|
2021-12-14 17:10:26 +01:00
|
|
|
const fingerprint = await this.cryptoService.getFingerprint(
|
|
|
|
await this.stateService.getUserId()
|
|
|
|
);
|
2018-11-15 05:13:50 +01:00
|
|
|
if (fingerprint != null) {
|
|
|
|
this.fingerprint = fingerprint.join("-");
|
2018-06-21 17:43:50 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2023-01-01 16:30:09 +01:00
|
|
|
async ngOnDestroy() {
|
|
|
|
this.destroy$.next();
|
|
|
|
this.destroy$.complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
async openChangeAvatar() {
|
|
|
|
const modalOpened = await this.modalService.openViewRef(
|
|
|
|
ChangeAvatarComponent,
|
|
|
|
this.avatarModalRef,
|
|
|
|
(modal) => {
|
|
|
|
modal.profile = this.profile;
|
|
|
|
modal.changeColor.pipe(takeUntil(this.destroy$)).subscribe(() => {
|
|
|
|
modalOpened[0].close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:43:50 +02:00
|
|
|
async submit() {
|
|
|
|
try {
|
|
|
|
const request = new UpdateProfileRequest(this.profile.name, this.profile.masterPasswordHint);
|
|
|
|
this.formPromise = this.apiService.putProfile(request);
|
|
|
|
await this.formPromise;
|
2021-12-07 20:41:45 +01:00
|
|
|
this.platformUtilsService.showToast("success", null, this.i18nService.t("accountUpdated"));
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
2018-06-21 17:43:50 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-06-21 17:43:50 +02:00
|
|
|
}
|