Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_setNewPhoto = async (uri: string) => {
// console.log(this.id, this.state.contact, uri);
try {
await Contacts.updateContactAsync({
[Contacts.Fields.ID]: this.id,
[Contacts.Fields.Image]: uri,
} as any);
} catch ({ message }) {
// tslint:disable-next-line no-console
console.error(message);
}
this.loadAsync();
}
label: item.label || item.localizedService,
};
break;
case Contacts.Fields.UrlAddresses:
transform = {
value: item.url,
onPress: () => {
const webUrl = item.url.indexOf('://') === -1 ? 'http://' + item.url : item.url;
// tslint:disable-next-line no-console
console.log('open', item.url, webUrl);
Linking.openURL(webUrl);
},
};
break;
case Contacts.Fields.Dates:
transform = {
value: ContactUtils.parseDate(item).toDateString(),
};
break;
case Contacts.Fields.Emails:
transform = {
value: item.email,
onPress: () => Linking.openURL(`mailto:${item.email}`),
};
break;
case Contacts.Fields.Addresses:
{
const address = ContactUtils.parseAddress(item);
const targetUriAdress = encodeURI(address);
transform = {
value: address,
const enabled = state.settings.contacts.importEnabled
if (!enabled || !perm) {
if (enabled && !perm) {
logger.info('contact import enabled but no contact permissions')
}
if (enabled === null) {
logger.info("haven't loaded contact import enabled")
}
return
}
// feature enabled and permission granted
let contacts: Contacts.ContactResponse
try {
contacts = await Contacts.getContactsAsync({
fields: [Contacts.Fields.Name, Contacts.Fields.PhoneNumbers, Contacts.Fields.Emails],
})
} catch (e) {
logger.error(`error loading contacts: ${e.message}`)
return SettingsGen.createSetContactImportedCount({error: e.message})
}
let defaultCountryCode: string = ''
try {
defaultCountryCode = await NativeModules.Utils.getDefaultCountryCode()
if (__DEV__ && !defaultCountryCode) {
// behavior of parsing can be unexpectedly different with no country code.
// iOS sim + android emu don't supply country codes, so use this one.
defaultCountryCode = 'us'
}
} catch (e) {
logger.warn(`Error loading default country code: ${e.message}`)
}
const fetchContacts = async (regionFromState: string): Promise<[Array, string]> => {
const contacts = await Contacts.getContactsAsync({
fields: [
Contacts.Fields.Name,
Contacts.Fields.PhoneNumbers,
Contacts.Fields.Emails,
Contacts.Fields.ImageAvailable,
Contacts.Fields.Image,
],
})
let region = ''
if (regionFromState) {
logger.debug(`Got region from state: ${regionFromState}, no need to call NativeModules.`)
region = regionFromState
} else {
try {
let defaultCountryCode = await NativeModules.Utils.getDefaultCountryCode()
if (__DEV__ && !defaultCountryCode) {
// behavior of parsing can be unexpectedly different with no country code.
(async () => {
const { status } = await Contacts.requestPermissionsAsync();
if (status === 'granted') {
const { data } = await Contacts.getContactsAsync({
fields: [Contacts.Fields.Emails],
});
if (data.length > 0) {
const contact = data[0];
console.log(contact);
}
}
})();
}, []);
loadAsync = async ({ distanceFromEnd }: { distanceFromEnd?: number } = {}, restart = false) => {
if (!this.state.permission || this.state.refreshing) {
return;
}
this.setState({ refreshing: true });
const pageOffset = restart ? 0 : this.state.contacts.length || 0;
const pageSize = restart ? Math.max(pageOffset, CONTACT_PAGE_SIZE) : CONTACT_PAGE_SIZE;
const payload = await Contacts.getContactsAsync({
fields: [Contacts.Fields.Name],
sort: Contacts.SortTypes.LastName,
pageSize,
pageOffset,
});
const { data: contacts, hasPreviousPage, hasNextPage } = payload;
if (restart) {
this._rawContacts = {};
}
for (const contact of contacts) {
this._rawContacts[contact.id] = contact;
}
this.setState({
contacts: Object.values(this._rawContacts),
export function parseKey(key: Contacts.FieldType) {
return {
[Contacts.Fields.ID]: 'ID',
[Contacts.Fields.ContactType]: 'Contact Type',
[Contacts.Fields.Name]: 'Name',
[Contacts.Fields.FirstName]: 'First Name',
[Contacts.Fields.MiddleName]: 'Middle Name',
[Contacts.Fields.LastName]: 'Last Name',
[Contacts.Fields.MaidenName]: 'Maiden Name',
[Contacts.Fields.NamePrefix]: 'Prefix',
[Contacts.Fields.NameSuffix]: 'Suffix',
[Contacts.Fields.Nickname]: 'Nickname',
[Contacts.Fields.PhoneticFirstName]: 'Phonetic First Name',
[Contacts.Fields.PhoneticMiddleName]: 'Phonetic Middle Name',
[Contacts.Fields.PhoneticLastName]: 'Phonetic Last Name',
[Contacts.Fields.Birthday]: 'Birthday',
[Contacts.Fields.NonGregorianBirthday]: 'Non-Gregorian Birthday',
[Contacts.Fields.Emails]: 'Email Addresses',
[Contacts.Fields.PhoneNumbers]: 'Phone Numbers',
export async function getContacts() {
const { status } = await Permissions.askAsync(Permissions.CONTACTS);
if (status !== "granted") {
return [];
}
const { data } = await Contacts.getContactsAsync({
fields: [Contacts.Fields.Name, Contacts.Fields.PhoneNumbers]
});
if (!data.length) {
return [];
}
const contacts = data
.reduce((contacts, item) => {
const hasUSNumber =
item.phoneNumbers &&
item.phoneNumbers.length &&
item.phoneNumbers[0] &&
item.phoneNumbers[0].countryCode === "us";
if (hasUSNumber) {
const number = item.phoneNumbers[0].digits.substr(-10);
[Contacts.Fields.Nickname]: 'Nickname',
[Contacts.Fields.PhoneticFirstName]: 'Phonetic First Name',
[Contacts.Fields.PhoneticMiddleName]: 'Phonetic Middle Name',
[Contacts.Fields.PhoneticLastName]: 'Phonetic Last Name',
[Contacts.Fields.Birthday]: 'Birthday',
[Contacts.Fields.NonGregorianBirthday]: 'Non-Gregorian Birthday',
[Contacts.Fields.Emails]: 'Email Addresses',
[Contacts.Fields.PhoneNumbers]: 'Phone Numbers',
[Contacts.Fields.Addresses]: 'Addresses',
[Contacts.Fields.SocialProfiles]: 'Social Profiles',
[Contacts.Fields.InstantMessageAddresses]: 'Instant Message Addresses',
[Contacts.Fields.UrlAddresses]: 'URL Addresses',
[Contacts.Fields.Company]: 'Company',
[Contacts.Fields.JobTitle]: 'Job Title',
[Contacts.Fields.Department]: 'Department',
[Contacts.Fields.ImageAvailable]: 'Image Available',
[Contacts.Fields.Image]: 'Image',
[Contacts.Fields.RawImage]: 'Raw Image',
[Contacts.Fields.Note]: 'Note',
[Contacts.Fields.Dates]: 'Dates',
[Contacts.Fields.Relationships]: 'Relationships',
}[key];
}