save tmp clean + Contacts OK

This commit is contained in:
Vincent BRIDIER 2022-12-29 10:32:28 +01:00
parent 43d2f21591
commit 4f415e5cd2
No known key found for this signature in database
GPG key ID: 8571BFA20420D512
7 changed files with 37 additions and 28 deletions

View file

@ -1,11 +1,10 @@
class Helpers {
compareValues(key, order = 'asc') {
return function innerSort(a, b) {
if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
if (!(key in a) || !(key in b)) {
// property doesn't exist on either object
return 0;
}
const varA = (typeof a[key] === 'string')
? a[key].toUpperCase() : a[key];
const varB = (typeof b[key] === 'string')

View file

@ -93,6 +93,15 @@ class ContactModel {
});
}
delete(key) {
const tmp = this.get(key);
if (typeof tmp === 'object') {
this.realm.write(() => {
this.realm.delete(tmp);
});
}
}
}
export default ContactModel;

View file

@ -42,11 +42,12 @@ export const editContact = (currentUser, data) => {
/**
* Delete contact
* @param {object} currentUser The currentUser
* @param {string} uid The contact uid
* @param {string} id The contact id
*/
export const deleteContact = (currentUser, uid) => {
export const deleteContact = (currentUser, id) => {
return {
type: DELETE_CONTACT,
uid,
currentUser,
id,
};
};

View file

@ -7,30 +7,20 @@ const initialState = [];
const reducer = (state = initialState, action) => {
switch (action.type) {
case INIT_CONTACT: {
console.log("Init contact", Contact.getByUser(action.currentUser.id), action.currentUser.id)
return Contact.getByUser(action.currentUser.id);
}
case ADD_CONTACT: {
action.data.user_id = action.currentUser.id;
Contact.add(action.data);
return [
...state,
Contact.getByUser(action.currentUser.id),
];
return Contact.getByUser(action.currentUser.id);
}
case EDIT_CONTACT: {
Contact.update(action.data);
return [
...state,
Contact.getByUser(action.currentUser.id),
];
return Contact.getByUser(action.currentUser.id);
}
case DELETE_CONTACT: {
Contact.delete(action.data);
return [
...state,
Contact.getByUser(action.currentUser.id),
];
Contact.delete(action.id);
return Contact.getByUser(action.currentUser.id);
}
default: {

View file

@ -10,6 +10,8 @@ import {
} from 'react-native';
// Reduxs
import { connect } from 'react-redux';
import { editContact } from '../Store/Actions/contacts';
import { deleteContact } from '../Store/Actions/contacts';
// Helpers
import Helpers from "../Lib/Helpers";
// Translations
@ -34,6 +36,14 @@ function mapStateToProps(state) {
};
}
const mapDispatchToProps = dispatch => {
return {
editContact: (user, data) => dispatch(editContact(user, data)),
deleteContact: (user, id) => dispatch(deleteContact(user, id)),
sendBrochure: (email) => dispatch({ type: 'SEND_BROCHURE', data: email })
};
};
class ContactScreen extends React.Component {
state = {
contacts: [],
@ -69,7 +79,7 @@ class ContactScreen extends React.Component {
documents: JSON.stringify(this.state.sendDocumentList),
user_id: this.props.currentUser.id
};
this.props.dispatch({ type: 'SEND_BROCHURE', data: email });
this.props.sendBrochure(email);
this.setState({
showSendBrochure: false,
tmpSendBrochureEmail: '',
@ -115,7 +125,7 @@ class ContactScreen extends React.Component {
synced: false,
user_id: nextUser.id,
};
this.props.dispatch({ type: 'EDIT_CONTACT', data: contact });
this.props.editContact(this.props.currentUser, contact);
this.setState({ showTransferContact: false });
showMessage({
message: i18n.t('contact_s.contact_transferred') + '.',
@ -125,7 +135,7 @@ class ContactScreen extends React.Component {
});
}
deleteContact(id) {
deleteContactAction(id) {
Alert.alert(
i18n.t('global.confirmation'),
i18n.t('contact_s.confirm_contact_delete'),
@ -139,7 +149,7 @@ class ContactScreen extends React.Component {
text: i18n.t('global.yes'),
onPress: () => {
this.setState({ contacts: [], sortedEmails: [] });
this.props.dispatch({ type: 'DELETE_CONTACT', data: id });
this.props.deleteContact(this.props.currentUser, id);
showMessage({
message: i18n.t('contact_s.contact_deleted') + '.',
type: 'success',
@ -172,7 +182,7 @@ class ContactScreen extends React.Component {
<TouchableOpacity
style={styles.button}
onPress={() => {
this.deleteContact(item.id);
this.deleteContactAction(item.id);
}}>
<FontAwesome5 name="trash" size={20} color={'#ea4f52'} />
</TouchableOpacity>
@ -203,6 +213,7 @@ class ContactScreen extends React.Component {
}
}
this.setState({ contacts: this.state.contacts.sort(Helpers.compareValues(field, sort)), sortType: sort, sortField: field });
//console.log(this.state.contacts[0].name)
}
HeaderItem() {
@ -261,7 +272,7 @@ class ContactScreen extends React.Component {
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]; });
console.log("Contacts", objContacts);
this.setState({
contacts: arrayContacts.sort(Helpers.compareValues('name', 'asc')),
sortedDocuments: arrayDocs.sort(Helpers.compareValues('title', 'asc')),
@ -537,4 +548,4 @@ class ContactScreen extends React.Component {
}
}
export default connect(mapStateToProps)(ContactScreen);
export default connect(mapStateToProps, mapDispatchToProps)(ContactScreen);

View file

@ -130,7 +130,6 @@ class LoginScreen extends React.Component {
LoginController.login(user);
this.props.setCurrentUser(user);
this.props.setIsLogged();
this.props.initContact(this.props.app.currentUser);
if (forceSync === true) {
this.props.navigation.navigate('SyncScreen', { forceSync: true });
}

View file

@ -128,7 +128,7 @@ class SyncScreen extends React.Component {
});
// getContacts
this.setState({ progress: 0.1, txtStep: i18n.t('sync_s.updating_contacts') + '...' });
const resContacts = await SyncController.getContacts().then(res => {
const resContacts = await SyncController.getContacts(this.props.currentUser.id).then(res => {
Contact.clear();
return res.data;
}).catch(error => {