alert_words_ui.js: Fix the data flow while adding alert word.

Modify the `add_alert_word()` function to follow the correct data
flow for error handling and propagation.
This commit is contained in:
Harshit Bansal 2017-06-11 18:30:09 +00:00 committed by Tim Abbott
parent d5ab8ac1e1
commit 049bc02778
2 changed files with 18 additions and 18 deletions

View File

@ -44,25 +44,24 @@ function update_alert_words() {
data: {alert_words: JSON.stringify(words)}});
}
function add_alert_word(word, event) {
if ($.trim(word) === '') {
update_alert_word_status(i18n.t("Alert words can't be empty!"), true);
function add_alert_word(alert_word) {
if ($.trim(alert_word) === '') {
update_alert_word_status(i18n.t("Alert word can't be empty!"), true);
return;
}
var final_li = templates.render('alert_word_settings_item', {word: word, editing: false});
var li = $(event.target).parents('li');
li.replaceWith(final_li);
var words_to_be_added = [alert_word];
var new_word = templates.render('alert_word_settings_item', {word: '', editing: true});
var word_list = $('#alert_words_list');
word_list.append(new_word);
if (word_list.find('input').length > 0) {
word_list.find('input').focus();
}
update_alert_words();
channel.put({
url: '/json/users/me/alert_words',
data: {alert_words: JSON.stringify(words_to_be_added)},
success: function () {
update_alert_word_status(i18n.t("Alert word added successfully!"), false);
},
error: function () {
update_alert_word_status(i18n.t("Error adding alert word!"), true);
},
});
}
exports.set_up_alert_words = function () {
@ -70,9 +69,9 @@ exports.set_up_alert_words = function () {
exports.render_alert_words_ui();
$('#alert_words_list').on('click', '#create_alert_word_button', function (event) {
$('#alert_words_list').on('click', '#create_alert_word_button', function () {
var word = $('#create_alert_word_name').val();
add_alert_word(word, event);
add_alert_word(word);
});
$('#alert_words_list').on('click', '.remove-alert-word', function (event) {
@ -89,7 +88,7 @@ exports.set_up_alert_words = function () {
event.preventDefault();
var word = $(event.target).val();
add_alert_word(word, event);
add_alert_word(word);
}
});

View File

@ -6,6 +6,7 @@ exports.dispatch_normal_event = function dispatch_normal_event(event) {
switch (event.type) {
case 'alert_words':
alert_words.words = event.alert_words;
alert_words_ui.render_alert_words_ui();
break;
case 'default_streams':