thank-you-tweet-button-004
Version:
Easy-to-use tweet button for saying thank you to @oluwatobiss.
37 lines (36 loc) • 2.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.tweetButton = tweetButton;
function generateStarIcons(rating) {
var megaStars = "\u2B50\u2B50\u2B50\u2B50\u2B50 x ".concat(Math.round(rating / 5));
var stars = "";
for (var i = 0; i < rating; i++) {
stars += "⭐";
}
return rating > 10 ? megaStars : stars;
}
function showError() {
var errorContainer = document.createElement("div");
var errorParagraph = document.createElement("p");
var errorParagraphText = document.createTextNode("ⓘ Error: One or more required parameters are missing in 'tweetButtonParameters'.");
errorContainer.setAttribute("style", "display: flex; justify-content: center; margin-top: 30px");
errorParagraph.setAttribute("style", "background-color: #DC3545; color: #fff; width: 35%; border-radius: 5px; padding: 15px 20px");
errorParagraph.appendChild(errorParagraphText);
errorContainer.appendChild(errorParagraph);
return errorContainer;
}
function tweetButton(rating, bestNPMPackage, quantity) {
var tweetButtonContainer = document.createElement("section");
var anchorElement = document.createElement("a");
var anchorElementContent = document.createTextNode("Post a thank you message");
var tweetUrl = "https://twitter.com/intent/tweet?text=Thank+you,+%40oluwatobiss.+Your+book+helped+me+create,+test,+and+publish+".concat(quantity && quantity > 1 ? quantity : "an", "+NPM+").concat(quantity && quantity > 1 ? "packages" : "package", ".%0A%0AMy+Favorite:+").concat(bestNPMPackage, "%0A%0ABook's+Rating:+").concat(rating, "-star+rating!+").concat(generateStarIcons(rating), "%0A%0Ahttps%3A%2F%2Famzn.to/4lifL3n");
tweetButtonContainer.setAttribute("id", "tweet-btn-container");
anchorElement.setAttribute("class", "tweet-button");
anchorElement.setAttribute("target", "_blank");
anchorElement.setAttribute("href", tweetUrl);
anchorElement.appendChild(anchorElementContent);
tweetButtonContainer.appendChild(anchorElement);
return typeof rating === "number" && bestNPMPackage
? tweetButtonContainer
: showError();
}