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)
{
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;

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.
</noscript>
<form id="<?php echo $formId ?>" novalidate style="display: none">
<?php if ($error): ?>
<div class="notice notice-error spacer1"><?php echo $error ?></div>
<?php endif ?>
<div class="notice notice-success hide">Your email preferences have been updated.</div>
<div style="min-height: 48px">
<?php if ($error): ?>
<div class="notice notice-error spacer1"><?php echo $error ?></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">
<h4>Fine-tune your newsletter</h4>
<h4>Fine-tune your Mail</h4>
<div class="notice notice-error hide"></div>
<div class="row-fluid spacer1">
@ -46,36 +78,4 @@
<?php endforeach ?>
</div>
</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>

View file

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