-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcontact.service.ts
More file actions
114 lines (99 loc) · 2.61 KB
/
contact.service.ts
File metadata and controls
114 lines (99 loc) · 2.61 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
import * as angular from 'angular';
import {Injectable, Inject} from "@angular/core";
import {downgradeInjectable} from '@angular/upgrade/static';
import {Toaster} from "../ajs-upgraded-providers";
import {Contact} from "./contact.resource";
@Injectable()
export class ContactService {
public page = 1;
public hasMore = true;
public isLoading = false;
public isSaving = false;
public isDeleting = false;
public persons = [];
public search = null;
public sorting = 'name';
public ordering = 'ASC';
constructor(private contact: Contact, @Inject(Toaster) private toaster) {
this.loadContacts();
}
getPerson(email) {
for (let person of this.persons) {
if (person.email === email) {
return person;
}
}
}
doSearch() {
this.hasMore = true;
this.page = 1;
this.persons = [];
this.loadContacts();
}
loadContacts() {
if (this.hasMore && !this.isLoading) {
this.isLoading = true;
let params = {
'_page': this.page,
'_sort': this.sorting,
"_order": this.ordering,
'q': this.search
};
this.contact.query(params).then(res => {
console.log(res);
for (let person of res) {
this.persons.push(person);
}
if (res.length === 0) {
this.hasMore = false;
}
this.isLoading = false;
});
}
};
loadMore() {
if (this.hasMore && !this.isLoading) {
this.page += 1;
this.loadContacts();
}
};
updateContact(person) {
return new Promise((resolve, reject) => {
this.isSaving = true;
this.contact.update(person).then(() => {
this.isSaving = false;
this.toaster.pop('success', 'Updated ' + person.name);
resolve()
});
});
};
removeContact(person) {
return new Promise((resolve, reject) => {
this.isDeleting = true;
this.contact.remove(person).then(() => {
this.isDeleting = false;
var index = this.persons.indexOf(person);
this.persons.splice(index, 1);
this.toaster.pop('success', 'Deleted ' + person.name);
resolve()
});
});
};
createContact(person) {
return new Promise((resolve, reject) => {
this.isSaving = true;
this.contact.save(person).then(() => {
this.isSaving = false;
this.hasMore = true;
this.page = 1;
this.persons = [];
this.loadContacts();
this.toaster.pop('success', 'Created ' + person.name);
resolve()
});
});
};
}
angular
.module('codecraft')
.factory('ContactService', downgradeInjectable(ContactService));