Skip to content

Instantly share code, notes, and snippets.

@SCdF
Created April 29, 2016 18:55
Show Gist options
  • Save SCdF/b4ef336bc1535a756e0195346d8938cd to your computer and use it in GitHub Desktop.
Save SCdF/b4ef336bc1535a756e0195346d8938cd to your computer and use it in GitHub Desktop.
send-message refactoring
diff --git a/static/js/modules/send-message.js b/static/js/modules/send-message.js
index 7ffd075..3ce2443 100644
--- a/static/js/modules/send-message.js
+++ b/static/js/modules/send-message.js
@@ -21,7 +21,7 @@ var _ = require('underscore'),
};
var validatePhoneNumber = function(data) {
- if (data.everyoneAt) {
+ if (data.doc.everyoneAt) {
return true;
}
if (data.doc) {
@@ -88,7 +88,7 @@ var _ = require('underscore'),
if (row.text) {
return row.text;
}
- if (row.everyoneAt) {
+ if (row.doc.everyoneAt) {
icon = 'fa-hospital-o';
contact = format.sender({
name: formatEveryoneAt(row),
@@ -105,7 +105,7 @@ var _ = require('underscore'),
};
var formatSelection = function(row) {
- if (row.everyoneAt) {
+ if (row.doc && row.doc.everyoneAt) {
return formatEveryoneAt(row);
}
if (row.doc) {
@@ -122,39 +122,18 @@ var _ = require('underscore'),
};
};
- var filter = function(options, contacts) {
- var terms = options.term ?
- _.map(options.term.toLowerCase().split(/\s+/), function(term) {
- if (libphonenumber.validate(settings, term)) {
- return libphonenumber.format(settings, term);
- }
- return term;
- }) : [];
- var matches = _.filter(contacts, function(val) {
- var tags = [ val.name, val.phone ];
- var parent = val.parent;
- while (parent) {
- tags.push(parent.name);
- parent = parent.parent;
- }
- tags = tags.join(' ').toLowerCase();
- return _.every(terms, function(term) {
- return tags.indexOf(term) > -1;
- });
- });
- matches.sort(function(a, b) {
+ var preSelect2Filtering = function(contacts) {
+ //! Don't care, our search will do sorting
+ contacts.sort(function(a, b) {
return a.name.toLowerCase().localeCompare(
b.name.toLowerCase());
});
- var data = _.map(matches, function(doc) {
- return { id: doc._id, doc: doc, everyoneAt: doc.everyoneAt };
- });
- // if a valid phone number is entered, allow it to be selected as a recipient
- if (libphonenumber.validate(settings, options.term)) {
- var formatted = libphonenumber.format(settings, options.term);
- data.unshift(createChoiceFromNumber(formatted));
- }
+ //! custom post-search filter?
+ //! important point is pulling out everyoneAt, maybe just move where this code references it?
+ var data = _.map(contacts, function(doc) {
+ return { id: doc._id, doc: doc};//, everyoneAt: doc.everyoneAt };
+ });
return data;
};
@@ -164,11 +143,11 @@ var _ = require('underscore'),
return;
}
$phone.parent().show();
- contact(function(err, data) {
+ ContactService(function(err, data) {
if (err) {
return callback(err);
}
- data = filter({}, data);
+ data = preSelect2Filtering(data);
$phone.select2({
data: data,
allowClear: true,
@@ -207,12 +186,12 @@ var _ = require('underscore'),
});
};
- var contact;
+ var ContactService;
var recipients = [];
var settings = {};
exports.init = function(Settings, Contact, _translateFn) {
- contact = Contact;
+ ContactService = Contact;
translateFn = _translateFn;
Settings()
.then(function(_settings) {
@@ -248,7 +227,7 @@ var _ = require('underscore'),
};
var resolveRecipients = function(recipients, callback) {
- contact(function(err, contacts) {
+ ContactService(function(err, contacts) {
if (err) {
return callback(err);
}
@@ -260,7 +239,7 @@ var _ = require('underscore'),
(recipient.contact.phone);
var match = _.find(contacts, function(contact) {
return contact.phone === phone &&
- contact.everyoneAt === recipient.everyoneAt;
+ contact.everyoneAt === (recipient.doc && recipient.doc.everyoneAt);
});
return match || recipient;
}));
@@ -269,6 +248,7 @@ var _ = require('underscore'),
var validateRecipients = function($modal, callback) {
var validated = recipients;
+ // is this ever false?
if ($modal.is('.modal')) {
var $phoneField = $modal.find('[name=phone]');
var result = updateValidation(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment