diff --git a/staff/lucas-orts/ponies/app/home.html b/staff/lucas-orts/ponies/app/home.html deleted file mode 100644 index e69de29bb..000000000 diff --git a/staff/lucas-orts/ponies/app/logic/createPost.js b/staff/lucas-orts/ponies/app/logic/createPost.js new file mode 100644 index 000000000..8458c7a03 --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/createPost.js @@ -0,0 +1,19 @@ +function createPost(image, caption) { + if (!image.startsWith('http')) { + throw new Error('invalid image') + } + + var posts = localStorage.posts !== undefined ? JSON.parse(localStorage.posts) : [] + + var post = { + image: image, + caption: caption, + author: sessionStorage.username, + date: new Date().toISOString(), + id: generateRandomId() + } + + posts.push(post) + + localStorage.posts = JSON.stringify(posts) +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/logic/deletePost.js b/staff/lucas-orts/ponies/app/logic/deletePost.js new file mode 100644 index 000000000..4eda5ad83 --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/deletePost.js @@ -0,0 +1,14 @@ +function deletePost(id) { + + var posts = getAllPosts() + var index = posts.findIndex(function (element) { + + return element.id === id + + }) + + posts.splice(index, 1) + + localStorage.posts = JSON.stringify(posts) + +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/logic/getAllPosts.js b/staff/lucas-orts/ponies/app/logic/getAllPosts.js new file mode 100644 index 000000000..fa27f6e8d --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/getAllPosts.js @@ -0,0 +1,5 @@ +function getAllPosts() { + var posts = localStorage.posts !== undefined ? JSON.parse(localStorage.posts) : [] + + return posts.reverse() +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/logic/getUserName.js b/staff/lucas-orts/ponies/app/logic/getUserName.js new file mode 100644 index 000000000..3aec79a50 --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/getUserName.js @@ -0,0 +1,13 @@ +function getUserName() { + var users = localStorage.users !== undefined ? JSON.parse(localStorage.users) : [] + + var user = users.find(function (user) { + return user.username === sessionStorage.username + }) + + if (user === undefined) { + throw new Error('user not found') + } + + return user.name +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/logic/getUserUsername.js b/staff/lucas-orts/ponies/app/logic/getUserUsername.js new file mode 100644 index 000000000..660002cf1 --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/getUserUsername.js @@ -0,0 +1,3 @@ +function getUserUsername() { + return sessionStorage.username +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/logic/loginUser.js b/staff/lucas-orts/ponies/app/logic/loginUser.js new file mode 100644 index 000000000..ff8ebe07f --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/loginUser.js @@ -0,0 +1,27 @@ +var USER_REGEX = /^(?!.*\s{2})[a-zA-Z0-9._-]{4,16}$/ + +function loginUser(username, password) { + if (!USER_REGEX.test(username)) { + throw new Error('invalid username') + } + + if (password.trim().length < 8) { + throw new Error('invalid password') + } + + var users = localStorage.users !== undefined ? JSON.parse(localStorage.users) : [] + + var user = users.find(function (user) { + return user.username === username + }) + + if (user === undefined) { + throw new Error('username does not exist') + } + + if (user.password !== password) { + throw new Error('wrong password') + } + + sessionStorage.username = username +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/logic/logoutUser.js b/staff/lucas-orts/ponies/app/logic/logoutUser.js new file mode 100644 index 000000000..1307bfdaf --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/logoutUser.js @@ -0,0 +1,3 @@ +function logoutUser() { + delete sessionStorage.username +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/logic/registerUser.js b/staff/lucas-orts/ponies/app/logic/registerUser.js new file mode 100644 index 000000000..3e1573fdb --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/registerUser.js @@ -0,0 +1,59 @@ +var EMAIL_REGEX = /^[a-z0-9._]+@[a-z0-9.-]{3,63}\.[a-z]{2,10}$/ +var NAME_REGEX = /^(?!.*\s{2})[a-zA-Z ]{3,16}$/ +var USER_REGEX = /^(?!.*\s{2})[a-zA-Z0-9._-]{4,16}$/ + +function registerUser(name, surname, email, username, password, passwordRepeat) { + if (!NAME_REGEX.test(name.trim())) { + throw new Error('invalid name') + } + + if (!NAME_REGEX.test(surname.trim())) { + throw new Error('ivalid surname') + } + + if (!EMAIL_REGEX.test(email)) { + throw new Error('invalid email') + } + + if (!USER_REGEX.test(username)) { + throw new Error('invalid username') + } + + if (password.trim().length < 8) { + throw new Error('invalid password') + } + + if (password !== passwordRepeat) { + throw new Error('passwords do not match') + } + + var users = localStorage.users !== undefined ? JSON.parse(localStorage.users) : [] + + var user = users.find(function (user) { + return user.email === email + }) + + if (user !== undefined) { + throw new Error('email already exists') + } + + var user = users.find(function (user) { + return user.username === username + }) + + if (user !== undefined) { + throw new Error('username already exists') + } + + user = { + name: name, + surname: surname, + email: email, + username: username, + password: password + } + + users.push(user) + + localStorage.users = JSON.stringify(users) +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/login.html b/staff/lucas-orts/ponies/app/login.html deleted file mode 100644 index e69de29bb..000000000 diff --git a/staff/lucas-orts/ponies/app/register.html b/staff/lucas-orts/ponies/app/register.html deleted file mode 100644 index e69de29bb..000000000 diff --git a/staff/lucas-orts/ponies/app/utils/formatTime.js b/staff/lucas-orts/ponies/app/utils/formatTime.js new file mode 100644 index 000000000..c54838bf7 --- /dev/null +++ b/staff/lucas-orts/ponies/app/utils/formatTime.js @@ -0,0 +1,41 @@ +function formatTime(date) { + var seconds = Math.round((Date.now() - date.getTime()) / 1000) + + if (seconds < 60) { + return seconds + ' second' + (seconds === 1 ? '' : 's') + } + + var minutes = Math.round(seconds / 60) + + if (minutes < 60) { + return minutes + ' minute' + (minutes === 1 ? '' : 's') + } + + var hours = Math.round(minutes / 60) + + if (hours < 24) { + return hours + ' hour' + (hours === 1 ? '' : 's') + } + + var days = Math.round(hours / 24) + + if (days < 7) { + return days + ' day' + (days === 1 ? '' : 's') + } + + var weeks = Math.round(days / 7) + + if (weeks < 4) { + return weeks + ' week' + (weeks === 1 ? '' : 's') + } + + var months = Math.round(weeks / 4) + + if (months < 12) { + return months + ' month' + (months === 1 ? '' : 's') + } + + var years = Math.round(months / 12) + + return years + ' year' + (years === 1 ? '' : 's') +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/utils/generateRandomId.js b/staff/lucas-orts/ponies/app/utils/generateRandomId.js new file mode 100644 index 000000000..6e3866900 --- /dev/null +++ b/staff/lucas-orts/ponies/app/utils/generateRandomId.js @@ -0,0 +1,3 @@ +function generateRandomId() { + return Math.random().toString() + Date.now().toString() +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/views/home/index.html b/staff/lucas-orts/ponies/app/views/home/index.html new file mode 100644 index 000000000..3345b80cb --- /dev/null +++ b/staff/lucas-orts/ponies/app/views/home/index.html @@ -0,0 +1,32 @@ + + + + + + + Home + + + + + +

Home

+ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/views/home/index.js b/staff/lucas-orts/ponies/app/views/home/index.js new file mode 100644 index 000000000..89bc69b6d --- /dev/null +++ b/staff/lucas-orts/ponies/app/views/home/index.js @@ -0,0 +1,135 @@ +try { + var name = getUserName() + + var title = document.querySelector('h1') + + title.innerText = 'Hello, ' + name + '!' +} catch (error) { + alert(error.message) +} + +var logoutButton = document.getElementById('logout-button') + +logoutButton.onclick = function () { + try { + logoutUser() + + location.href = '../login' + } catch (error) { + alert(error.message) + } +} + +var addPostButton = document.getElementById('add-post-button') + +addPostButton.onclick = function () { + var createPostSection = document.createElement('section') + document.body.appendChild(createPostSection) + + var createPostTitle = document.createElement('h2') + createPostTitle.innerText = 'Create Post' + createPostSection.appendChild(createPostTitle) + + var createPostForm = document.createElement('form') + createPostSection.appendChild(createPostForm) + + createPostForm.onsubmit = function (event) { + event.preventDefault() + + // var postImageInput = document.getElementById('post-image-input') + var postImage = postImageInput.value + var postCaption = postCaptionInput.value + + try { + createPost(postImage, postCaption) + + document.body.removeChild(createPostSection) + + for (var i = postListSection.children.length - 1; i > -1; i--) { + var child = postListSection.children[i] + + postListSection.removeChild(child) + } + + listPosts() + } catch (error) { + alert(error.message) + } + } + + var postImageLabel = document.createElement('label') + postImageLabel.htmlFor = 'post-image-input' + postImageLabel.innerText = 'Image' + createPostForm.appendChild(postImageLabel) + + var postImageInput = document.createElement('input') + postImageInput.id = postImageLabel.htmlFor + createPostForm.appendChild(postImageInput) + + var postCaptionLabel = document.createElement('label') + postCaptionLabel.htmlFor = 'post-caption-input' + postCaptionLabel.innerText = 'Caption' + createPostForm.appendChild(postCaptionLabel) + + var postCaptionInput = document.createElement('input') + postCaptionInput.id = postCaptionLabel.htmlFor + createPostForm.appendChild(postCaptionInput) + + var postButtonSubmit = document.createElement('button') + postButtonSubmit.type = 'submit' + postButtonSubmit.innerText = 'Submit' + createPostForm.appendChild(postButtonSubmit) + + var postCancelButton = document.createElement('button') + postCancelButton.innerText = 'Cancel' + postCancelButton.type = 'reset' + createPostForm.appendChild(postCancelButton) + + postCancelButton.onclick = function () { + document.body.removeChild(createPostSection) + } +} + +var postListSection = document.createElement('section') +document.body.appendChild(postListSection) + +function listPosts() { + var posts = getAllPosts() + + posts.forEach(function (post) { + var postArticle = document.createElement('article') + postListSection.appendChild(postArticle) + + var postAuthorTitle = document.createElement('h3') + postAuthorTitle.innerText = post.author + postArticle.appendChild(postAuthorTitle) + + var postImage = document.createElement('img') + postImage.src = post.image + postArticle.appendChild(postImage) + + var postCaptionText = document.createElement('p') + postCaptionText.innerText = post.caption + postArticle.appendChild(postCaptionText) + + var postDateTime = document.createElement('time') + postDateTime.innerText = formatTime(new Date(post.date)) + postArticle.appendChild(postDateTime) + + if (post.author === getUserUsername()) { + var postDeleteButton = document.createElement('button') + postDeleteButton.innerText = 'Delete' + postArticle.appendChild(postDeleteButton) + + postDeleteButton.onclick = function () { + + var id = post.id + + deletePost(id) + postListSection.removeChild(postArticle) + } + } + }) +} + +listPosts() \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/views/login/index.html b/staff/lucas-orts/ponies/app/views/login/index.html new file mode 100644 index 000000000..1e853f2e0 --- /dev/null +++ b/staff/lucas-orts/ponies/app/views/login/index.html @@ -0,0 +1,33 @@ + + + + + + + Login + + + + + +

Login

+ +
+ + + + + + + +
+ + Register + + + + + + \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/views/login/index.js b/staff/lucas-orts/ponies/app/views/login/index.js new file mode 100644 index 000000000..0a9f6879f --- /dev/null +++ b/staff/lucas-orts/ponies/app/views/login/index.js @@ -0,0 +1,27 @@ +var form = document.querySelector('form') + +form.onsubmit = function (event) { + event.preventDefault() + + var usernameInput = document.getElementById('username-input') + var passwordInput = document.getElementById('password-input') + + var username = usernameInput.value + var password = passwordInput.value + + try { + loginUser(username, password) + + location.href = '../home' + } catch (error) { + alert(error.message) + } +} + +var a = document.querySelector('a') + +a.onclick = function (event) { + event.preventDefault() + + window.location.href = '../register' +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/views/register/index.html b/staff/lucas-orts/ponies/app/views/register/index.html new file mode 100644 index 000000000..9be3801a4 --- /dev/null +++ b/staff/lucas-orts/ponies/app/views/register/index.html @@ -0,0 +1,45 @@ + + + + + + + Register + + + + + +

Register

+ +
+ + + + + + + + + + + + + + + + + + + +
+ + Login + + + + + + \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/views/register/index.js b/staff/lucas-orts/ponies/app/views/register/index.js new file mode 100644 index 000000000..8136675da --- /dev/null +++ b/staff/lucas-orts/ponies/app/views/register/index.js @@ -0,0 +1,37 @@ +var form = document.querySelector('form') + +form.onsubmit = function (event) { + event.preventDefault() + + var nameInput = document.getElementById('name-input') + var surnameInput = document.getElementById('surname-input') + var emailInput = document.getElementById('email-input') + var usernameInput = document.getElementById('username-input') + var passwordInput = document.getElementById('password-input') + var passwordRepeatInput = document.getElementById('password2-input') + + var name = nameInput.value + var surname = surnameInput.value + var email = emailInput.value + var username = usernameInput.value + var password = passwordInput.value + var passwordRepeat = passwordRepeatInput.value + + try { + registerUser(name, surname, email, username, password, passwordRepeat) + + alert('user successfully registered') + + location.href = '../login' + } catch (error) { + alert(error.message) + } +} + +var a = document.querySelector('a') + +a.onclick = function (event) { + event.preventDefault() + + location.href = '../login' +} \ No newline at end of file