Skip to content
This repository has been archived by the owner on Feb 5, 2023. It is now read-only.

Bugfix/fetch portal name #166

Merged
merged 2 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ import { InstallerModule } from './modules/installer/installer.module';

registerLocaleData(localeRu);

export function initializeUser(appInitService: AppInitService) {
function initializeUser(appInitService: AppInitService) {
return (): Promise<any> => {
return appInitService.getCurrentUser();
};
}

function initializeCompany(appInitService: AppInitService) {
return (): Promise<any> => {
return appInitService.getCompany();
}
}

@NgModule({
declarations: [AppComponent],
imports: [AppRoutingModule, CoreModule, AuthModule, UserModule, AdminModule, EmployeeModule, NgbModule, MaterialModule, InstallerModule],
Expand All @@ -51,6 +57,12 @@ export function initializeUser(appInitService: AppInitService) {
useClass: AuthInterceptor,
multi: true,
},
{
provide: APP_INITIALIZER,
useFactory: initializeCompany,
deps: [AppInitService],
multi: true,
},
{
provide: APP_INITIALIZER,
useFactory: initializeUser,
Expand Down
17 changes: 7 additions & 10 deletions src/app/core/guards/installer.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ import { CompanyService } from '@app/services/company/company.service';
providedIn: 'root',
})
export class InstallerGuard implements CanActivate, CanLoad {
constructor(private _companyService: CompanyService, private router: Router) {}
constructor(private _companyService: CompanyService, private _router: Router) {}

canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this._companyService.getCompany().pipe(
map((result) => {
if (result.body !== null) {
return true;
}
this.router.navigate(['installer']);
return false;
})
);
const company = this._companyService.getCurrentCompany();
if (company) {
return true;
}
this._router.navigate(['installer']);
return false;
}
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return true;
Expand Down
8 changes: 8 additions & 0 deletions src/app/core/services/app-init.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ import { catchError } from 'rxjs/operators';
import { HttpErrorResponse } from '@angular/common/http';
import { throwError } from 'rxjs';
import { AuthService } from '@app/services/auth/auth.service';
import { CompanyService } from '@app/services/company/company.service';

@Injectable({
providedIn: 'root',
})
export class AppInitService {
constructor(
private _userService: UserService,
private _companyService: CompanyService,
private _localStorage: LocalStorageService,
private _authService: AuthService,
) {}

public getCompany(): Promise<any> {
return new Promise((resolve) => {
this._companyService.getCompany().subscribe().add(resolve);
});
}

public getCurrentUser(): Promise<any> {
const token: string | null = this._localStorage.get('access_token');

Expand Down
28 changes: 25 additions & 3 deletions src/app/core/services/company/company.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { BehaviorSubject, Observable } from 'rxjs';
import { IFindRequestEx } from '@app/types/find-request.interface';
import { OperationResultResponse } from '@data/api/company-service/models/operation-result-response';
import { FindResultResponseDepartmentInfo } from '@data/api/company-service/models/find-result-response-department-info';
Expand All @@ -8,19 +8,27 @@ import { CreateCompanyRequest } from '@data/api/company-service/models/create-co
import { IGetCompanyRequest } from '@app/types/get-company-request.interface';
import { OperationResultResponseCompanyInfo } from '@data/api/company-service/models/operation-result-response-company-info';
import { EditCompanyRequest } from '@data/api/company-service/models/edit-company-request';
import { Company, CompanyInfo } from '@app/models/company';
import { tap } from 'rxjs/operators';

@Injectable({
providedIn: 'root',
})
export class CompanyService {
constructor(private _companyService: CompanyApiService) {}
private _company: BehaviorSubject<Company>;

constructor(private _companyService: CompanyApiService) {
this._company = new BehaviorSubject<Company>(null);
}

public createCompany(body: CreateCompanyRequest): Observable<OperationResultResponse> {
return this._companyService.createCompany({ body });
}

public getCompany(params?: IGetCompanyRequest): Observable<OperationResultResponseCompanyInfo> {
return this._companyService.getCompany(params);
return this._companyService
.getCompany(params)
.pipe(tap((response: OperationResultResponseCompanyInfo) => this._setCompany(response.body)));
}

public findOffices(params: IFindRequestEx): Observable<FindResultResponseDepartmentInfo> {
Expand All @@ -30,4 +38,18 @@ export class CompanyService {
public editCompany(body: EditCompanyRequest): Observable<OperationResultResponse> {
return this._companyService.editCompany({ body });
}

public getCurrentCompany(): Company {
return this._company.value;
}

//TODO create separate service with global variables like portal name?
public getPortalName(): string {
return this._company.value.portalName;
}

private _setCompany(companyInfo: CompanyInfo): void {
const company = new Company(companyInfo);
this._company.next(company);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(ngSubmit)="resetPassword()"
autocomplete="off"
>
<h1 doText color='dark_1' class="form__title">Digital Office</h1>
<h1 doText color='dark_1' class="form__title">{{ portalName }}</h1>
<div class="form__error forgot">
<small *ngIf="!isEmailInputValid()">
Неправильно введен email адрес :(</small
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

import { UserService } from '@app/services/user/user.service';
import { CompanyService } from '@app/services/company/company.service';

@Component({
selector: 'do-forgot-password',
templateUrl: './forgot-password.component.html',
styleUrls: ['./forgot-password.component.scss'],
})
export class ForgotPasswordComponent implements OnInit {
public portalName: string;
public forgotPasswordForm: FormGroup;
public forgotPasswordError: string;
public isWaiting = false;
public isCompleted = false;

constructor(private userService: UserService, private formBuilder: FormBuilder) {
constructor(private userService: UserService, private _companyService: CompanyService, private formBuilder: FormBuilder) {
this.portalName = _companyService.getPortalName();
this.forgotPasswordForm = this.formBuilder.group({
email: ['', [Validators.required, Validators.email]],
email: [ '', [ Validators.required, Validators.email ] ],
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/auth/components/login/login.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(ngSubmit)="login()"
autocomplete="off"
>
<h1 doText color='dark_1' class="form__title">Digital Office</h1>
<h1 doText color='dark_1' class="form__title">{{ portalName }}</h1>
<div class="form__error">
<!-- <small *ngIf="email.dirty && email.invalid"-->
<!-- >Неправильно введен email адрес :(-->
Expand Down
31 changes: 22 additions & 9 deletions src/app/modules/auth/components/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,43 @@ import { AuthenticationRequest } from '@data/api/auth-service/models/authenticat
import { OperationResultResponseUserResponse } from '@data/api/user-service/models/operation-result-response-user-response';
import { User } from '@app/models/user/user.model';
import { of } from 'rxjs';
import { CompanyService } from '@app/services/company/company.service';

@Component({
selector: 'do-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss'],
})
export class LoginComponent implements OnInit {
public portalName: string;
public loginForm: FormGroup;
public loginError: string;
public isLoading = false;

constructor(private _authService: AuthService, private _userService: UserService, private _router: Router, private formBuilder: FormBuilder) {
constructor(
private _authService: AuthService,
private _userService: UserService,
private _companyService: CompanyService,
private _router: Router,
private formBuilder: FormBuilder
) {
this.portalName = _companyService.getPortalName();
this.loginForm = this.formBuilder.group({
email: ['', Validators.required],
password: ['', Validators.required],
});
}

public ngOnInit(): void {
this.loginForm.valueChanges.pipe(tap(() => {
if (this.loginForm) {
this.loginError = null;
}
})
).subscribe();
this.loginForm.valueChanges
.pipe(
tap(() => {
if (this.loginForm) {
this.loginError = null;
}
})
)
.subscribe();
}

public login(): void {
Expand All @@ -44,7 +56,9 @@ export class LoginComponent implements OnInit {
password: this.loginForm.get('password').value,
};

this._authService.login(authenticationRequest).pipe(
this._authService
.login(authenticationRequest)
.pipe(
finalize(() => (this.isLoading = false)),
catchError((error) => {
this.loginError = error.message;
Expand All @@ -64,6 +78,5 @@ export class LoginComponent implements OnInit {

public get password() {
return this.loginForm.get('password');

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<form class='form' [formGroup]='loginForm' (ngSubmit)='signUp()' autocomplete='off'>
<h1 doText color='dark_1' class='form__title'>Digital Office</h1>
<h1 doText color='dark_1' class='form__title'>{{ portalName }}</h1>
<div class="form__description"><p doText color='gray1'>Придумайте логин и введите пароль из письма</p></div>
<div class='form__error' *ngIf='loginForm.hasError("busy") || (login.touched && password.touched && loginForm.invalid)'>
<small *ngIf='login.dirty && login.invalid'>Логин обязателен :(</small>
Expand Down
4 changes: 4 additions & 0 deletions src/app/modules/auth/components/signup/signup.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import { CreateCredentialsRequest } from '@data/api/user-service/models/create-c
import { of, throwError } from 'rxjs';
import { OperationResultResponseUserResponse } from '@data/api/user-service/models/operation-result-response-user-response';
import { User } from '@app/models/user/user.model';
import { CompanyService } from '@app/services/company/company.service';

@Component({
selector: 'do-signup',
templateUrl: './signup.component.html',
styleUrls: ['./signup.component.scss'],
})
export class SignupComponent implements OnInit {
public portalName: string;
public userId: string;
public loginForm: FormGroup;
public isWaiting = false;
Expand All @@ -30,10 +32,12 @@ export class SignupComponent implements OnInit {
constructor(
private _authService: AuthService,
private _userService: UserService,
private _companyService: CompanyService,
private _activatedRoute: ActivatedRoute,
private _router: Router,
private _fb: FormBuilder
) {
this.portalName = this._companyService.getPortalName();
this.userId = null;
this.loginForm = this._fb.group({
login: ['', Validators.required],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
<mat-sidenav-container class='sidenav' [hasBackdrop]="false" (click)='onClick($event)'>
<mat-sidenav #menu [autoFocus]='false' [opened]='navOpened' class='sidenav__nav'>
<div class='sidenav__header'>
<h1 doText class='sidenav__title'>Цифровой офис</h1>
<button mat-icon-button doText color='gray1' class='sidenav__close-button' (click)="closeNav()">
<mat-icon>
close
</mat-icon>
<mat-sidenav-container class="sidenav" [hasBackdrop]="false" (click)="onClick($event)">
<mat-sidenav #menu [autoFocus]="false" [opened]="navOpened" class="sidenav__nav">
<div class="sidenav__header">
<h1 doText class="sidenav__title">{{ portalName }}</h1>
<button mat-icon-button doText color="gray1" class="sidenav__close-button" (click)="closeNav()">
<mat-icon> close </mat-icon>
</button>
</div>
<mat-action-list class='sidenav__group'>
<a (click)="closeNav()" doText color='gray1' mat-list-item routerLink='/user/projects-table'>
<span class='sidenav__icon'>
<mat-action-list class="sidenav__group">
<a (click)="closeNav()" doText color="gray1" mat-list-item routerLink="/user/projects-table">
<span class="sidenav__icon">
<mat-icon>desktop_mac</mat-icon>
</span>
<span>
Проекты
</span>
<span> Проекты </span>
</a>
<a (click)="closeNav()" doText color='gray1' mat-list-item routerLink='/admin/manage-users'>
<span class='sidenav__icon'>
<a (click)="closeNav()" doText color="gray1" mat-list-item routerLink="/admin/manage-users">
<span class="sidenav__icon">
<mat-icon>group</mat-icon>
</span>
<span>
Сотрудники
</span>
<span> Сотрудники </span>
</a>
<a (click)="closeNav()" doText color='gray1' mat-list-item routerLink='/departments'>
<span class='sidenav__icon'>
<a (click)="closeNav()" doText color="gray1" mat-list-item routerLink="/departments">
<span class="sidenav__icon">
<mat-icon>groups</mat-icon>
</span>
<span>
Департаменты
</span>
<span> Департаменты </span>
</a>
<a (click)="closeNav()" doText color='gray1' mat-list-item>
<span class='sidenav__icon'>
<a (click)="closeNav()" doText color="gray1" mat-list-item>
<span class="sidenav__icon">
<mat-icon>business</mat-icon>
</span>
<span>
Компании
</span>
<span> Компании </span>
</a>
</mat-action-list>
</mat-sidenav>

<mat-sidenav-content>
<do-header (menuClick)="onMenuClick($event)" [userName]="user.user.firstName" [userId]="user.user.id"
[avatar]="user.user.avatar" class="header__container"></do-header>
<do-header
(menuClick)="onMenuClick($event)"
[userName]="user.user.firstName"
[userId]="user.user.id"
[avatar]="user.user.avatar"
[portalName]="portalName"
class="header__container"
></do-header>
<div class="content-container">
<div class="wrapper">
<router-outlet></router-outlet>
</div>
</div>
</mat-sidenav-content>
</mat-sidenav-container>
</mat-sidenav-container>
Loading