Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
siddiquesheikh30 opened this issue Apr 22, 2024 · 1 comment

Comments

@siddiquesheikh30
Copy link

siddiquesheikh30 commented Apr 22, 2024

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants