This repository has been archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
SelectContact.ts
96 lines (85 loc) · 3.69 KB
/
SelectContact.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
// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// BEGIN EXTRA CODE
// END EXTRA CODE
/**
* @returns {Promise.<MxObject>}
*/
export async function SelectContact(): Promise<mendix.lib.MxObject | void> {
// BEGIN USER CODE
// Documentation: https://github.com/apache/cordova-plugin-contacts
return new Promise((resolve, reject) => {
if (!navigator.contacts) {
return reject(
new Error("SelectContact action requires cordova-plugin-contacts to be installed in the app")
);
}
navigator.contacts.pickContact(
contact => {
createMxObject(contact)
.then(object => resolve(object))
.catch(error => reject(error));
},
error =>
error.code === ContactErrorCode.OPERATION_CANCELLED_ERROR ? resolve() : reject(errorMessage(error))
);
});
function createMxObject(contact: Contact): Promise<mendix.lib.MxObject> {
return new Promise((resolve, reject) => {
mx.data.create({
entity: "HybridMobileActions.Contact",
callback: mxObject => {
mxObject.set("ContactID", contact.id);
const name = contact.displayName || contact.nickname || (contact.name && contact.name.formatted);
if (name) {
mxObject.set("DisplayName", name);
}
if (contact.name && contact.name.givenName) {
mxObject.set("FirstName", contact.name.givenName);
}
if (contact.name && contact.name.middleName) {
mxObject.set("MiddleName", contact.name.middleName);
}
if (contact.name && contact.name.familyName) {
mxObject.set("LastName", contact.name.familyName);
}
const email = contact.emails && contact.emails[0].value;
if (email) {
mxObject.set("Email", email);
}
const phoneNumber = contact.phoneNumbers && contact.phoneNumbers[0].value;
if (phoneNumber) {
mxObject.set("PhoneNumber", phoneNumber);
}
resolve(mxObject);
},
error: () => reject(new Error("Could not create 'HybridMobileActions.Contact' object"))
});
});
}
function errorMessage(error: ContactError): string {
switch (error.code) {
case ContactErrorCode.UNKNOWN_ERROR:
return "Found an unknown error while handling the request.";
case ContactErrorCode.INVALID_ARGUMENT_ERROR:
return "Invalid argument found.";
case ContactErrorCode.TIMEOUT_ERROR:
return "Operation timed out.";
case ContactErrorCode.PENDING_OPERATION_ERROR:
return "Pending operation error.";
case ContactErrorCode.IO_ERROR:
return "IO error encountered.";
case ContactErrorCode.NOT_SUPPORTED_ERROR:
return "Operation not supported.";
case ContactErrorCode.PERMISSION_DENIED_ERROR:
return "Permission denied.";
}
return "Error code undefined";
}
// END USER CODE
}