import React from 'react';
import {
View,
StyleSheet,
Button,
FlatList,
Text,
Dimensions,
TouchableOpacity,
Alert,
TextInput,
} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome';
import { connect, ReactReduxContext } from 'react-redux';
import { showMessage } from 'react-native-flash-message';
import { Overlay } from '@rneui/themed';
import Helpers from "../Controllers/Helpers";
import i18n from "../I18n";
import { Picker } from '@react-native-picker/picker';
function mapStateToProps(state) {
return {
users: state.users,
contacts: state.contacts,
documents: state.documents,
currentUser: state.currentUser,
};
}
class ContactScreen extends React.Component {
state = {
contacts: [],
sortType: 'asc',
sortField: 'name',
searchTxt: '',
showTransferContact: false,
selectedTransferUser: 0,
contactToTransfer: {},
showSendBrochure: false,
selectedDocument: -1,
sendDocumentList: [],
selectedSendBrochureEmail: -1,
tmpSendBrochureEmail: '',
tmpSendBrochureSubject: '',
tmpSendBrochureMessage: '',
sortedDocuments: [],
sortedEmails: [],
};
sendBrochure() {
if (
this.state.tmpSendBrochureEmail !== '' &&
this.state.tmpSendBrochureSubject !== '' &&
this.state.tmpSendBrochureMessage !== '' &&
this.state.sendDocumentList.length > 0 &&
Helpers.isEmailValid(this.state.tmpSendBrochureEmail)
) {
const email = {
email: this.state.tmpSendBrochureEmail,
subject: this.state.tmpSendBrochureSubject,
message: this.state.tmpSendBrochureMessage,
documents: JSON.stringify(this.state.sendDocumentList),
user_id: this.props.currentUser.id
};
this.props.dispatch({ type: 'SEND_BROCHURE', data: email });
this.setState({
showSendBrochure: false,
tmpSendBrochureEmail: '',
tmpSendBrochureSubject: '',
tmpSendBrochureMessage: '',
selectedSendBrochureEmail: -1,
selectedDocument: -1,
sendDocumentList: [],
});
showMessage({
message: i18n.t('global.message_sent') + '.',
description: i18n.t('global.message_will_be_sent_after_sync') + '.',
type: 'success',
duration: 1500,
});
} else {
Alert.alert(
i18n.t('error.incomplete_form'),
i18n.t('error.fill_all_fields_and_1_doc_min') + '.',
);
}
}
transferContact() {
const nextUser = this.props.users.filter(user => user.id !== this.props.currentUser.id)[this.state.selectedTransferUser];
const transferContact = this.state.contactToTransfer;
const contact = {
id: transferContact.id,
email: transferContact.email,
type: transferContact.type,
other: transferContact.other,
network: transferContact.network,
name: transferContact.name,
firstname: transferContact.firstname,
office: transferContact.office,
company: transferContact.company,
address: transferContact.address,
phone: transferContact.phone,
products: transferContact.products,
notes: transferContact.note,
visits: transferContact.visits,
to_delete: false,
synced: false,
user_id: nextUser.id,
};
this.props.dispatch({ type: 'UPDATE_CONTACT', data: contact });
this.setState({ showTransferContact: false });
showMessage({
message: i18n.t('contact_s.contact_transferred') + '.',
description: i18n.t('contact_s.contact_will_be_sent_after_sync') + '.',
type: 'success',
duration: 1500,
});
}
deleteContact(id) {
Alert.alert(
i18n.t('global.confirmation'),
i18n.t('contact_s.confirm_contact_delete'),
[
{
text: i18n.t('global.no'),
onPress: () => console.log('Cancel Pressed'),
style: 'cancel',
},
{
text: i18n.t('global.yes'),
onPress: () => {
this.setState({ contacts: [], sortedEmails: [] });
this.props.dispatch({ type: 'DELETE_CONTACT', data: id });
showMessage({
message: i18n.t('contact_s.contact_deleted') + '.',
type: 'success',
duration: 500,
});
},
},
],
{ cancelable: false },
);
}
Item(item) {
return (
this.props.navigation.navigate('ContactDetailsScreen', {
action: 'edit',
contactId: item.id,
})
}>
{item.name.toUpperCase()} {item.firstname}
{item.company}
{item.email}
{
this.deleteContact(item.id);
}}>
{
this.setState({
showTransferContact: true,
contactToTransfer: item,
});
}}>
);
}
sortContacts(field) {
let sort;
if (field !== this.state.sortField) {
sort = 'asc';
} else {
if (this.state.sortType === 'asc') {
sort = 'desc';
} else {
sort = 'asc';
}
}
this.setState({ contacts: this.state.contacts.sort(Helpers.compareValues(field, sort)), sortType: sort, sortField: field });
}
HeaderItem() {
return (
{
this.sortContacts('name');
}}>
{i18n.t('global.name')}
{
this.sortContacts('company');
}}>
{i18n.t('global.company')}
{i18n.t('global.email')}
);
}
handleSearchBtn() {
if (this.state.searchTxt.length > 0) {
let obj = this.props.contacts;
let array = Object.keys(obj).map(function (key) { return obj[key]; });
this.setState({ contacts: array.sort(Helpers.compareValues('name', 'asc')), sortType: 'asc', searchTxt: '' });
}
}
handleSearchTxt(text) {
this.setState({ searchTxt: text });
if (text.length > 0) {
let tmpContactsName = this.props.contacts.filter(item => item.name.toLowerCase().indexOf(text.toLowerCase()) >= 0);
let tmpContactsCompany = this.props.contacts.filter(item => item.company.toLowerCase().indexOf(text.toLowerCase()) >= 0);
let resSearch = Object.assign(tmpContactsName, tmpContactsCompany);
this.setState({ contacts: resSearch.sort(Helpers.compareValues('name', 'asc')), sortType: 'asc' });
} else {
let obj = this.props.contacts;
let array = Object.keys(obj).map(function (key) { return obj[key]; });
this.setState({ contacts: array.sort(Helpers.compareValues('name', 'asc')), sortType: 'asc' });
}
}
componentDidMount() {
this.init();
}
componentDidUpdate(prevProps, prevState, snapshot) {
if (prevProps.contacts !== this.props.contacts) {
this.init();
}
}
init() {
let objContacts = this.props.contacts;
let objDocs = this.props.documents;
let arrayContacts = Object.keys(objContacts).map(function (key) { return objContacts[key]; });
let arrayEmails = Object.keys(objContacts).map(function (key) { return objContacts[key]; });
let arrayDocs = Object.keys(objDocs).map(function (key) { return objDocs[key]; });
this.setState({
contacts: arrayContacts.sort(Helpers.compareValues('name', 'asc')),
sortedDocuments: arrayDocs.sort(Helpers.compareValues('title', 'asc')),
sortedEmails: arrayEmails.sort(Helpers.compareValues('email', 'asc')),
sortType: 'asc',
sortField: 'name',
});
}
render() {
const { contactAdded } = this.props.route.params;
if (contactAdded === true) {
this.props.navigation.setParams({ contactAdded: false });
showMessage({
message: i18n.t('contact_s.contact_added') + '.',
type: 'success',
duration: 500,
});
}
return (
this.Item(item)}
ListHeaderComponent={this.HeaderItem.bind(this)}
keyExtractor={item => item.id.toString()}
/>
{i18n.t('contact_s.select_contact')} :
{
this.setState({ selectedTransferUser: itemIndex });
}}>
{this.props.users.filter(user => user.id !== this.props.currentUser.id).map((item, index) => {
return ;
})}
this.setState({ showTransferContact: false })}
/>
this.transferContact()}
/>
this.setState({ tmpSendBrochureEmail: text })
}
/>
{
if (itemValue !== '-1') {
this.setState({
tmpSendBrochureEmail: this.state.sortedEmails[itemIndex - 1]
.email,
});
}
}}>
{this.state.sortedEmails.map((item, index) => {
return ;
})}
this.setState({ tmpSendBrochureSubject: text })
}
/>
this.setState({ tmpSendBrochureMessage: text })
}
/>
{
if (
itemValue !== '-1' &&
this.state.sendDocumentList.length < 8
) {
let tmp = this.state.sendDocumentList;
const toPush = this.state.sortedDocuments[itemIndex - 1];
const test = this.state.sendDocumentList.filter(
item => item.id === toPush.id,
);
if (test.length === 0) {
tmp.push(toPush);
this.setState({
sendDocumentList: tmp,
});
}
}
}}>
{this.state.sortedDocuments.map((item, index) => {
return (
);
})}
{this.state.sendDocumentList.map((item, index) => {
return (
{
let tmp = this.state.sendDocumentList;
tmp.splice(this.props.documents[index], 1);
this.setState({
sendDocumentList: tmp,
});
}}>
{item.title} ({item.type.toUpperCase()}){' '}
);
})}
this.setState({
showSendBrochure: false,
tmpSendBrochureEmail: '',
tmpSendBrochureSubject: '',
tmpSendBrochureMessage: '',
selectedSendBrochureEmail: -1,
selectedDocument: -1,
sendDocumentList: [],
})
}
/>
this.sendBrochure()} color={'#ea4f52'} />
);
}
}
const { width } = Dimensions.get('window');
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
},
item: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
borderBottomWidth: 1,
padding: 15,
marginHorizontal: 15,
},
clickable: {
textAlign: 'center',
fontSize: 18,
width: width / 4,
color: '#ea4f52',
fontWeight: 'bold'
},
company: {
textAlign: 'center',
fontSize: 15,
width: width / 4,
},
email: {
textAlign: 'center',
fontSize: 15,
width: width / 4,
},
buttons: {
justifyContent: 'center',
flexDirection: 'row',
fontSize: 15,
width: width / 4,
},
button: {
width: 35,
height: 35,
justifyContent: 'center',
alignItems: 'center',
margin: 2,
},
txtSendBrochure: {
borderWidth: 1,
borderColor: 'lightgrey',
width: 280,
marginBottom: 5,
paddingLeft: 10,
},
textAreaSendBrochure: {
borderWidth: 1,
borderColor: 'lightgrey',
width: 280,
height: 150,
marginBottom: 5,
paddingLeft: 10,
textAlignVertical: 'top',
},
inputSearch: {
backgroundColor: 'white',
width: 200,
paddingLeft: 20
},
btnSearch: {
width: 50,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#ea4f52'
}
});
export default connect(mapStateToProps)(ContactScreen);