|
| 1 | +class localStorage { |
| 2 | + constructor() { |
| 3 | + if (typeof(Storage) === "undefined") { |
| 4 | + this.state = false; |
| 5 | + } else { |
| 6 | + this.state = true; |
| 7 | + } |
| 8 | + } |
| 9 | + |
| 10 | + getState() { |
| 11 | + return this.state; |
| 12 | + } |
| 13 | + |
| 14 | + setKey(key, data) { |
| 15 | + if (this.getState()) { |
| 16 | + window.localStorage.setItem(key, data); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + getKey(key) { |
| 21 | + if (this.getState()) { |
| 22 | + return window.localStorage.getItem(key); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + removeKey(key) { |
| 27 | + if (this.getState()) { |
| 28 | + window.localStorage.removeItem(key); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + clearAll() { |
| 33 | + window.localStorage.clear(); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +let ls = new localStorage(); |
| 38 | + |
1 | 39 | $(document).ready(function(){ |
2 | 40 | $(".sidenav").sidenav(); |
3 | 41 | $(".tabs").tabs(); |
4 | 42 | $(".collapsible").collapsible(); |
5 | 43 | $(".tooltipped").tooltip(); |
| 44 | + |
| 45 | + fetchRepos(); |
| 46 | + fetchTeam(); |
6 | 47 | }); |
| 48 | + |
| 49 | +function fetchRepos(force = false) { |
| 50 | + fetch("https://api.github.com/orgs/devCANS/repos", { |
| 51 | + headers: { |
| 52 | + "Accept": "application/vnd.github.v3+json", |
| 53 | + "If-None-Match": ls.getKey("repo-etag") |
| 54 | + } |
| 55 | + }) |
| 56 | + .then( |
| 57 | + function(response) { |
| 58 | + if (response.status !== 200) { |
| 59 | + console.log("[FETCH Repo] Request failed!. Status Code: " + response.status); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + response.json().then(function(data) { |
| 64 | + try { |
| 65 | + ls.setKey("repo-etag", response.headers.get("etag")); |
| 66 | + } catch (e) { |
| 67 | + console.log("[FETCH Repo] ETag Missing"); |
| 68 | + } |
| 69 | + |
| 70 | + updateRepos(data); |
| 71 | + }); |
| 72 | + } |
| 73 | + ) |
| 74 | + .catch(function(err) { |
| 75 | + console.log("[FETCH Repo] Fetch Error!", err); |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +function updateRepos(data) { |
| 80 | + data.forEach(function(i) { |
| 81 | + let outerDiv = createElement("div", {class: "card z-depth-1 scale-card"}); |
| 82 | + let div1 = createElement("div", {class: "card-content black-text"}); |
| 83 | + div1.append(createElement("span", {class: "card-title"}, i.full_name)); |
| 84 | + div1.append(createElement("p", {}, i.description)); |
| 85 | + let d = new Date(i.updated_at); |
| 86 | + lastUpdated = formatDate(d.getDay()) + "-" + formatDate((d.getMonth() + 1)) + "-" + formatDate(d.getFullYear()) + ", " + formatDate(d.getHours()) + ":" + formatDate(d.getMinutes()); |
| 87 | + div1.append(createElement("p", {}, "<b>Last Updated:</b> " + lastUpdated)) |
| 88 | + let div2 = createElement("div", {class: "card-action"}); |
| 89 | + div2.append(createElement("a", {href: i.html_url}, "View on GitHub")); |
| 90 | + outerDiv.append(div1); |
| 91 | + outerDiv.append(div2); |
| 92 | + $("#repo").append(outerDiv); |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +function fetchTeam(force = false) { |
| 97 | + fetch("https://api.github.com/orgs/devCANS/members", { |
| 98 | + headers: { |
| 99 | + "Accept": "application/vnd.github.v3+json", |
| 100 | + "If-None-Match": ls.getKey("team-etag") |
| 101 | + } |
| 102 | + }) |
| 103 | + .then( |
| 104 | + function(response) { |
| 105 | + if (response.status !== 200) { |
| 106 | + console.log("[FETCH Team] Request failed!. Status Code: " + response.status); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + response.json().then(function(data) { |
| 111 | + try { |
| 112 | + ls.setKey("team-etag", response.headers.get("etag")); |
| 113 | + } catch (e) { |
| 114 | + console.log("[FETCH Team] ETag Missing"); |
| 115 | + } |
| 116 | + |
| 117 | + updateTeam(data); |
| 118 | + }); |
| 119 | + } |
| 120 | + ) |
| 121 | + .catch(function(err) { |
| 122 | + console.log("[FETCH Team] Fetch Error!", err); |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +function updateTeam(data) { |
| 127 | + let container = $("#team").find(".card-content"); |
| 128 | + data.forEach(function(i) { |
| 129 | + let anchor = createElement("a", {href: i.html_url, target: "_blank"}); |
| 130 | + anchor.append(createElement("img", {class: "circle team-img tooltipped", src: i.avatar_url, "data-tooltip": i.login})); |
| 131 | + container.append(anchor); |
| 132 | + }); |
| 133 | + $(".tooltipped").tooltip(); |
| 134 | +} |
| 135 | + |
| 136 | +function createElement(tag, options = {}, html = "") { |
| 137 | + let e = document.createElement(tag); |
| 138 | + |
| 139 | + for (const attr in options) { |
| 140 | + $(e).attr(attr, options[attr]); |
| 141 | + } |
| 142 | + |
| 143 | + if (html != "") { |
| 144 | + $(e).html(html); |
| 145 | + } |
| 146 | + return e; |
| 147 | +} |
| 148 | + |
| 149 | +function getMaterialIcon(icon, addClasses = "", DOMElement = true) { |
| 150 | + if (DOMElement) { |
| 151 | + return createElement("i", {class: "material-icons " + addClasses}, icon); |
| 152 | + } |
| 153 | + return "<i class='material-icons " + addClasses + "'>" + icon + "</i>"; |
| 154 | +} |
| 155 | + |
| 156 | +function formatDate(data) { |
| 157 | + if (Number(data) < 10) { |
| 158 | + return "0" + data; |
| 159 | + } |
| 160 | + return data; |
| 161 | +} |
0 commit comments