much cleaner

This commit is contained in:
Jeremy Kauffman 2018-09-21 16:28:15 -04:00
parent ab2091096e
commit 755a25a226
2 changed files with 95 additions and 121 deletions

View file

@ -6,76 +6,74 @@
<noscript>
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">
<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>
<?php if ($error): ?>
<div class="notice notice-error spacer1"><?php echo $error ?></div>
<?php else: ?>
<form id="<?php echo $formId ?>" novalidate style="display: none">
<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 spacer-half">Uncheck all boxes if you want to receive no future messages.</div>
<?php endif ?>
<div class="notice notice-error hide spacer-half"></div>
<div class="notice notice-success hide spacer-half">Your email preferences have been updated.</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 Mail</h4>
<div class="notice notice-error hide spacer-half"></div>
<div class="notice notice-success hide spacer-half">Your email preferences have been updated.</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 class="row-fluid spacer1">
<?php $tagIndex = 0 ?>
<?php foreach ($tags as $tag => $enabled): ?>
<?php $tagId = 'tag_' . (++$tagIndex) ?>
<div class="span6">
<div class="row-fluid">
<div class="span10">
<label for="<?php echo $tagId ?>"><?php echo isset($tagMetadata[$tag]['label']) ? $tagMetadata[$tag]['label'] : $tag ?></label>
</div>
</td>
<td>
<div class="spacer-half" style="padding-left: 5px">
<div class="span2">
<span class="slider-checkbox">
<input id="<?php echo $emailId ?>" type="checkbox" <?php echo $enabled ? 'checked' : '' ?> value="<?php echo urlencode($email) ?>" />
<input id="<?php echo $tagId ?>" type="checkbox" <?php echo $enabled ? 'checked' : '' ?> value="<?php echo urlencode($tag) ?>" />
<label class="label"></label>
</span>
</div>
</td>
</tr>
<?php endforeach ?>
</table>
</section>
<section class="tag-section spacer1">
<h4>Fine-tune your Mail</h4>
<div class="notice notice-error hide"></div>
<div class="row-fluid spacer1">
<?php $tagIndex = 0 ?>
<?php foreach($tags as $tag => $enabled): ?>
<?php $tagId = 'tag_' . (++$tagIndex) ?>
<div class="span6">
<div class="row-fluid">
<div class="span10">
<label for="<?php echo $tagId ?>"><?php echo isset($tagMetadata[$tag]['label']) ? $tagMetadata[$tag]['label'] : $tag ?></label>
</div>
<div class="span2">
<span class="slider-checkbox">
<input id="<?php echo $tagId ?>" type="checkbox" <?php echo $enabled ? 'checked' : '' ?> value="<?php echo urlencode($tag) ?>" />
<label class="label"></label>
</span>
<?php if (isset($tagMetadata[$tag]['description'])): ?>
<div class="meta">
<?php echo $tagMetadata[$tag]['description'] ?>
</div>
<?php endif ?>
</div>
<?php if (isset($tagMetadata[$tag]['description'])): ?>
<div class="meta">
<?php echo $tagMetadata[$tag]['description'] ?>
</div>
<?php if ($tagIndex % 2 == 0): ?>
</div><div class="row-fluid spacer1">
<?php endif ?>
<?php endforeach ?>
</div>
<?php if ($tagIndex % 2 == 0): ?>
</div><div class="row-fluid spacer1">
<?php endif ?>
<?php endforeach ?>
</div>
</section>
</form>
</section>
</form>
<?php endif ?>

View file

@ -3,48 +3,36 @@ lbry.emailSettingsForm = function (formSelector, tags, userAuthToken) {
form = $(formSelector),
emailSection = form.find('.email-section'),
tagSection = form.find('.tag-section'),
hasError = false,
isEmailSubmitPending = false,
tagMap = new Map(),
isTagSubmitPending = false;
tagMap = new Map();
$.each(tags, function(tag, enabled){
tagMap[tag] = enabled;
});
form.find(':input').change(submitForm);
emailSection.find(':input').change(submitEmail);
tagSection.find(':input').change(submitTags);
function submitForm() {
form.find('.notice').hide();
hasError = false;
isEmailSubmitPending = true;
isTagSubmitPending = true;
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()
})
function submitEmail(e) {
var element = e.target,
url = 'https://api.lbry.io/user/email/edit?auth_token=' + userAuthToken
+ "&email=" + element.value + "&enabled=" + element.checked.toString();
emailSection.find('.notice').hide();
fetch(url).then(function (value) {
var apiValues = value.json();
emailSection.find('.notice-success').show()
})
.catch(function (value) {
var error = "Something went wrong saving your email";
emailSection.find('.notice-error').html(error).show();
});
//Call api for each email a user will have linked - polyfill needed for IE for Promise.all
Promise.all(promiseMap)
.then(function (apiValues) {
isEmailSubmitPending = false;
showSuccess();
})
.catch(function (value) {
isEmailSubmitPending = false;
hasError = true;
var error = "get actual error message from value";
emailSection.find('.notice-error').html(error).show();
});
}
//do tag edit
var url = 'https://api.lbry.io/user/tag/edit?auth_token=' + userAuthToken
var addTags = new Array(),
removeTags = new Array();
function submitTags() {
tagSection.find('.notice').hide();
var url = 'https://api.lbry.io/user/tag/edit?auth_token=' + userAuthToken,
addTags = [],
removeTags = [];
tagSection.find('input').each(function () {
var tagName = this.value
@ -68,39 +56,27 @@ lbry.emailSettingsForm = function (formSelector, tags, userAuthToken) {
removeTagsParam = removeTagsParam + "," + removeTags[i];
}
if (addTagsParam && addTagsParam.length > 0) {
url = url + "&add=" + addTagsParam
url += "&add=" + addTagsParam
}
if (removeTagsParam && removeTagsParam.length > 0) {
url = url + "&remove=" + removeTagsParam
url += "&remove=" + removeTagsParam
}
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) {
isTagSubmitPending = false;
hasError = true;
tagSection.find('.notice-error').html(value.error).show();
});
} else {
isTagSubmitPending = false;
}
}
function showSuccess() {
if (!isEmailSubmitPending && !isTagSubmitPending && !hasError)
{
form.find('.notice-success').show()
// .get(0).scrollIntoView();
fetch(url)
.then(function (response) {
return response.json()
})
.then(function (jsonResponse) {
if (jsonResponse.success) {
tagSection.find('.notice-success').show()
} else {
tagSection.find('.notice-error').html(jsonResponse.error).show();
}
})
.catch(function (value) {
tagSection.find('.notice-error').html(value.error).show();
});
}
}