-
Notifications
You must be signed in to change notification settings - Fork 1
/
person-invite.ts
156 lines (142 loc) · 4.7 KB
/
person-invite.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
import { Component, NgZone } from '@angular/core';
import { IonicPage, Platform, NavParams, NavController, ViewController, ModalController, ToastController, AlertController, LoadingController, ActionSheetController } from 'ionic-angular';
import { BasePrivatePage } from '../../pages/base-private-page/base-private-page';
import { Organization } from '../../models/organization';
import { User } from '../../models/user';
import { Person } from '../../models/person';
import { ApiProvider } from '../../providers/api/api';
import { StorageProvider } from '../../providers/storage/storage';
@IonicPage({
name: 'PersonInvitePage',
segment: 'people/invite',
defaultHistory: ['PersonListPage']
})
@Component({
selector: 'page-person-invite',
templateUrl: 'person-invite.html',
providers: [ ApiProvider, StorageProvider ],
entryComponents:[ ]
})
export class PersonInvitePage extends BasePrivatePage {
person:Person = null;
people:Person[] = null;
loading:boolean = false;
constructor(
protected zone:NgZone,
protected platform:Platform,
protected navParams:NavParams,
protected navController:NavController,
protected viewController:ViewController,
protected modalController:ModalController,
protected toastController:ToastController,
protected alertController:AlertController,
protected loadingController:LoadingController,
protected actionController:ActionSheetController,
protected api:ApiProvider,
protected storage:StorageProvider) {
super(zone, platform, navParams, navController, viewController, modalController, toastController, alertController, loadingController, actionController, storage);
}
ionViewWillEnter() {
super.ionViewWillEnter();
let loading = this.showLoading("Loading...");
this.loadUpdates(true).then((loaded:any) => {
loading.dismiss();
});
}
ionViewDidEnter() {
super.ionViewDidEnter();
if (this.organization) {
this.analytics.trackPage(this, {
organization: this.organization.name
});
}
}
protected loadUpdates(cache:boolean=true, event:any=null) {
this.logger.info(this, "loadUpdates");
this.loading = true;
return Promise.resolve()
.then(() => { return this.loadOrganization(cache); })
.then(() => { return this.loadUser(cache); })
.then(() => { return this.loadPeople(cache); })
.then(() => {
this.logger.info(this, "loadUpdates", "Loaded");
if (event) {
event.complete();
}
this.loading = false;
})
.catch((error:any) => {
this.logger.error(this, "loadUpdates", "Failed", error);
if (event) {
event.complete();
}
this.loading = false;
this.showToast(error);
});
}
private cancelInvite(event:any) {
this.logger.info(this, 'cancelInvite');
this.hideModal();
}
private invitePeople(event:any) {
let invites = [];
for (let person of this.people) {
if (person.selected == true) {
invites.push(this.invitePerson(person));
}
}
if (invites.length > 0) {
let loading = this.showLoading("Inviting...", true);
Promise.all(invites).then(invited => {
loading.dismiss();
this.showToast("Invites sent");
this.hideModal({people: invites});
});
}
else {
this.showToast("No invites sent");
this.hideModal();
}
}
private loadPeople(cache:boolean=true):Promise<Person[]> {
return new Promise((resolve, reject) => {
if (cache && this.mobile) {
this.storage.getPeople(this.organization).then((people:Person[]) => {
this.logger.info(this, "loadPeople", people);
if (people && people.length > 0) {
this.people = people.filter(person => person.needsInvite() == true);
this.logger.info(this, "loadPeople", this.people);
resolve(this.people);
}
else {
this.loadPeople(false).then((people:Person[]) => {
resolve(this.people);
},
(error:any) => {
this.showToast(error);
});
}
});
}
else {
this.api.getPeople(this.organization).then((people:Person[]) => {
this.people = people.filter(person => person.needsInvite() == true);
resolve(this.people);
},
(error:any) => {
this.showToast(error);
});
}
});
}
private invitePerson(person:Person):Promise<Person> {
return new Promise((resolve, reject) => {
this.api.invitePerson(this.organization, person).then((invited:Person) => {
resolve(invited);
},
(error:any) => {
reject(error);
});
});
}
}