diff --git a/src/database/database.json b/src/database/database.json index fe51488..0637a08 100644 --- a/src/database/database.json +++ b/src/database/database.json @@ -1 +1 @@ -[] +[] \ No newline at end of file diff --git a/src/public/javascripts/script.js b/src/public/javascripts/script.js index 1d7f12a..729daa6 100644 --- a/src/public/javascripts/script.js +++ b/src/public/javascripts/script.js @@ -1,120 +1,155 @@ +// let $ = require("jquery") +let db = localStorage.getItem("shortens") || "[]"; +db = JSON.parse(db); + +function saveToLocalStorage(data) { + localStorage.setItem("shortens", JSON.stringify(data)); +} + +$(document).on("click", "#shorten", function () { + let data = {} + let url = $("#url").val() + let customKey = $("#custom_key").val() + let isCustomKey = $("#custom_key").is(":checked") + + data["url"] = url + if (isCustomKey) data['custom_key'] = custom_key + + $.ajax({ + async: true, + cache: false, + url: '/api/v1/url', + method: 'post', + data: data, + success: function (response) { + $("#result").show() + $("#error").hide() + let { key, redirect_uri } = response.data + let result_url = window.location.href + key + $("#redirect_uri").val(result_url) + $("#visit_redirect_uri").attr('href', result_url) + db.push({ key, redirect_uri}) + saveToLocalStorage(db) + }, + error: function (response) { + let message = response.data.message + $("#error").show() + $("#result").hide() + $("#error").text(message) + } + }) +}) + $(document).ready(function () { - var db = localStorage.getItem("shortens") || "[]"; - db = JSON.parse(db); - - $("#result").hide(); - $("#error").hide(); - $("#total_shortens").text(db.length); - - if (db.length < 1) { - $("#recent_shortens").hide(); - } else { - $("#recent_shortens").show(); - } - - $("input:checkbox").click(function () { - $("input#custom_key").attr("disabled", !this.checked); - if (this.checked) { - $("input#custom_key").attr("placeholder", "/your-link"); - } else { - $("input#custom_key").attr("placeholder", "Check to customize your link"); - } - }); - - $("#shorten").click(function () { - var url = $("#url").val(); - var customKey = $("#custom_key").val(); - var isCustomKey = $("input:checkbox").is(":checked"); - var data = { - url: url, - }; - - if (isCustomKey) data["custom_key"] = customKey; - - axios - .post("/api/v1/url", data) - .then(({ data: { data: result } }) => { - $("#result").show(); - $("#error").hide(); - var { key, redirect_uri } = result; - $("#redirect_uri").val(`${window.location.href}${key}`); - $("#visit_redirect_uri").attr("href", `${window.location.href}${key}`); - - db.push({ key, redirect_uri }); - saveToLocalStorage(db); - }) - .catch( - ({ - response: { - data: { message }, - }, - }) => { - $("#error").show(); - $("#result").hide(); - $("#error").text(message); - } - ); - }); - - $("#copy").click(function () { - copyToClipboard("#redirect_uri"); - $("#copy").removeClass("btn-outline-info"); - $("#copy").addClass("btn-success"); - $("#copy").text("Copied!"); - setTimeout(() => { - $("#copy").removeClass("btn-success"); - $("#copy").addClass("btn-outline-info"); - $("#copy").addClass("fa fa-copy"); - $("#copy").text(" Copy"); - }, 500); - }); - - db.reverse().forEach(({ key, redirect_uri }) => { - var href = `${window.location.origin}/${key}`; - - var data = `
${redirect_uri}Visit
${href}
`; - $("#shortens").append(data); - }); - - db.slice(db.length - 3, db.length) - .reverse() - .forEach(({ key, redirect_uri }) => { - var href = `${window.location.origin}/${key}`; - - var data = `
${redirect_uri} Visit
${href}
`; - $("#recent_shortens").append(data); - }); - - axios.get("/api/v1/contributors").then(({ data }) => { - var { contributors } = data; - $("#total_contributors").text(contributors.length); - contributors.forEach((user) => { - axios.get(`https://api.github.com/users/${user}`).then(({ data: { avatar_url, url, login, name } }) => { - axios.get("https://api.github.com/repos/kodingkeun/shorturl/commits").then(({ data: commits }) => { - var { - author, - html_url, - commit: { message }, - } = commits.filter((user) => user.author.login === login)[0]; - var commit_data = `${message.split("\n")[0]}`; - var data = `
${ - name || login - } ${author.login === login ? commit_data : ""}Follow
@${login}
`; - $("#contributors").append(data); - }); - }); - }); - }); - - function saveToLocalStorage(data) { - localStorage.setItem("shortens", JSON.stringify(data)); - } - - function copyToClipboard(element) { - var $temp = $(""); - $("body").append($temp); - $temp.val($(element).val()).select(); - document.execCommand("copy"); - $temp.remove(); - } + $("#result").hide(); + $("#error").hide(); + $("#total_shortens").text(db.length); + + if (db.length < 1) { + $("#recent_shortens").hide(); + } else { + $("#recent_shortens").show(); + } + + $("input:checkbox").click(function () { + $("input#custom_key").attr("disabled", !this.checked); + if (this.checked) { + $("input#custom_key").attr("placeholder", "/your-link"); + } else { + $("input#custom_key").attr("placeholder", "Check to customize your link"); + } + }); + + // $("#shorten").click(function () { + // var url = $("#url").val(); + // var customKey = $("#custom_key").val(); + // var isCustomKey = $("input:checkbox").is(":checked"); + // var data = { + // url: url, + // }; + + // if (isCustomKey) data["custom_key"] = customKey; + + // axios + // .post("/api/v1/url", data) + // .then(({ data: { data: result } }) => { + // $("#result").show(); + // $("#error").hide(); + // var { key, redirect_uri } = result; + // $("#redirect_uri").val(`${window.location.href}${key}`); + // $("#visit_redirect_uri").attr("href", `${window.location.href}${key}`); + + // db.push({ key, redirect_uri }); + // saveToLocalStorage(db); + // }) + // .catch( + // ({ + // response: { + // data: { message }, + // }, + // }) => { + // $("#error").show(); + // $("#result").hide(); + // $("#error").text(message); + // } + // ); + // }); + + $("#copy").click(function () { + copyToClipboard("#redirect_uri"); + $("#copy").removeClass("btn-outline-info"); + $("#copy").addClass("btn-success"); + $("#copy").text("Copied!"); + setTimeout(() => { + $("#copy").removeClass("btn-success"); + $("#copy").addClass("btn-outline-info"); + $("#copy").addClass("fa fa-copy"); + $("#copy").text(" Copy"); + }, 500); + }); + + db.reverse().forEach(({ key, redirect_uri }) => { + var href = `${window.location.origin}/${key}`; + + var data = `
${redirect_uri}Visit
${href}
`; + $("#shortens").append(data); + }); + + db.slice(db.length - 3, db.length) + .reverse() + .forEach(({ key, redirect_uri }) => { + var href = `${window.location.origin}/${key}`; + + var data = `
${redirect_uri} Visit
${href}
`; + $("#recent_shortens").append(data); + }); + + axios.get("/api/v1/contributors").then(({ data }) => { + var { contributors } = data; + $("#total_contributors").text(contributors.length); + contributors.forEach((user) => { + axios.get(`https://api.github.com/users/${user}`).then(({ data: { avatar_url, url, login, name } }) => { + axios.get("https://api.github.com/repos/kodingkeun/shorturl/commits").then(({ data: commits }) => { + var { + author, + html_url, + commit: { message }, + } = commits.filter((user) => user.author.login === login)[0]; + var commit_data = `${message.split("\n")[0]}`; + var data = `
${ + name || login + } ${author.login === login ? commit_data : ""}Follow
@${login}
`; + $("#contributors").append(data); + }); + }); + }); + }); + + function copyToClipboard(element) { + var $temp = $(""); + $("body").append($temp); + $temp.val($(element).val()).select(); + document.execCommand("copy"); + $temp.remove(); + } }); diff --git a/src/public/stylesheets/style.css b/src/public/stylesheets/style.css index 9453385..f215ff4 100644 --- a/src/public/stylesheets/style.css +++ b/src/public/stylesheets/style.css @@ -6,3 +6,7 @@ body { a { color: #00B7FF; } + +#result, #error { + display: none; +} \ No newline at end of file diff --git a/src/views/index.ejs b/src/views/index.ejs index 473da65..2a0c7e5 100644 --- a/src/views/index.ejs +++ b/src/views/index.ejs @@ -35,7 +35,7 @@
Paste the URL to be shortened
- +
diff --git a/src/views/partials/result.ejs b/src/views/partials/result.ejs index e708983..ccfa1bf 100644 --- a/src/views/partials/result.ejs +++ b/src/views/partials/result.ejs @@ -1,7 +1,7 @@

Your shorten link

- + Visit
diff --git a/src/views/partials/script.ejs b/src/views/partials/script.ejs index 4f7a305..a8601bf 100644 --- a/src/views/partials/script.ejs +++ b/src/views/partials/script.ejs @@ -1,4 +1,4 @@ - + - + \ No newline at end of file