generated from navikt/crm-shared-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourseRegistrationForm.js
183 lines (157 loc) · 6.11 KB
/
courseRegistrationForm.js
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
import { LightningElement, track, wire } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import createRegistration from '@salesforce/apex/CourseRegistrationController.createRegistration';
import getCourseFields from '@salesforce/apex/CourseRegistrationController.getCourseFields';
import icons from '@salesforce/resourceUrl/icons';
import houseIconNew from '@salesforce/resourceUrl/houseicon2';
export default class CourseRegistrationForm extends NavigationMixin(LightningElement) {
@track courseId;
@track theRecord = {};
@track output;
@track showForm = false;
@track showConfirmation = false;
@track showError = false;
@track displayErrorMessage = false;
@track errorMessage;
@track message;
@track inputValCode = '';
@track code;
@track dueDate;
@track title;
@track canceled = false;
@track allergies = false;
@track invoiceAdress = false;
@track invoiceReference = false;
@track workplace = false;
@track courseIsFullWarning = false;
@track numberOnWaitinglist;
@track showValidationInput = false;
parameters = {};
@track url;
@track county = false;
@track companyName = false;
@track role = false;
//icons
warningicon = icons + '/warningicon.svg';
informationicon = icons + '/informationicon.svg';
successicon = icons + '/successicon.svg';
erroricon = icons + '/erroricon.svg';
chevrondown = icons + '/chevrondown.svg';
houseicon = houseIconNew;
connectedCallback() {
this.parameters = this.getQueryParameters();
this.courseId = this.parameters.id;
getCourseFields({ courseId: this.courseId }).then((result) => {
if (result) {
this.code = result.InvitationCode__c;
this.title = result.Name;
this.companyName = result.ShowCompany__c;
this.county = result.ShowCounty__c;
this.role = result.ShowRole__c;
this.canceled = result.Cancel__c;
this.allergies = result.ShowAllergies__c;
this.invoiceAdress = result.ShowInvoiceAdress__c;
this.invoiceReference = result.ShowInvoiceReference__c;
this.workplace = result.ShowWorkplace__c;
this.additionalInformation = result.ShowAdditionalInformation__c;
this.dueDate = result.RegistrationDeadline__c;
let registrationDeadline = new Date(this.dueDate);
let dateNow = new Date(Date.now());
this.url = 'https://arbeidsgiver.nav.no/kursoversikt/' + this.courseId;
if (registrationDeadline > dateNow && this.canceled == false) {
this.showForm = true;
} else {
if (!this.canceled) {
this.errorMessage = 'Påmeldingsfristen er passert, det er ikke lenger mulig å melde seg på';
this.displayErrorMessage = true;
} else {
this.errorMessage = 'Kurset er avlyst, det er ikke lenger mulig å melde seg på';
this.displayErrorMessage = true;
}
}
let maxNumberOfParticipants = result.MaxNumberOfParticipants__c;
let numberOfParticipants = result.NumberOfParticipants__c;
this.numberOnWaitinglist = result.Waitinglist__c + 1;
if (numberOfParticipants >= maxNumberOfParticipants) {
this.courseIsFullWarning = true;
}
if (this.code != undefined) {
this.showForm = false;
this.showValidationInput = true;
}
} else {
}
});
}
getQueryParameters() {
var params = {};
var search = location.search.substring(1);
if (search) {
params = JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g, '":"') + '"}', (key, value) => {
return key === '' ? value : decodeURIComponent(value);
});
}
return params;
}
handleChange(event) {
this.theRecord[event.target.name] = event.target.value;
this.showError = false;
}
handleChange2(event) {
this.inputValCode = event.target.value;
}
handleSubmit(event) {
event.preventDefault();
// Basis felter som alltid vises
if (!this.theRecord.firstName || !this.theRecord.lastName || !this.theRecord.email || !this.theRecord.phone) {
this.showError = true;
return;
}
// Tillegs felter sjekk om de er satt
if (this.companyName && !this.theRecord.companyName) {
this.showError = true;
return;
}
if (this.county && !this.theRecord.county) {
this.showError = true;
return;
}
if (this.role && !this.theRecord.role) {
this.showError = true;
return;
}
if (this.invoiceAdress && !this.theRecord.invoiceAdress) {
this.showError = true;
return;
}
if (this.invoiceReference && !this.theRecord.invoiceReference) {
this.showError = true;
return;
}
if (this.workplace && !this.theRecord.workplace) {
this.showError = true;
return;
}
// Alle sjekker er passert om vi kommer hit
let output = JSON.stringify(this.theRecord, null);
createRegistration({
fields: output,
courseId: this.courseId
}).then((result) => {
this.showForm = false;
this.showConfirmation = true;
this.message = result;
});
}
validateCode(event) {
event.preventDefault();
if (this.inputValCode === this.code) {
this.showValidationInput = false;
this.showForm = true;
this.displayErrorMessage = false;
} else {
this.displayErrorMessage = true;
this.errorMessage = 'Koden er ikke gyldig. Vennligst prøv igjen';
}
}
}