instant save of email settings, slight refactor

This commit is contained in:
Jeremy Kauffman 2018-09-21 16:02:45 -04:00
parent 5b8ca112d0
commit ab2091096e
5 changed files with 74 additions and 69 deletions

View file

@ -6,7 +6,7 @@ class LBRY
public static function getApiUrl($endpoint) public static function getApiUrl($endpoint)
{ {
if (!strlen(Config::get(Config::LBRY_API_SERVER)) > 0) { if (!strlen(Config::get(Config::LBRY_API_SERVER)) > 0) {
throw new Exception("API server URL is missing from `/data/config.php`"); throw new Exception("API server URL is missing from configuration");
} }
return Config::get(Config::LBRY_API_SERVER) . $endpoint; return Config::get(Config::LBRY_API_SERVER) . $endpoint;

View file

@ -7,13 +7,45 @@
Javascript is required to securely send your unsubscribe information. Email <a href="mailto:help@lbry.io" class="link-primary">help@lbry.io</a> for manual unsubscription. Javascript is required to securely send your unsubscribe information. Email <a href="mailto:help@lbry.io" class="link-primary">help@lbry.io</a> for manual unsubscription.
</noscript> </noscript>
<form id="<?php echo $formId ?>" novalidate style="display: none"> <form id="<?php echo $formId ?>" novalidate style="display: none">
<?php if ($error): ?> <div style="min-height: 48px">
<div class="notice notice-error spacer1"><?php echo $error ?></div> <?php if ($error): ?>
<?php endif ?> <div class="notice notice-error spacer1"><?php echo $error ?></div>
<div class="notice notice-success hide">Your email preferences have been updated.</div> <?php endif ?>
<div class="notice notice-success hide">Your email preferences have been updated.</div>
</div>
<section class="email-section">
<h4><?php echo count($emails ) > 1 ? 'Receiving Addresses' : 'Do You Want To Receive Mail?' ?></h4>
<?php if (count($emails) > 1): ?>
<div class="meta spacer1">Uncheck all boxes if you want to receive no future messages.</div>
<?php endif ?>
<div class="notice notice-error hide spacer1"></div>
<?php $emailIndex = 0 ?>
<table>
<?php foreach($emails as $email => $enabled): ?>
<?php $emailId = 'email_' . (++$emailIndex) ?>
<tr>
<td>
<div class="spacer-half">
<label for="<?php echo $emailId ?>"><?php echo $email ?></label>
</div>
</td>
<td>
<div class="spacer-half" style="padding-left: 5px">
<span class="slider-checkbox">
<input id="<?php echo $emailId ?>" type="checkbox" <?php echo $enabled ? 'checked' : '' ?> value="<?php echo urlencode($email) ?>" />
<label class="label"></label>
</span>
</div>
</td>
</tr>
<?php endforeach ?>
</table>
</section>
<section class="tag-section spacer1"> <section class="tag-section spacer1">
<h4>Fine-tune your newsletter</h4> <h4>Fine-tune your Mail</h4>
<div class="notice notice-error hide"></div> <div class="notice notice-error hide"></div>
<div class="row-fluid spacer1"> <div class="row-fluid spacer1">
@ -46,36 +78,4 @@
<?php endforeach ?> <?php endforeach ?>
</div> </div>
</section> </section>
<section class="email-section">
<h4>Quick Unsubscribe</h4>
<div class="notice notice-error hide spacer1"></div>
<?php $emailIndex = 0 ?>
<table>
<?php foreach($emails as $email => $enabled): ?>
<?php $emailId = 'email_' . (++$emailIndex) ?>
<tr>
<td>
<div class="spacer-half">
<label for="<?php echo $emailId ?>"><?php echo $email ?></label>
</div>
</td>
<td>
<div class="spacer-half" style="padding-left: 5px">
<span class="slider-checkbox">
<input id="<?php echo $emailId ?>" type="checkbox" <?php echo $enabled ? 'checked' : '' ?> value="<?php echo urlencode($email) ?>" />
<label class="label"></label>
</span>
</div>
</td>
</tr>
<?php endforeach ?>
</table>
<div class="meta">If you toggle "off" and hit "Save", you will be removed from all of our mailing lists.</div>
</section>
<div>
<input type="submit" value="Save" class="btn-primary">
</div>
</form> </form>

View file

@ -12,27 +12,29 @@ lbry.emailSettingsForm = function (formSelector, tags, userAuthToken) {
tagMap[tag] = enabled; tagMap[tag] = enabled;
}); });
form.submit(function(e) { form.find(':input').change(submitForm);
e.preventDefault(); function submitForm() {
form.find('.notice').hide(); form.find('.notice').hide();
hasError = false; hasError = false;
isEmailSubmitPending = true; isEmailSubmitPending = true;
isTagSubmitPending = true; isTagSubmitPending = true;
var promiseMap = $.map(emailSection.find("input"), function(element) { var promiseMap = $.map(emailSection.find("input"), function (element) {
var url = 'https://api.lbry.io/user/email/edit?auth_token=' + userAuthToken var url = 'https://api.lbry.io/user/email/edit?auth_token=' + userAuthToken
url = url + "&email="+element.value+"&enabled="+element.checked.toString(); url = url + "&email=" + element.value + "&enabled=" + element.checked.toString();
return fetch(url).then(function(value) { return value.json()}) return fetch(url).then(function (value) {
return value.json()
})
}); });
//Call api for each email a user will have linked - polyfill needed for IE for Promise.all //Call api for each email a user will have linked - polyfill needed for IE for Promise.all
Promise.all(promiseMap) Promise.all(promiseMap)
.then(function(apiValues) { .then(function (apiValues) {
isEmailSubmitPending = false; isEmailSubmitPending = false;
showSuccess(); showSuccess();
}) })
.catch(function(value) { .catch(function (value) {
isEmailSubmitPending = false; isEmailSubmitPending = false;
hasError = true; hasError = true;
var error = "get actual error message from value"; var error = "get actual error message from value";
@ -41,15 +43,15 @@ lbry.emailSettingsForm = function (formSelector, tags, userAuthToken) {
//do tag edit //do tag edit
var url = 'https://api.lbry.io/user/tag/edit?auth_token=' + userAuthToken var url = 'https://api.lbry.io/user/tag/edit?auth_token=' + userAuthToken
var addTags = new Array(), var addTags = new Array(),
removeTags = new Array(); removeTags = new Array();
tagSection.find('input').each(function () { tagSection.find('input').each(function () {
var tagName = this.value var tagName = this.value
var enabled = this.checked var enabled = this.checked
if (enabled && !tagMap[tagName] ){ if (enabled && !tagMap[tagName]) {
addTags.push(tagName) addTags.push(tagName)
} else if (!enabled && tagMap[tagName]){ } else if (!enabled && tagMap[tagName]) {
removeTags.push(tagName) removeTags.push(tagName)
} }
}); });
@ -58,46 +60,50 @@ lbry.emailSettingsForm = function (formSelector, tags, userAuthToken) {
var addTagsParam = addTags[0] var addTagsParam = addTags[0]
for (var i = 1; i < addTags.length; i++) { for (var i = 1; i < addTags.length; i++) {
hasChanges = true hasChanges = true
addTagsParam = addTagsParam+","+addTags[i]; addTagsParam = addTagsParam + "," + addTags[i];
} }
var removeTagsParam = removeTags[0] var removeTagsParam = removeTags[0]
for (var i = 1; i < removeTags.length; i++) { for (var i = 1; i < removeTags.length; i++) {
hasChanges = true hasChanges = true
removeTagsParam = removeTagsParam +","+removeTags[i]; removeTagsParam = removeTagsParam + "," + removeTags[i];
} }
if (addTagsParam && addTagsParam.length > 0){ if (addTagsParam && addTagsParam.length > 0) {
url = url + "&add="+addTagsParam url = url + "&add=" + addTagsParam
} }
if ( removeTagsParam && removeTagsParam.length > 0){ if (removeTagsParam && removeTagsParam.length > 0) {
url = url + "&remove="+removeTagsParam url = url + "&remove=" + removeTagsParam
} }
if (hasChanges){ if (hasChanges) {
fetch(url).then(response => { return response.json() }).then(jsonResponse =>{ fetch(url).then(response => {return response.json()}
).
then(jsonResponse => {
isTagSubmitPending = false; isTagSubmitPending = false;
if (jsonResponse.success){ if (jsonResponse.success) {
showSuccess(); showSuccess();
}else { } else {
hasError = true; hasError = true;
tagSection.find('.notice-error').html(jsonResponse.error).show(); tagSection.find('.notice-error').html(jsonResponse.error).show();
} }
}).catch(function(value) { }).
catch(function (value) {
isTagSubmitPending = false; isTagSubmitPending = false;
hasError = true; hasError = true;
tagSection.find('.notice-error').html(value.error).show(); tagSection.find('.notice-error').html(value.error).show();
}); });
} else{ } else {
isTagSubmitPending = false; isTagSubmitPending = false;
} }
}); }
form.show();
function showSuccess() { function showSuccess() {
if (!isEmailSubmitPending && !isTagSubmitPending && !hasError) if (!isEmailSubmitPending && !isTagSubmitPending && !hasError)
{ {
form.find('.notice-success').show().get(0).scrollIntoView(); form.find('.notice-success').show()
// .get(0).scrollIntoView();
} }
} }
form.show();
} }

View file

@ -18,5 +18,4 @@
@import "quickstart"; @import "quickstart";
@import "social"; @import "social";
@import "home"; @import "home";
@import "mail_settings";
@import "slider"; @import "slider";