<!DOCTYPE html>
<!--
Copyright (c) Jupyter Development Team.
Distributed under the terms of the Modified BSD License.
-->
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="google" content="notranslate" />
    <meta http-equiv="Content-Language" content="en" />

    <title>Clone Landing page</title>
    <script>
      window.onload = function() {
        var getCookieByName = function(name) {
          // originally from https://github.com/jupyter/notebook/blob/master/notebook/static/base/js/utils.js#L800-L804
          // from tornado docs: http://www.tornadoweb.org/en/stable/guide/security.html
          var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
          return r ? r[1] : undefined;
        };

        var button = document.getElementById("mybutton");
        button.onclick = function(event) {
          var post_json = {
            s3_bucket: "{{ s3_bucket }}",
            s3_key: "{{ s3_key }}"
          };
          var xsrf_token = getCookieByName("_xsrf");
          var xhr = new XMLHttpRequest();
          xhr.responseType = "json";
          xhr.onload = function(e) {
            if (xhr.readyState === 4) {
              if (xhr.status === 201) {
                window.location.href = `{{ redirect_contents_url }}/${
                  xhr.response.path
                }`;
              } else {
                console.error(xhr);
              }
            }
          };
          xhr.onerror = function(e) {
            console.error(xhr.statusText);
          };

          xhr.open("POST", "{{ clone_api_url }}", true);
          xhr.withCredentials = true;
          xhr.setRequestHeader("X-XSRFToken", xsrf_token);
          xhr.setRequestHeader(
            "Content-Type",
            "application/json;charset=UTF-8"
          );
          xhr.send(JSON.stringify(post_json));
        };
      };
    </script>
  </head>

  <body>
    <p>
      This is a placeholder for the cloning landing page for the file
      {{ s3_key }} at {{ s3_bucket }}.
    </p>
    <button id="mybutton" onclick="alert('did not get replaced')">
      confirm clone
    </button>
  </body>
</html>
