import React from 'react'; import { View, StyleSheet, Button, TextInput, TouchableOpacity, Text, Alert, ScrollView } from 'react-native'; import { CheckBox, ButtonGroup, Overlay } from '@rneui/themed'; import { connect } from 'react-redux'; import Icon from 'react-native-vector-icons/FontAwesome'; import { showMessage } from 'react-native-flash-message'; import Helpers from "../Controllers/Helpers"; import i18n from "../I18n"; import { BarCodeScanner } from 'expo-barcode-scanner'; import vCard from 'vcard-parser'; import { Picker } from '@react-native-picker/picker'; function mapStateToProps(state) { return { contacts: state.contacts, currentUser: state.currentUser, }; } let date = new Date().getDate(); if (date < 10) { date = '0' + date.toString(); } let month = new Date().getMonth() + 1; if (month < 10) { month = '0' + month.toString(); } const year = new Date().getFullYear(); class ContactDetailsScreen extends React.Component { state = { action: 'add', visits: [], selectedVisit: 0, tmpVisitNote: '', newVisitTitle: date + '/' + month + '/' + year, selectedIndex: 0, showNewVisit: false, tmpID: 0, tmpNote: '', tmpName: '', tmpFirstname: '', tmpEmail: '', tmpCompany: '', tmpAddress: '', tmpPhone: '', tmpSelect1: '', tmpSelect2: '', tmpOther: '', clientChecked: false, prospectChecked: false, otherChecked: false, p1: false, p2: false, p3: false, p4: false, p5: false, p6: false, p7: false, p8: false, p9: false, showQRcodevCard: false, parsedType: null, parsedDatas: [], hasPermission: null, scanned: false, showDynContact: false, parsedName: '', parsedFirstname: '', parsedEmail: '', parsedCompany: '', parsedAddress: '', parsedPhone: '' }; updateContact() { let error = ''; const { action } = this.props.route.params; // On récupère le texte courant if (this.state.visits.length > 0) { this.state.visits[ this.state.selectedVisit ].note = this.state.tmpVisitNote; } let type; if (this.state.clientChecked === true) { type = 'client'; } else if (this.state.prospectChecked === true) { type = 'prospect'; } else if (this.state.otherChecked === true) { type = 'other'; if (this.state.tmpOther.length === 0) { error = error.concat(i18n.t('error.fill_other_field') + '.\n'); } } else { error = error.concat(i18n.t('error.specify_type_contact') + '.\n'); } if (this.state.tmpEmail.length < 5 || !Helpers.isEmailValid(this.state.tmpEmail)) { error = error.concat(i18n.t('error.invalid_email_address') + '.\n'); } if (this.state.tmpName.length === 0 || !Helpers.isValidRequiredString(this.state.tmpName)) { error = error.concat(i18n.t('error.invalid_name') + '.\n'); } if (this.state.tmpFirstname.length === 0 || !Helpers.isValidRequiredString(this.state.tmpFirstname)) { error = error.concat(i18n.t('error.invalid_firstname') + '.\n'); } if (error.length > 0) { Alert.alert(i18n.t('global.warning') + ' !', error, [ { text: 'OK' }, ], { cancelable: false }, ); } else { const products = [ this.state.p1, this.state.p2, this.state.p3, this.state.p4, this.state.p5, this.state.p6, this.state.p7, this.state.p8, this.state.p9 ]; const contact = { id: Helpers.generateUUID(), email: this.state.tmpEmail, type: type, other: this.state.tmpOther, select1: this.state.tmpSelect1, select2: this.state.tmpSelect2, name: this.state.tmpName, firstname: this.state.tmpFirstname, company: this.state.tmpCompany, address: this.state.tmpAddress, phone: this.state.tmpPhone, products: JSON.stringify(products), notes: this.state.tmpNote, visits: JSON.stringify(this.state.visits), to_delete: false, synced: false, }; if (action === 'edit') { contact.id = this.state.tmpID; this.props.dispatch({ type: 'EDIT_CONTACT', data: contact }); this.populate(); showMessage({ message: i18n.t('contact_details_s.contact_saved') + '.', type: 'success', duration: 500, }); } else { contact.user_id = this.props.currentUser.id; this.props.dispatch({ type: 'ADD_CONTACT', data: contact }); this.props.navigation.navigate('ContactScreen', { contactAdded: true }); } } } updateContactType(type) { if (type === 'client') { this.setState({ clientChecked: true, prospectChecked: false, otherChecked: false, tmpOther: '', }); } else if (type === 'prospect') { this.setState({ clientChecked: false, prospectChecked: true, otherChecked: false, tmpOther: '', }); } else { this.setState({ clientChecked: false, prospectChecked: false, otherChecked: true, }); } } newVisit() { this.state.visits.unshift({ title: this.state.newVisitTitle, note: '' }); this.state.visits[this.state.selectedVisit].note = this.state.tmpVisitNote; this.setState({ tmpVisitNote: '', visits: this.state.visits, showNewVisit: false, selectedVisit: 0, }); } deleteVisit() { Alert.alert( i18n.t('global.confirmation'), i18n.t('contact_details_s.confirm_visit_delete'), [ { text: i18n.t('global.no'), onPress: () => console.log('Cancel Pressed'), style: 'cancel', }, { text: i18n.t('global.yes'), onPress: () => { this.state.visits.splice(this.state.selectedVisit, 1); if (this.state.visits.length === 0) { this.setState({ visits: this.state.visits, selectedVisit: 0, tmpVisitNote: '', }); } else { this.setState({ visits: this.state.visits, selectedVisit: 0, tmpVisitNote: this.state.visits[0].note, }); } }, }, ], { cancelable: false }, ); } populate() { const { contactId } = this.props.route.params; const contact = this.props.contacts.filter(tmp => tmp.id === contactId)[0]; this.setState({ tmpID: contact.id, tmpNote: contact.notes, tmpName: contact.name, tmpFirstname: contact.firstname, tmpEmail: contact.email, tmpCompany: contact.company, tmpAddress: contact.address, tmpPhone: contact.phone, tmpSelect1: contact.select1, tmpSelect2: contact.select2, tmpOther: contact.other, tmpProducts: contact.product, }); if (contact.type === 'client') { this.setState({ clientChecked: true }); } else if (contact.type === 'prospect') { this.setState({ prospectChecked: true }); } else { this.setState({ otherChecked: true }); } if (contact.visits !== null) { const tmp = JSON.parse(contact.visits); this.setState({ visits: tmp }); if (tmp.length > 0) { this.setState({ tmpVisitNote: tmp[this.state.selectedVisit].note }); } } if (contact.products !== null) { const tmp = JSON.parse(contact.products); this.setState({ p1: tmp[0], p2: tmp[1], p3: tmp[2], p4: tmp[3], p5: tmp[4], p6: tmp[5], p7: tmp[6], p8: tmp[7], p9: tmp[8], p10: tmp[9], }); } } componentDidMount() { const { action } = this.props.route.params; if (action === 'edit') { this.setState({ action: 'edit' }); this.populate(); } BarCodeScanner.requestPermissionsAsync().then(res => { this.setState({ hasPermission: res.status === 'granted' }); }); } render() { const handleBarCodeScanned = ({ type, data }) => { this.setState({ scanned: true }); if (data.includes('VCARD')) { let datas = vCard.parse(data); let tmpDatas = []; if (datas['n'] !== undefined) { const nx = Object.assign({ n1: [{ value: datas['n'][0].value[0] }] }, { n2: [{ value: datas['n'][0].value[1] }] }); datas = Object.assign(datas, nx); } if (datas['adr'] !== undefined) { let tmp = datas['adr'][0].value; let tmpadr = ''; for (let i = 0; i < tmp.length; i++) { if (tmp[i].length > 0) { if (tmpadr.length > 0) { tmpadr = tmpadr + ', ' + tmp[i]; } else { tmpadr = tmp[i]; } } } datas['adr'] = [{ value: tmpadr }]; } Object.keys(datas).map((objectKey, index) => { if (objectKey === 'email' || objectKey === 'adr' || objectKey === 'tel' || objectKey === 'org' || objectKey === 'n1' || objectKey === 'n2') { let value = datas[objectKey][0].value; if (typeof value === 'string') { tmpDatas.push(value); } } }); this.setState({ parsedType: 'vcard', parsedDatas: tmpDatas, showQRcodevCard: false, showDynContact: true }) } else { Alert.alert(i18n.t('contact_details_s.qrcode_not_vcard_format')); } }; return ( this.updateContactType('client')} /> this.updateContactType('prospect')} /> this.updateContactType('other')} /> {this.state.otherChecked === true && ( this.setState({ tmpOther: text })} /> )} this.setState({ tmpName: text })} /> this.setState({ tmpFirstname: text })} /> this.setState({ tmpEmail: text })} autoCapitalize={'none'} autoCorrect={false} keyboardType={'email-address'} /> this.setState({ tmpCompany: text })} /> this.setState({ tmpAddress: text })} /> this.setState({ tmpPhone: text })} autoCompleteType={'tel'} dataDetectorTypes={'phoneNumber'} keyboardType={'phone-pad'} /> { if (itemValue !== '0') { this.setState({ tmpSelect1: itemValue }); } }}> { if (itemValue !== '0') { this.setState({ tmpSelect2: itemValue }); } }}> this.setState({ p1: !this.state.p1 })} containerStyle={styles.clearCheckbox} /> this.setState({ p2: !this.state.p2 })} containerStyle={styles.clearCheckbox} /> this.setState({ p3: !this.state.p3 })} containerStyle={styles.clearCheckbox} /> this.setState({ p4: !this.state.p4 })} containerStyle={styles.clearCheckbox} /> this.setState({ selectedIndex: index })} /> {this.state.selectedIndex === 0 && ( {this.state.visits.length > 0 && ( { this.state.visits[ this.state.selectedVisit ].note = this.state.tmpVisitNote; this.setState({ visits: this.state.visits, selectedVisit: itemIndex, tmpVisitNote: this.state.visits[itemIndex].note, }); }}> {this.state.visits.map((item, index) => { return ; })} )} {this.state.visits.length === 0 && ( {i18n.t('contact_details_s.no_visits')}. )} this.setState({ showNewVisit: true })}> {this.state.visits.length > 0 && ( this.deleteVisit()}> )} {this.state.visits.length > 0 && ( this.setState({ tmpVisitNote: text })} /> )} )} {this.state.selectedIndex === 1 && ( this.setState({ tmpNote: text })} /> )} {this.state.action === 'add' && ( <>