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