rramona2
6/14/2018 - 9:12 PM

sheets cms demo app

sheets cms demo app

html, body {
	background: #f8f8f8;
	color: #2d3436;
	font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
	font-size: normal;
	line-height: 1.5;
}

a {
	color: #1a1a1a;
	border-bottom: solid 1px #b2bec3;
	text-decoration: none;
}

a:hover {
	border-color: #1a1a1a;
}

img {
	max-width: 100%;
}

button {
	border-radius: 5px;
	border: solid 1px #b2bec3;
	cursor: pointer;
	display: inline-block;
	padding: 0.2em 0.5em;
}

button:hover {
	background: #dfe6e9; 	
	border-color: #b2bec3;
}

button.selected {
	border-color: #2d3436;
}

header, main, footer {
	padding: 4em 5%;
}

header {
	background: #2d3436;
	color: white;
	text-align: center;
}

header h1 {
	margin-bottom: 0;
}

.information {
	background: #dfe6e9;
	margin-bottom: 4em;
	padding: 2em;
}

nav {
	margin-bottom: 4em;
}

nav span {
	display: block;
	font-size: 0.9em;
	margin-bottom: 1em;
}

nav button {
	margin: 0 0.5em 1em 0;
}

article {
	margin-bottom: 6em;
}

.article-details {	
	border-bottom: solid 2px #dfe6e9;
	font-size: 0.9em;
	padding-bottom: 1em;
}

.article-details div:first-child {
	margin-bottom: 0.5em;
}

#notice {
	font-weight: bold;
	text-align: center;
}

footer {
	background: #dfe6e9;
	text-align: center;
}

footer p {
	margin-top: 0;
}

@media only screen and (min-width : 992px) {
	.article-details {
		align-items: center;
		display: flex;
		justify-content: space-between;
	}

	.article-details div:first-child {
		margin-bottom: 0;
	}

	header, main, footer {
		padding: 4em 25%;
	}
}

@media only screen and (min-width : 1200px) {
	header, main, footer {
		padding: 4em 30%;
	}
}

/*! normalize.css v7.0.0 | MIT License | github.com/necolas/normalize.css */html{line-height:1.15;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}article,aside,footer,header,nav,section{display:block}h1{font-size:2em;margin:.67em 0}figcaption,figure,main{display:block}figure{margin:1em 40px}hr{box-sizing:content-box;height:0;overflow:visible}pre{font-family:monospace,monospace;font-size:1em}a{background-color:transparent;-webkit-text-decoration-skip:objects}abbr[title]{border-bottom:none;text-decoration:underline;text-decoration:underline dotted}b,strong{font-weight:inherit}b,strong{font-weight:bolder}code,kbd,samp{font-family:monospace,monospace;font-size:1em}dfn{font-style:italic}mark{background-color:#ff0;color:#000}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}audio,video{display:inline-block}audio:not([controls]){display:none;height:0}img{border-style:none}svg:not(:root){overflow:hidden}button,input,optgroup,select,textarea{font-family:sans-serif;font-size:100%;line-height:1.15;margin:0}button,input{overflow:visible}button,select{text-transform:none}[type=reset],[type=submit],button,html [type=button]{-webkit-appearance:button}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner{border-style:none;padding:0}[type=button]:-moz-focusring,[type=reset]:-moz-focusring,[type=submit]:-moz-focusring,button:-moz-focusring{outline:1px dotted ButtonText}fieldset{padding:.35em .75em .625em}legend{box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal}progress{display:inline-block;vertical-align:baseline}textarea{overflow:auto}[type=checkbox],[type=radio]{box-sizing:border-box;padding:0}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}[type=search]::-webkit-search-cancel-button,[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}details,menu{display:block}summary{display:list-item}canvas{display:inline-block}template{display:none}[hidden]{display:none}/*# sourceMappingURL=normalize.min.css.map */

sheets cms demo app

demo 1

A Pen by ryan ramona on CodePen.

License.

const app = (function() {
  const API_BASE =
    "https://script.google.com/macros/s/AKfycbyZZVWwGFg4_gf0dS_Vo60GjjEkLqP-vcGqrj2W-A/exec";
  const API_KEY = "abcdef";
  const CATEGORIES = ["general", "financial", "technology", "marketing"];

  const state = { activePage: 1, activeCategory: null };
  const page = {};

  function init() {
    page.notice = document.getElementById("notice");
    page.filter = document.getElementById("filter");
    page.container = document.getElementById("container");

    _buildFilter();
    _getNewPosts();
  }

  function _getNewPosts() {
    page.container.innerHTML = "";
    _getPosts();
  }

  function _getPosts() {
    _setNotice("Loading posts");

    fetch(_buildApiUrl(state.activePage, state.activeCategory))
      .then(response => response.json())
      .then(json => {
        if (json.status !== "success") {
          _setNotice(json.message);
        }

        _renderPosts(json.data);
        _renderPostsPagination(json.pages);
      })
      .catch(error => {
        _setNotice("Unexpected error loading posts");
      });
  }

  function _buildFilter() {
    page.filter.appendChild(_buildFilterLink("no filter", true));

    CATEGORIES.forEach(function(category) {
      page.filter.appendChild(_buildFilterLink(category, false));
    });
  }

  function _buildFilterLink(label, isSelected) {
    const link = document.createElement("button");
    link.innerHTML = _capitalize(label);
    link.classList = isSelected ? "selected" : "";
    link.onclick = function(event) {
      let category = label === "no filter" ? null : label.toLowerCase();

      _resetActivePage();
      _setActiveCategory(category);
      _getNewPosts();
    };

    return link;
  }

  function _buildApiUrl(page, category) {
    let url = API_BASE;
    url += "?key=" + API_KEY;
    url += "&page=" + page;
    url += category !== null ? "&category=" + category : "";

    return url;
  }

  function _setNotice(label) {
    page.notice.innerHTML = label;
  }

  function _renderPosts(posts) {
    posts.forEach(function(post) {
      const article = document.createElement("article");
      article.innerHTML = `
				<h2>${post.title}</h2>
				<div class="article-details">
					<div>By ${post.author} on ${_formatDate(post.timestamp)}</div>
					<div>Posted in ${post.category}</div>
				</div>
				${_formatContent(post.content)}
			`;
      page.container.appendChild(article);
    });
  }

  function _renderPostsPagination(pages) {
    if (pages.next) {
      const link = document.createElement("button");
      link.innerHTML = "Load more posts";
      link.onclick = function(event) {
        _incrementActivePage();
        _getPosts();
      };

      page.notice.innerHTML = "";
      page.notice.appendChild(link);
    } else {
      _setNotice("No more posts to display");
    }
  }

  function _formatDate(string) {
    return new Date(string).toLocaleDateString("en-GB");
  }

  function _formatContent(string) {
    return string
      .split("\n")
      .filter(str => str !== "")
      .map(str => `<p>${str}</p>`)
      .join("");
  }

  function _capitalize(label) {
    return label.slice(0, 1).toUpperCase() + label.slice(1).toLowerCase();
  }

  function _resetActivePage() {
    state.activePage = 1;
  }

  function _incrementActivePage() {
    state.activePage += 1;
  }

  function _setActiveCategory(category) {
    state.activeCategory = category;

    const label = category === null ? "no filter" : category;
    Array.from(page.filter.children).forEach(function(element) {
      element.classList =
        label === element.innerHTML.toLowerCase() ? "selected" : "";
    });
  }

  return {
    init: init
  };
})();
<!-- <!DOCTYPE html> -->
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>Google Sheets CMS</title>
  
   <link rel="stylesheet" href="https://codepen.io/rramona2/pen/BVdMLe.css">
<!--   <link rel="stylesheet" href="css/main.css"> -->
</head>

<body>
  <header>
    <img src="https://github.com/danielireson/google-sheets-blog-cms/blob/master/src/img/google-sheets-logo.png?raw=true" alt="logo for google sheets">
    <h1>Generic Blog</h1>
  </header>
  <main>
    <div class="information">
      This is an example of a frontend for a blog CMS built using Google Sheets, Google Forms and Google Apps Script which was published as a <a href="https://medium.com/p/c2eab3fb0b2b">Medium article</a>. The blog is designed as a single page application
      with pagination and category filtering.
    </div>
    <nav id="filter"></nav>
    <div id="container"></div>
    <div id="notice"></div>
  </main>
  <footer>
    <p>These blog posts are from a Google Sheets backend</p>
    <a href="https://medium.com/p/c2eab3fb0b2b">Read the article on Medium</a>
  </footer>
  <script src="https://codepen.io/rramona2/pen/BVdMLe.js"></script>
  <script>
    document.addEventListener('DOMContentLoaded', app.init);
  </script>
</body>

</html>