Skip to content

Commit c57c6ef

Browse files
TomHAndersonmathisGarberg
authored andcommitted
Feature/code refactor part1 (mathisGarberg#58)
* Minor edits; cleaned up unused constructors * Renamed ILoginContext (vb code style) to LoginContextInterface (php style); do not export interface as it is not used * Removed orphan index.ts * Add constructor to theme service; remove unused observable * Refactored core * Refactored data * Refactored layout * Refactored shared
1 parent b563323 commit c57c6ef

24 files changed

+172
-200
lines changed

src/app/core/core.module.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,22 @@ import { NgxSpinnerModule } from 'ngx-spinner';
1111

1212

1313
@NgModule({
14-
imports: [
15-
HttpClientModule,
16-
NgxSpinnerModule
17-
],
18-
providers: [
19-
AuthGuard,
20-
NoAuthGuard,
21-
22-
{
23-
provide: HTTP_INTERCEPTORS,
24-
useClass: TokenInterceptor,
25-
multi: true
26-
}
27-
]
14+
imports: [
15+
HttpClientModule,
16+
NgxSpinnerModule
17+
],
18+
providers: [
19+
AuthGuard,
20+
NoAuthGuard,
21+
{
22+
provide: HTTP_INTERCEPTORS,
23+
useClass: TokenInterceptor,
24+
multi: true
25+
}
26+
]
2827
})
2928
export class CoreModule {
30-
constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
31-
throwIfAlreadyLoaded(parentModule, 'CoreModule');
32-
}
29+
constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
30+
throwIfAlreadyLoaded(parentModule, 'CoreModule');
31+
}
3332
}

src/app/core/guard/auth.guard.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import { CanActivate } from '@angular/router';
33

44
@Injectable()
55
export class AuthGuard implements CanActivate {
6-
7-
constructor() {}
8-
9-
canActivate(): boolean {
10-
return true;
11-
}
12-
6+
canActivate(): boolean {
7+
return true;
8+
}
139
}

src/app/core/guard/no-auth.guard.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import { CanActivate } from '@angular/router';
33

44
@Injectable()
55
export class NoAuthGuard implements CanActivate {
6-
7-
constructor() {}
8-
9-
canActivate(): boolean {
10-
return true;
11-
}
12-
6+
canActivate(): boolean {
7+
return true;
8+
}
139
}

src/app/core/interceptor/token.interceptor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ export class TokenInterceptor implements HttpInterceptor {
1212
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
1313
return next.handle(request);
1414
}
15-
}
15+
}

src/app/core/logger.service.ts

Lines changed: 74 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -35,80 +35,81 @@
3535
* LogLevel.Off is never emitted and only used with Logger.level property to disable logs.
3636
*/
3737
export enum LogLevel {
38-
Off = 0,
39-
Error,
40-
Warning,
41-
Info,
42-
Debug
43-
}
38+
Off = 0,
39+
Error,
40+
Warning,
41+
Info,
42+
Debug
43+
}
4444

45+
/**
46+
* Log output handler function.
47+
*/
48+
export type LogOutput = (source: string, level: LogLevel, ...objects: any[]) => void;
49+
50+
export class Logger {
51+
4552
/**
46-
* Log output handler function.
53+
* Current logging level.
54+
* Set it to LogLevel.Off to disable logs completely.
4755
*/
48-
export type LogOutput = (source: string, level: LogLevel, ...objects: any[]) => void;
49-
50-
export class Logger {
51-
52-
/**
53-
* Current logging level.
54-
* Set it to LogLevel.Off to disable logs completely.
55-
*/
56-
static level = LogLevel.Debug;
57-
58-
/**
59-
* Additional log outputs.
60-
*/
61-
static outputs: LogOutput[] = [];
62-
63-
/**
64-
* Enables production mode.
65-
* Sets logging level to LogLevel.Warning.
66-
*/
67-
static enableProductionMode() {
68-
Logger.level = LogLevel.Warning;
69-
}
70-
71-
constructor(private source?: string) { }
72-
73-
/**
74-
* Logs messages or objects with the debug level.
75-
* Works the same as console.log().
76-
*/
77-
debug(...objects: any[]) {
78-
this.log(console.log, LogLevel.Debug, objects);
79-
}
80-
81-
/**
82-
* Logs messages or objects with the info level.
83-
* Works the same as console.log().
84-
*/
85-
info(...objects: any[]) {
86-
this.log(console.info, LogLevel.Info, objects);
87-
}
88-
89-
/**
90-
* Logs messages or objects with the warning level.
91-
* Works the same as console.log().
92-
*/
93-
warn(...objects: any[]) {
94-
this.log(console.warn, LogLevel.Warning, objects);
95-
}
96-
97-
/**
98-
* Logs messages or objects with the error level.
99-
* Works the same as console.log().
100-
*/
101-
error(...objects: any[]) {
102-
this.log(console.error, LogLevel.Error, objects);
103-
}
104-
105-
private log(func: Function, level: LogLevel, objects: any[]) {
106-
if (level <= Logger.level) {
107-
const log = this.source ? ['[' + this.source + ']'].concat(objects) : objects;
108-
func.apply(console, log);
109-
Logger.outputs.forEach((output) => output.apply(output, [this.source, level].concat(objects)));
110-
}
111-
}
112-
56+
static level = LogLevel.Debug;
57+
58+
/**
59+
* Additional log outputs.
60+
*/
61+
static outputs: LogOutput[] = [];
62+
63+
/**
64+
* Enables production mode.
65+
* Sets logging level to LogLevel.Warning.
66+
*/
67+
static enableProductionMode() {
68+
Logger.level = LogLevel.Warning;
69+
}
70+
71+
constructor(
72+
private source?: string
73+
) {}
74+
75+
/**
76+
* Logs messages or objects with the debug level.
77+
* Works the same as console.log().
78+
*/
79+
debug(...objects: any[]) {
80+
this.log(console.log, LogLevel.Debug, objects);
11381
}
114-
82+
83+
/**
84+
* Logs messages or objects with the info level.
85+
* Works the same as console.log().
86+
*/
87+
info(...objects: any[]) {
88+
this.log(console.info, LogLevel.Info, objects);
89+
}
90+
91+
/**
92+
* Logs messages or objects with the warning level.
93+
* Works the same as console.log().
94+
*/
95+
warn(...objects: any[]) {
96+
this.log(console.warn, LogLevel.Warning, objects);
97+
}
98+
99+
/**
100+
* Logs messages or objects with the error level.
101+
* Works the same as console.log().
102+
*/
103+
error(...objects: any[]) {
104+
this.log(console.error, LogLevel.Error, objects);
105+
}
106+
107+
private log(func: Function, level: LogLevel, objects: any[]) {
108+
if (level <= Logger.level) {
109+
const log = this.source ? ['[' + this.source + ']'].concat(objects) : objects;
110+
func.apply(console, log);
111+
Logger.outputs.forEach((output) =>
112+
output.apply(output, [this.source, level].concat(objects)));
113+
}
114+
}
115+
}

src/app/core/service/auth.service.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { of, Observable, throwError } from 'rxjs';
33

44
import { User } from '../../data/schema/user';
55

6-
export class ILoginContext {
6+
interface LoginContextInterface {
77
username: string;
88
password: string;
99
token: string;
@@ -21,12 +21,11 @@ const defaultUser = {
2121
export class AuthService {
2222
token: string;
2323

24-
constructor() { }
25-
26-
login(loginContext: ILoginContext): Observable<User> {
24+
login(loginContext: LoginContextInterface): Observable<User> {
2725
if (
2826
loginContext.username === defaultUser.username &&
29-
loginContext.password === defaultUser.password) {
27+
loginContext.password === defaultUser.password
28+
) {
3029
return of(defaultUser);
3130
}
3231

@@ -40,5 +39,4 @@ export class AuthService {
4039
getToken() {
4140
return this.getToken;
4241
}
43-
4442
}

src/app/core/service/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/app/core/service/theme.service.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { Injectable } from '@angular/core';
22
import { Subject } from 'rxjs/Subject';
3+
import { Observable } from 'rxjs';
34

45
@Injectable({
56
providedIn: 'root'
67
})
78
export class ThemeService {
8-
private darkTheme: Subject<boolean> = new Subject<boolean>();
9-
isDarkTheme = this.darkTheme.asObservable();
9+
private isDarkTheme: Subject<boolean>;
10+
11+
constructor() {
12+
this.isDarkTheme = new Subject<boolean>();
13+
this.isDarkTheme.next(false);
14+
}
1015

1116
setDarkTheme(isDarkTheme: boolean) {
12-
this.darkTheme.next(isDarkTheme);
17+
this.isDarkTheme.next(isDarkTheme);
18+
}
19+
20+
getDarkTheme(): Observable<boolean> {
21+
return this.isDarkTheme;
1322
}
1423
}

src/app/data/data.module.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { NgModule } from '@angular/core';
22
import { CommonModule } from '@angular/common';
33

4-
5-
64
@NgModule({
75
declarations: [],
86
imports: [

src/app/data/schema/project.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export class Project {
2-
link: string;
3-
title: string;
4-
thumbnail: string;
2+
link: string;
3+
title: string;
4+
thumbnail: string;
55
}

src/app/data/service/json-api.service.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { HttpClient } from '@angular/common/http';
3-
import { Observable, throwError, of } from 'rxjs';
4-
import { delay, catchError } from 'rxjs/operators';
5-
import { environment } from '@env/environment';
2+
import { Observable, of } from 'rxjs';
63
import data from './json/data.json';
74

85
@Injectable({

src/app/data/service/project.service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Injectable } from '@angular/core';
22
import { Observable } from 'rxjs';
33

44
import { Project } from '../schema/project';
5-
import { ApiService } from './api.service';
65
import { JsonApiService } from './json-api.service';
76

87
@Injectable({

src/app/data/service/user.service.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
import { Injectable } from '@angular/core';
2-
32
import { JsonApiService } from './json-api.service';
43

5-
const routes = {
6-
users: '/users'
7-
};
8-
94
@Injectable({
105
providedIn: 'root'
116
})
127
export class UserService {
13-
148
constructor(
159
private jsonApiService: JsonApiService
1610
) {}
17-
18-
getAll() {
19-
}
2011
}

src/app/layout/auth-layout/auth-layout.component.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
<div class="container-fluid full-width-image">
32
<div class="auth-container">
43
<div class="auth-box">
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component } from '@angular/core';
22

33
@Component({
44
selector: 'app-auth-layout',
55
templateUrl: './auth-layout.component.html',
66
styleUrls: ['./auth-layout.component.scss']
77
})
8-
export class AuthLayoutComponent implements OnInit {
9-
10-
constructor() { }
11-
12-
ngOnInit() {
13-
}
14-
8+
export class AuthLayoutComponent {
159
}

src/app/layout/content-layout/content-layout.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
<router-outlet></router-outlet>
77
</div>
88

9-
<app-footer (changeTheme)="onThemeChange($event)"></app-footer>
9+
<app-footer></app-footer>
1010
</div>
1111
</div>

0 commit comments

Comments
 (0)