-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevents-view.page.ts
More file actions
executable file
·269 lines (251 loc) · 7.99 KB
/
events-view.page.ts
File metadata and controls
executable file
·269 lines (251 loc) · 7.99 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { Component, Output, EventEmitter } from '@angular/core';
import { Tabs, NavParams, NavController, AlertController, LoadingController, ActionSheetController, ToastController } from 'ionic-angular';
// services
import { AssessmentService } from '../../../services/assessment.service';
import { EventService } from '../../../services/event.service';
import { SubmissionService } from '../../../services/submission.service';
// pages
import { AssessmentsGroupPage } from '../../assessments/group/assessments-group.page';
import { AssessmentsPage } from '../../assessments/assessments.page';
import { EventCheckinPage } from '../checkin/event-checkin.page';
import { EventsDownloadPage } from '../download/events-download.page';
import { EventsListPage } from '../list/list.page';
// Others
import { CacheService } from '../../../shared/cache/cache.service';
import { loadingMessages, errMessages } from '../../../app/messages';
import { TranslationService } from '../../../shared/translation/translation.service';
import * as moment from 'moment';
const terms = {
booked: 'Booked'
};
@Component({
templateUrl: './events-view.html'
})
export class EventsViewPage {
booked_text: string = 'Booked';
bookEventErrMessage: any = errMessages.Events.bookEvents.book;
bookingStatus: string = '';
cancelBookingErrMessage: any = errMessages.Events.cancelBooking.cancel;
completedSubmissions: boolean = false;
event: any = {};
justBooked: boolean = false;
loadings: any = { checkin: true };
submissions: Array<any> = [];
constructor(
public actionSheetCtrl: ActionSheetController,
public alertCtrl: AlertController,
public assessmentService: AssessmentService,
public cache: CacheService,
public eventService: EventService,
public loadingCtrl: LoadingController,
public navParams: NavParams,
public navCtrl: NavController,
public submissionService: SubmissionService,
public toastCtrl: ToastController,
public translationService: TranslationService
) {
this.event = navParams.get('event');
}
public availability(event): string {
return (event.isBooked)? terms.booked : event.remaining_capacity + ' of ' + event.capacity + ' seats available';
}
ionViewWillEnter() {
this.loadings.checkin = true;
this.submissions = []; // reset submissions
if (this.event.References) {
this.event = Object.assign(this.event, this.extractAssessment(this.event.References));
}
if (this.event) {
this.bookingStatus = this.availability(this.event);
}
}
ionViewDidEnter() {
this.completedSubmissions = false;
this.submissionService.getSubmissions({
context_id: this.event.context_id
}).subscribe(res => {
this.loadings.checkin = false;
res.forEach(submission => {
submission = this.submissionService.normalise(submission);
this.submissions.push(submission);
if (submission.status === 'done') {
this.completedSubmissions = true;
}
});
}, (err) => {
this.loadings.checkin = false;
console.log(err);
});
}
/**
* @name extractAssessment
* @description each event has only one assessment
* @param {Array} references References array response from get_activity API
*/
extractAssessment(references: Array<any>) {
let ref = references[0];
ref.Assessment.context_id = ref.context_id;
return {
assessment: ref.Assessment,
context_id: ref.context_id
};
}
/**
* Push Download page to ionic nav stack (navigate to attachment download page)
*/
gotoDownload(event) {
this.navCtrl.push(EventsDownloadPage, {event});
}
/**
* Event booking function
* @param {object} event Single event object from get_events API response
*/
checkBookStatus() {
return false ? (this.event.remaining_capacity == this.event.capacity && this.event.isBooked == false) : (this.event.remaining_capacity != this.event.capacity && this.event.isBooked == true)
}
book(event): void {
let earnPoints = this.alertCtrl.create({
message: `<div class="earn-points-box"><h4>Congratulations!</h4><br><img src="./assets/img/success-logo.png" alt="Congratulations logo"><p>You have earned 20 points.</p></div>`,
buttons: [
{
text: 'OK',
role: 'OK',
handler: () => {
console.log('OK, points earned');
}
}
]
});
let bookLoading = this.loadingCtrl.create({
content: 'Booking ..'
});
let bookFailed = this.toastCtrl.create({
message: this.bookEventErrMessage,
duration: 5000,
position: 'bottom'
});
let bookPopup = this.actionSheetCtrl.create({
title: `Do you want to book a seat for ${ this.event.title } at ${ moment.utc(this.event.start).local().format("dddd, MMM D [at] h:mm A") }?`,
buttons:[
{
text: 'Cancel',
role: 'cancel',
handler: () => {
this.bookingStatus = this.availability(this.event);
}
},
{
text: 'Confirm',
role: 'OK',
handler: () => {
bookLoading.present();
this.eventService.bookEvent(this.event.id)
.subscribe(
data => {
this.justBooked = true;
if(this.justBooked == true) {
this.booked_text;
}
bookLoading.dismiss().then(() => {
this.navCtrl.popToRoot(EventsListPage);
});
},
err => {
bookLoading.dismiss().then(() => {
bookFailed.present();
});
}
);
}
},
]
});
bookPopup.present();
}
/**
* @note existence of References array determines if an event is
* a checkin type
* @description examine event contain reference, because we need context ID
* @param {Object} event
*/
hasReference(event) {
if (event.References && event.References.length > 0) {
return true;
}
return false;
}
/**
* @description examine event to allow check in
* @param {Object} event
*/
allowCheckIn(event) {
return moment().isAfter(moment(event.start));
}
/**
* Event checkin action
* @param {Object} event single event object return from get_event API
*/
checkin(event) {
let loading = this.loadingCtrl.create({
content: 'loading checkin...'
});
loading.present().then(() => {
// if submission exist
loading.dismiss().then(() => {
// this.navCtrl.push(AssessmentsGroupPage, {
this.navCtrl.push(AssessmentsPage, {
event,
activity: event.activity,
submissions: this.submissions
});
});
})
}
/**
* Event cancel booking action
* @param
*/
cancelBooking() {
let cancelLoading = this.loadingCtrl.create({
content: 'Cancel Booking ..'
});
let cancelFailed = this.toastCtrl.create({
message: this.cancelBookingErrMessage,
duration: 5000,
position: 'bottom'
});
let cancelBooking = this.actionSheetCtrl.create({
title: 'Cancel Booking Of This Event?',
buttons: [
{
text: 'Cancel Booking',
role: 'destructive',
handler: () => {
cancelLoading.present();
this.eventService.cancelEventBooking(this.event.id)
.subscribe(
data => {
cancelLoading.dismiss().then(() => {
this.navCtrl.popToRoot(EventsListPage);
});
},
err => {
cancelLoading.dismiss().then(() => {
cancelFailed.present();
});
}
)
}
},
{
text: 'Close',
role: 'cancel',
handler: () => {
// console.log('Close this window ..');
}
}
]
});
cancelBooking.present();
}
}