<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Facebook Dashboard</title>
  <link rel="stylesheet" href="../style.css">
  <style>
  .verified { font-size: 50; color: #8c8; }
  .photo { height: 100px; }
  </style>
</head>
<body class="container">

  <nav class="menu">
    <h1><a href=".">Back to Guide</a></h1>
  </nav>

  <section class="content">
    <h1>Facebook Dashboard</h1>

    <div id="error"></div>

    <div class="about"></div>
    <script class="about" type="text/html">
      <h3>
        <%= o.name %>
        <% if (o.verified) { %><small class="verified">(Verified account)</small><% } %>
      </h3>
      <p>You're a <%= o.gender || 'person' %> living in time zone <%= o.timezone || 'unknown' %>.</p>
      <h4>Recent pictures</h4>
      <p>
        <% (o.photos && o.photos.data ? o.photos.data : []).forEach(function(photo) { %>
          <img class="photo" src="<%= photo.picture %>">
        <% }) %>
      </p>
      <h4>Recent posts</h4>
      <ul>
        <% (o.posts && o.posts.data ? o.posts.data : []).forEach(function(post) { %>
          <li><%= post.story %></li>
        <% }) %>
      </ul>
    </script>
  </section>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
  <script>
  $.get('facebook/me?fields=name,verified,gender,timezone,photos.limit(5){name,picture,tags.limit(5)},posts.limit(5)')
    .done(show_about_me)
    .fail(handle_errors)

  function show_about_me(data) {
    var tmpl = _.template($('script.about').html())
    $('div.about').html(tmpl({o: data}))
  }

  function handle_errors(xhr, status, message) {
    if (message.match(/access token/i))
      $('#error').html('Please <a href="facebook/">log into Facebook</a> to continue')
    else
      $('#error').html(message)
  }

  </script>
</body>
</html>
