/
message.component.ts
209 lines (182 loc) · 6.13 KB
/
message.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { Location } from '@angular/common';
import { Component, Input, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { ApiResponseError } from '@dasch-swiss/dsp-js';
import { StatusMsg } from 'src/assets/http/statusMsg';
/**
* data type for messages
*/
export class AppMessageData {
status: number;
statusMsg?: string;
statusText?: string;
type?: string;
route?: string;
footnote?: string;
errorInfo?: string;
url?: string;
}
@Component({
selector: 'app-message',
templateUrl: './message.component.html',
styleUrls: ['./message.component.scss']
})
export class MessageComponent implements OnInit {
/**
* message type: AppMessageData
*
* @param message This type needs at least a status number (0-599).
* In this case, or if type is ApiResponseError, it takes the default status messages
* from the list of HTTP status codes (https://en.wikipedia.org/wiki/List_of_HTTP_status_codes)
*/
@Input() message: AppMessageData = new AppMessageData();
/**
* message type: ApiResponseError
* @param apiError
*/
@Input() apiError?: ApiResponseError;
/**
* size of the message: long, medium or short?
* @param size Default size is 'long'
*/
@Input() size: 'short' | 'medium' | 'long' = 'long';
/**
* @param duration How long should the message be displayed
*/
@Input() duration?: number;
statusMsg: any;
isLoading = true;
showLinks = false;
// disable message
disable = false;
/**
* default link list, which will be used in message content to give a user some possibilities
* what he can do in the case of an error
*
*/
links: any = {
title: 'You have the following possibilities now',
list: [
{
label: 'go to the start page',
route: '/',
icon: 'keyboard_arrow_right'
},
{
label: 'try to login',
route: '/login',
icon: 'keyboard_arrow_right'
},
{
label: 'go back',
route: '<--',
icon: 'keyboard_arrow_left'
}
]
};
constructor(
private _router: Router,
private _location: Location,
private _activatedRoute: ActivatedRoute,
private _status: StatusMsg
) { }
ngOnInit() {
if (this.apiError) {
this.message.status = this.apiError.status;
}
this.statusMsg = this._status.default;
if (!this.message) {
this._activatedRoute.data.subscribe((data: any) => {
this.message.status = data.status;
});
}
this.message = this.setMessage(this.message);
this.isLoading = false;
if (this.duration) {
setTimeout(() => this.disable = true, this.duration);
}
}
setMessage(msg: AppMessageData) {
const tmpMsg: AppMessageData = {} as AppMessageData;
const s: number = msg.status === 0 ? 503 : msg.status;
tmpMsg.status = s;
tmpMsg.route = msg.route;
tmpMsg.statusMsg = msg.statusMsg;
tmpMsg.statusText = msg.statusText;
tmpMsg.route = msg.route;
tmpMsg.footnote = msg.footnote;
switch (true) {
case s > 0 && s < 300:
// the message is a note
tmpMsg.type = 'note';
tmpMsg.statusMsg =
msg.statusMsg !== undefined
? msg.statusMsg
: this.statusMsg[s].message;
tmpMsg.statusText =
msg.statusText !== undefined
? msg.statusText
: this.statusMsg[s].description;
// console.log('the message is a note');
break;
case s >= 300 && s < 400:
// the message is a warning
tmpMsg.type = 'warning';
tmpMsg.statusMsg =
msg.statusMsg !== undefined
? msg.statusMsg
: this.statusMsg[s].message;
tmpMsg.statusText =
msg.statusText !== undefined
? msg.statusText
: this.statusMsg[s].description;
// console.log('the message is a warning');
break;
case s >= 400 && s < 500:
// the message is a client side (app) error
// console.error('the message is a client side (app) error', s);
tmpMsg.type = 'error';
tmpMsg.statusMsg =
msg.statusMsg !== undefined
? msg.statusMsg
: this.statusMsg[s].message;
tmpMsg.statusText =
msg.statusText !== undefined
? msg.statusText
: this.statusMsg[s].description;
this.showLinks = (this.size === 'long');
break;
case s >= 500 && s < 600:
// the message is a server side (api) error
// console.error('the message is a server side (api) error');
tmpMsg.type = 'error';
tmpMsg.statusMsg =
msg.statusMsg !== undefined
? msg.statusMsg
: this.statusMsg[s].message;
tmpMsg.statusText =
msg.statusText !== undefined
? msg.statusText
: this.statusMsg[s].description;
this.showLinks = false;
break;
default:
// no default configuration?
break;
}
return tmpMsg;
}
goToLocation(route: string) {
if (route === '<--') {
this._location.back();
} else {
this._router.navigate([route]);
}
}
closeMessage() {
this.disable = !this.disable;
}
reload() {
window.location.reload();
}
}