morenoh149 / react-native-contacts

React Native Contacts
MIT License
1.64k stars 560 forks source link

How to Edit or Update contact details like familyName, givenName or number #734

Open siddiquesheikh30 opened 5 months ago

siddiquesheikh30 commented 5 months ago

I want to edit some details of my contact like givenName, familyName etc. So how can I do it? This is my code so far -

const EditContactScreen = ({ route, navigation }) => {
    const { data } = route.params;

    const [firstName, setFirstName] = useState(data.givenName);
    const [lastName, setLastName] = useState(data.familyName);
    const [number, setNumber] = useState(data.phoneNumbers[0].number);
    const [email, setEmail] = useState('');
    const [contactId, setContactId] = useState('');

    const editContact = () => {
        PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_CONTACTS, {
            title: 'Contacts',
            message: 'This app would like to view your contacts.',
            buttonPositive: 'Please accept bare mortal',
        })
            .then((res) => {
                console.log('Permission: ', res);
                if (res === 'granted') {
                    let updatedContact = {
                        recordID: data.recordID,
                        emailAddresses: [{
                            label: 'work',
                            email: email,
                        }],
                        phoneNumbers: [{
                            label: 'mobile',
                            number: number,
                        }],
                        familyName: lastName,
                        givenName: firstName,
                    };
                    Contacts.updateContact(updatedContact).then(() => {
                        console.log('Contact updated successfully');
                        navigation.goBack();
                    }).catch(error => {
                        console.log('Error updating contact:', error);
                    });
                }
            })
            .catch((error) => {
                console.error('Permission error: ', error);
            });
    };

    const onSubmit = () => {
        editContact();
    };

    return (
        <View style={styles.container}>
            <TextInput
                defaultValue={firstName}
                onChangeText={text => setFirstName(text)}
            />
            <TextInput
                defaultValue={lastName}
                onChangeText={text => setLastName(text)}
            />
            <TextInput
                defaultValue={number}
                onChangeText={text => setNumber(text)}
            />

                <TouchableHighlight
                    style={[styles.button]}
                    onPress={onSubmit}
                >
                    <Text style={styles.btnText}>Save Contact</Text>
                </TouchableHighlight>
        </View>
    );
};

on calling editContact () it is giving me error as [Error: Invalid recordId or rawContactId] so what should I do?

morenoh149 commented 4 months ago

what's in recordID: data.recordID? are you passing a recordID inside route.params?

siddiquesheikh30 commented 4 months ago

Now it is updating correctly, I have to pass rawContactId, recordId, and id of number present in phoneNumbers[] now my code is working and it is updating the number, givenName, and familyName correctly below is the code for anyone to refer:

const editContact = async () => {
        try {
            const res = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_CONTACTS, {
                title: 'Contacts',
                message: 'This app would like to view your contacts.',
                buttonPositive: 'Please accept bare mortal',
            });
            if (res === PermissionsAndroid.RESULTS.GRANTED) {
                const updatedContact = {
                    recordID: data.recordID,
                    rawContactId: data.rawContactId,
                    phoneNumbers: [{
                        id: data.phoneNumbers[0].id,
                        label: 'mobile',
                        number: number,
                    }],
                    familyName: lastName,
                    givenName: firstName,
                };
                console.log('Updating contact with data: ', updatedContact);
                await Contacts.updateContact(updatedContact);
                console.log('Contact updated successfully');
                // updateContact(updatedContact);
                navigation.goBack();
            } else {
                console.log('Permission denied');
            }
        } catch (error) {
            console.error('Error updating contact: ', error);
        }
    };

Thank you for taking the time and replying to my issue, just one thing if I want to add an image in contact while adding a new contact or updating a contact how should I do it?

github-actions[bot] commented 1 month ago

This issue is stale, please provide more information about the status

LucasWilliams2622 commented 1 month ago

Now it is updating correctly, I have to pass rawContactId, recordId, and id of number present in phoneNumbers[] now my code is working and it is updating the number, givenName, and familyName correctly below is the code for anyone to refer:

const editContact = async () => {
        try {
            const res = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_CONTACTS, {
                title: 'Contacts',
                message: 'This app would like to view your contacts.',
                buttonPositive: 'Please accept bare mortal',
            });
            if (res === PermissionsAndroid.RESULTS.GRANTED) {
                const updatedContact = {
                    recordID: data.recordID,
                    rawContactId: data.rawContactId,
                    phoneNumbers: [{
                        id: data.phoneNumbers[0].id,
                        label: 'mobile',
                        number: number,
                    }],
                    familyName: lastName,
                    givenName: firstName,
                };
                console.log('Updating contact with data: ', updatedContact);
                await Contacts.updateContact(updatedContact);
                console.log('Contact updated successfully');
                // updateContact(updatedContact);
                navigation.goBack();
            } else {
                console.log('Permission denied');
            }
        } catch (error) {
            console.error('Error updating contact: ', error);
        }
    };

Thank you for taking the time and replying to my issue, just one thing if I want to add an image in contact while adding a new contact or updating a contact how should I do it?

Thank you so much :b

misteryomi commented 3 weeks ago

Hey @siddiquesheikh30

You might want to check the docs. There’s a function getPhotoForId(contactId) for that.