Skip to content

Commit

Permalink
refactor(HeaderAccountMenu): Convert to standalone (#1810)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreykwan authored May 22, 2024
1 parent db69558 commit 385ff5a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { MatMenuModule } from '@angular/material/menu';
import { ConfigService } from '../../../services/config.service';
import { Observable } from 'rxjs';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { Config } from '../../../domain/config';
import { provideRouter } from '@angular/router';

export class MockConfigService {
getConfig(): Observable<Config> {
Expand All @@ -29,10 +29,8 @@ describe('HeaderAccountMenuComponent', () => {
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [HeaderAccountMenuComponent],
imports: [HttpClientTestingModule, MatMenuModule],
providers: [{ provide: ConfigService, useClass: MockConfigService }],
schemas: [NO_ERRORS_SCHEMA]
imports: [HeaderAccountMenuComponent, HttpClientTestingModule, MatMenuModule],
providers: [{ provide: ConfigService, useClass: MockConfigService }, provideRouter([])]
}).compileComponents();
})
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
import { Component, OnInit, Input } from '@angular/core';
import { Component, OnInit, Input, SimpleChanges } from '@angular/core';
import { ConfigService } from '../../../services/config.service';
import { User } from '../../../domain/user';
import { HttpClient } from '@angular/common/http';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatMenuModule } from '@angular/material/menu';
import { MatDividerModule } from '@angular/material/divider';
import { RouterModule } from '@angular/router';
import { CommonModule } from '@angular/common';
import { FlexLayoutModule } from '@angular/flex-layout';

@Component({
selector: 'app-header-account-menu',
templateUrl: './header-account-menu.component.html',
styleUrls: ['./header-account-menu.component.scss']
styleUrl: './header-account-menu.component.scss',
standalone: true,
imports: [
CommonModule,
FlexLayoutModule,
MatButtonModule,
MatIconModule,
MatMenuModule,
MatDividerModule,
RouterModule
]
})
export class HeaderAccountMenuComponent implements OnInit {
@Input()
user: User;
protected firstName: string = '';
protected isPreviousAdmin: boolean;
protected lastName: string = '';
protected logOutURL: string;
protected roles: string[] = [];
private switchToOriginalUserURL = '/api/logout/impersonate';
@Input() user: User;

firstName: string = '';
lastName: string = '';
roles: string[] = [];
isPreviousAdmin: boolean = false;
logOutURL: string;
switchToOriginalUserURL = '/api/logout/impersonate';
constructor(private configService: ConfigService, private http: HttpClient) {}

constructor(private configService: ConfigService, private http: HttpClient) {
this.configService = configService;
}

ngOnInit() {
ngOnInit(): void {
this.configService.getConfig().subscribe((config) => {
if (config != null) {
this.logOutURL = config.logOutURL;
}
this.logOutURL = config.logOutURL;
});
}

ngOnChanges(changes) {
ngOnChanges(changes: SimpleChanges): void {
if (changes.user) {
const user = changes.user.currentValue;
if (user) {
Expand All @@ -43,17 +54,17 @@ export class HeaderAccountMenuComponent implements OnInit {
}
}

switchToAdmin() {
protected switchToAdmin(): void {
window.location.href = '/admin';
}

switchToOriginalUser() {
protected switchToOriginalUser(): void {
this.http.post(this.switchToOriginalUserURL, {}).subscribe(() => {
window.location.href = '/teacher';
});
}

logOut() {
protected logOut(): void {
this.http.get(this.logOutURL).subscribe(() => {
window.location.href = '/';
});
Expand Down
7 changes: 4 additions & 3 deletions src/app/modules/header/header.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ const materialModules = [

@NgModule({
imports: [
AppRoutingModule,
CommonModule,
HeaderLinksComponent,
FlexLayoutModule,
AppRoutingModule,
HeaderAccountMenuComponent,
HeaderLinksComponent,
materialModules
],
declarations: [HeaderComponent, HeaderAccountMenuComponent],
declarations: [HeaderComponent],
providers: [ConfigService, UserService],
exports: [HeaderComponent]
})
Expand Down

0 comments on commit 385ff5a

Please sign in to comment.