/
feedback.modal.controller.ts
104 lines (91 loc) · 2.69 KB
/
feedback.modal.controller.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
import { module } from 'angular';
import { IModalServiceInstance } from 'angular-ui-bootstrap';
import { AUTHENTICATION_SERVICE, AuthenticationService } from 'core/authentication/authentication.service';
interface IFeedback {
title: string;
description: string;
contact: string;
}
class FeedbackModalController implements ng.IComponentController {
public states = {
EDITING: 0,
SUBMITTING: 1,
SUBMITTED: 2,
ERROR: 3
};
public state: number = this.states.EDITING;
public userIsAuthenticated: boolean;
public feedback: IFeedback;
public issueUrl: string;
public issueId: string;
static get $inject () {
return [
'$location',
'$http',
'$uibModalInstance',
'settings',
'authenticationService'
];
}
constructor (private $location: ng.ILocationService,
private $http: ng.IHttpService,
private $uibModalInstance: IModalServiceInstance,
private settings: any,
private authenticationService: AuthenticationService) {}
public $onInit (): void {
this.userIsAuthenticated = this.authenticationService.getAuthenticatedUser().authenticated;
this.feedback = {
title: '',
description: '',
contact: ''
};
}
private getContactInfo(): string {
if (this.userIsAuthenticated) {
return this.authenticationService.getAuthenticatedUser().name;
}
return this.feedback.contact;
}
private getUserNameFromContactInfo(): string {
const email = this.getContactInfo();
if (email.includes('@')) {
return email.split('@')[0];
}
return email;
}
private buildDescription(): string {
return [
'*Submitted by:*\n' + this.getContactInfo(),
'*From page:*\n' + this.$location.absUrl(),
'*Description:*\n' + this.feedback.description
].join('\n\n');
}
private buildRequestBody(): IFeedback {
return {
title: this.feedback.title,
description: this.buildDescription(),
contact: this.getUserNameFromContactInfo(),
};
}
public submit(): void {
this.state = this.states.SUBMITTING;
this.$http.post(this.settings.feedbackUrl, this.buildRequestBody())
.then((result: any) => {
this.state = this.states.SUBMITTED;
this.issueUrl = result.data.url;
this.issueId = result.data.id;
},
() => {
this.state = this.states.ERROR;
});
};
public cancel(): void {
this.$uibModalInstance.dismiss();
}
}
export const FEEDBACK_MODAL_CONTROLLER = 'spinnaker.netflix.feedback.modal.controller';
module(FEEDBACK_MODAL_CONTROLLER, [
require('core/config/settings'),
AUTHENTICATION_SERVICE
])
.controller('FeedbackModalCtrl', FeedbackModalController);