From ed8b731020c8fa77a0dd45faeae3a4b2ab8aa8b3 Mon Sep 17 00:00:00 2001 From: Aarchyy89 Date: Tue, 15 Oct 2024 11:35:54 +0200 Subject: [PATCH] Re ordered files, added compo extends and add posts #174 --- .../unsocial/{ => Prev_Index}/index.1.html | 0 .../unsocial/{ => Prev_Index}/index.2.html | 0 .../unsocial/{ => Prev_Index}/index.3.html | 0 staff/aaron-barrios/unsocial/compo.demo.html | 18 -- staff/aaron-barrios/unsocial/compo.demo.js | 22 -- staff/aaron-barrios/unsocial/compo.js | 240 --------------- staff/aaron-barrios/unsocial/compo/Button.js | 10 + staff/aaron-barrios/unsocial/compo/Compo.js | 32 ++ staff/aaron-barrios/unsocial/compo/Form.js | 11 + staff/aaron-barrios/unsocial/compo/Heading.js | 8 + staff/aaron-barrios/unsocial/compo/Image.js | 8 + staff/aaron-barrios/unsocial/compo/Input.js | 28 ++ staff/aaron-barrios/unsocial/compo/Label.js | 9 + staff/aaron-barrios/unsocial/compo/Link.js | 9 + .../aaron-barrios/unsocial/compo/ListItem.js | 5 + .../aaron-barrios/unsocial/compo/Paragraph.js | 15 + .../unsocial/compo/PasswordInput.js | 35 +++ staff/aaron-barrios/unsocial/compo/Span.js | 16 + staff/aaron-barrios/unsocial/compo/Time.js | 15 + .../unsocial/compo/UnorderedList.js | 5 + staff/aaron-barrios/unsocial/compo/index.html | 33 ++ staff/aaron-barrios/unsocial/compo/main.js | 28 ++ .../unsocial/{data.js => data/posts.js} | 7 - staff/aaron-barrios/unsocial/data/users.js | 6 + staff/aaron-barrios/unsocial/index.html | 39 ++- .../unsocial/logic/authenticateUser.js | 13 + .../unsocial/logic/createPost.js | 17 ++ .../aaron-barrios/unsocial/logic/getPosts.js | 3 + .../{logic.js => logic/registerUser.js} | 41 +-- .../unsocial/proto-chain/index.js | 4 + staff/aaron-barrios/unsocial/view.js | 281 ------------------ .../aaron-barrios/unsocial/view/CreatePost.js | 46 +++ staff/aaron-barrios/unsocial/view/Home.js | 41 +++ staff/aaron-barrios/unsocial/view/Login.js | 64 ++++ staff/aaron-barrios/unsocial/view/PostItem.js | 18 ++ staff/aaron-barrios/unsocial/view/PostList.js | 23 ++ staff/aaron-barrios/unsocial/view/Register.js | 71 +++++ 37 files changed, 609 insertions(+), 612 deletions(-) rename staff/aaron-barrios/unsocial/{ => Prev_Index}/index.1.html (100%) rename staff/aaron-barrios/unsocial/{ => Prev_Index}/index.2.html (100%) rename staff/aaron-barrios/unsocial/{ => Prev_Index}/index.3.html (100%) delete mode 100644 staff/aaron-barrios/unsocial/compo.demo.html delete mode 100644 staff/aaron-barrios/unsocial/compo.demo.js delete mode 100644 staff/aaron-barrios/unsocial/compo.js create mode 100644 staff/aaron-barrios/unsocial/compo/Button.js create mode 100644 staff/aaron-barrios/unsocial/compo/Compo.js create mode 100644 staff/aaron-barrios/unsocial/compo/Form.js create mode 100644 staff/aaron-barrios/unsocial/compo/Heading.js create mode 100644 staff/aaron-barrios/unsocial/compo/Image.js create mode 100644 staff/aaron-barrios/unsocial/compo/Input.js create mode 100644 staff/aaron-barrios/unsocial/compo/Label.js create mode 100644 staff/aaron-barrios/unsocial/compo/Link.js create mode 100644 staff/aaron-barrios/unsocial/compo/ListItem.js create mode 100644 staff/aaron-barrios/unsocial/compo/Paragraph.js create mode 100644 staff/aaron-barrios/unsocial/compo/PasswordInput.js create mode 100644 staff/aaron-barrios/unsocial/compo/Span.js create mode 100644 staff/aaron-barrios/unsocial/compo/Time.js create mode 100644 staff/aaron-barrios/unsocial/compo/UnorderedList.js create mode 100644 staff/aaron-barrios/unsocial/compo/index.html create mode 100644 staff/aaron-barrios/unsocial/compo/main.js rename staff/aaron-barrios/unsocial/{data.js => data/posts.js} (57%) create mode 100644 staff/aaron-barrios/unsocial/data/users.js create mode 100644 staff/aaron-barrios/unsocial/logic/authenticateUser.js create mode 100644 staff/aaron-barrios/unsocial/logic/createPost.js create mode 100644 staff/aaron-barrios/unsocial/logic/getPosts.js rename staff/aaron-barrios/unsocial/{logic.js => logic/registerUser.js} (64%) create mode 100644 staff/aaron-barrios/unsocial/proto-chain/index.js delete mode 100644 staff/aaron-barrios/unsocial/view.js create mode 100644 staff/aaron-barrios/unsocial/view/CreatePost.js create mode 100644 staff/aaron-barrios/unsocial/view/Home.js create mode 100644 staff/aaron-barrios/unsocial/view/Login.js create mode 100644 staff/aaron-barrios/unsocial/view/PostItem.js create mode 100644 staff/aaron-barrios/unsocial/view/PostList.js create mode 100644 staff/aaron-barrios/unsocial/view/Register.js diff --git a/staff/aaron-barrios/unsocial/index.1.html b/staff/aaron-barrios/unsocial/Prev_Index/index.1.html similarity index 100% rename from staff/aaron-barrios/unsocial/index.1.html rename to staff/aaron-barrios/unsocial/Prev_Index/index.1.html diff --git a/staff/aaron-barrios/unsocial/index.2.html b/staff/aaron-barrios/unsocial/Prev_Index/index.2.html similarity index 100% rename from staff/aaron-barrios/unsocial/index.2.html rename to staff/aaron-barrios/unsocial/Prev_Index/index.2.html diff --git a/staff/aaron-barrios/unsocial/index.3.html b/staff/aaron-barrios/unsocial/Prev_Index/index.3.html similarity index 100% rename from staff/aaron-barrios/unsocial/index.3.html rename to staff/aaron-barrios/unsocial/Prev_Index/index.3.html diff --git a/staff/aaron-barrios/unsocial/compo.demo.html b/staff/aaron-barrios/unsocial/compo.demo.html deleted file mode 100644 index 8d7651b4..00000000 --- a/staff/aaron-barrios/unsocial/compo.demo.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - Compo v0.0.0 - - - - - - - - - - - \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/compo.demo.js b/staff/aaron-barrios/unsocial/compo.demo.js deleted file mode 100644 index 40a59301..00000000 --- a/staff/aaron-barrios/unsocial/compo.demo.js +++ /dev/null @@ -1,22 +0,0 @@ -var page = new Compo(document.body) - -var title = new Heading('Compo', 1) -page.add(title) - -var inputTitle = new Heading('Input', 2) -page.add(inputTitle) - -var input = new Input('password', 'password') -page.add(input) - -var linkTitle = new Heading('Link', 2) -page.add(linkTitle) - -var link = new Link('Google') -page.add(link) - -var passwordInputTitle = new Heading('PasswordInput', 2) -page.add(passwordInputTitle) - -var passwordInput = new PasswordInput('password') -page.add(passwordInput) \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/compo.js b/staff/aaron-barrios/unsocial/compo.js deleted file mode 100644 index 178556e4..00000000 --- a/staff/aaron-barrios/unsocial/compo.js +++ /dev/null @@ -1,240 +0,0 @@ -//FUNCIÓN PADRE -function Compo(container) { - this.children = [] - this.container = container - - // this.parent = null -} - -Compo.prototype.add = function (child) { - this.children.push(child) - - // child.parent = this - - this.container.appendChild(child.container) -} - -Compo.prototype.remove = function () { - // var index = this.parent.children.findIndex(function (child) { - // return child === this - // }.bind(this)) - - // if (index > -1) - // this.parent.children.splice(index, 1) - - this.container.remove() -} - -Compo.prototype.addBehavior = function (type, callback) { - this.container.addEventListener(type, callback) -} - - -//FORMULARIOS -function Form(container) { - Compo.call(this, document.createElement('form')) -} - -Form.prototype = Object.create(Compo.prototype) -Form.prototype.container = Form - -Form.prototype.reset = function () { - this.container.reset() -} - - -//BUTTON INSTANCES -function Button(text, type) { - Compo.call(this, document.createElement('button')) - - this.container.innerText = text - this.container.type = type -} - -Button.prototype = Object.create(Compo.prototype) -Button.prototype.constructor = Button - - -//LABEL INSTANCES -function Label(text, id) { - Compo.call(this, document.createElement('label')) - - this.container.innerText = text - this.container.htmlFor = id -} - -Label.prototype = Object.create(Compo.prototype) -Label.prototype.constructor = Label - - - -//INPUT Instances -function Input(type, id) { - Compo.call(this, document.createElement('input')) - this.container.style.width = '100%' - this.container.style.boxSizing = 'border-box' - - this.container.type = type - this.container.id = id -} - -Input.prototype = Object.create(Compo.prototype) -Input.prototype.constructor = Input - - -Input.prototype.getValue = function () { - return this.container.value -} - -Input.prototype.setValue = function (value) { - this.container.value = value -} - -Input.prototype.getType = function () { - return this.container.type -} - -Input.prototype.setType = function (type) { - this.container.type = type -} - - -//HEADING INSTANCES -function Heading(text, level) { - Compo.call(this, document.createElement('h' + level)) - - this.container.innerText = text -} - -Heading.prototype = Object.create(Compo.prototype) -Heading.prototype.constructor = Heading - - -//LINK INSTANCES -function Link(text) { - Compo.call(this, document.createElement('a')) - - this.container.innerText = text - this.container.href = '' -} - -Link.prototype = Object.create(Compo.prototype) -Link.prototype.constructor = Link - - -//SPAN INSTANCES -function Span(text) { - Compo.call(this, document.createElement('span')) - - this.container.innerText = text -} - -Span.prototype = Object.create(Compo.prototype) -Span.prototype.constructor = Span - - -Span.prototype.getText = function () { - return this.container.innerText -} - -Span.prototype.setText = function (text) { - this.container.innerText = text -} - -//PASSWORDINPUT INSTANCES -function PasswordInput(id) { - Compo.call(this, document.createElement('div')) - this.container.style.display = 'flex' - - var input = new Input('password', id) - input.container.style.paddingRight = '18px' - this.add(input) - - var span = new Span('🔒') - span.container.style.cursor = 'pointer' - span.container.style.position = 'absolute' - span.container.style.right = '10px' - this.add(span) - - span.addBehavior('click', function () { - if (span.getText() === '🔒') { - input.setType('text') - span.setText('👁️‍🗨️') - } else { - input.setType('password') - span.setText('🔒') - } - }) -} - -PasswordInput.prototype = Object.create(Compo.prototype) -PasswordInput.prototype.constructor = PasswordInput - -PasswordInput.prototype.getValue = function () { - return this.children[0].container.value -} - -PasswordInput.prototype.setValue = function (value) { - this.container.value = value -} - - -//----- POST STUFF ----- -function UnorderedList() { - Compo.call(this, document.createElement('ul')) -} - -UnorderedList.prototype = Object.create(Compo.prototype) -UnorderedList.prototype.constructor = UnorderedList - -function ListItem() { - Compo.call(this, document.createElement('li')) -} - -ListItem.prototype = Object.create(Compo.prototype) -ListItem.prototype.constructor = ListItem - -function Image(address) { - Compo.call(this, document.createElement('img')) - - this.container.src = address - this.container.style.width = '100%' -} - -Image.prototype = Object.create(Compo.prototype) -Image.prototype.constructor = Image - -function Paragraph(text) { - Compo.call(this, document.createElement('p')) - - this.container.innerText = text -} - -Paragraph.prototype = Object.create(Compo.prototype) -Paragraph.prototype.constructor = Paragraph - -Paragraph.prototype.getText = function () { - return this.container.innerText -} - -Paragraph.prototype.setText = function (text) { - this.container.innerText = text -} - - -function Time(text) { - Compo.call(this, document.createElement('time')) - - this.container.innerText = text -} - -Time.prototype = Object.create(Compo.prototype) -Time.prototype.constructor = Time - -Time.prototype.getText = function () { - return this.container.innerText -} - -Time.prototype.setText = function (text) { - this.container.innerText = text -} diff --git a/staff/aaron-barrios/unsocial/compo/Button.js b/staff/aaron-barrios/unsocial/compo/Button.js new file mode 100644 index 00000000..e5446cec --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/Button.js @@ -0,0 +1,10 @@ +//BUTTON INSTANCES +class Button extends Compo { + constructor(text, type) { + super(document.createElement('button')) + + this.container.innerText = text + this.container.type = type + } + +} diff --git a/staff/aaron-barrios/unsocial/compo/Compo.js b/staff/aaron-barrios/unsocial/compo/Compo.js new file mode 100644 index 00000000..85bd11ea --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/Compo.js @@ -0,0 +1,32 @@ +//FUNCIÓN PADRE +class Compo { + constructor(container) { + this.children = [] + this.container = container + + // this.parent = null + } + + add = function (child) { + this.children.push(child) + + // child.parent = this + + this.container.appendChild(child.container) + } + + remove = function () { + // var index = this.parent.children.findIndex(function (child) { + // return child === this + // }.bind(this)) + + // if (index > -1) + // this.parent.children.splice(index, 1) + + this.container.remove() + } + + addBehavior = function (type, callback) { + this.container.addEventListener(type, callback) + } +} \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/compo/Form.js b/staff/aaron-barrios/unsocial/compo/Form.js new file mode 100644 index 00000000..86853219 --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/Form.js @@ -0,0 +1,11 @@ +//FORMULARIOS +class Form extends Compo { + constructor(container) { + super(document.createElement('form')) + } + + + reset = function () { + this.container.reset() + } +} diff --git a/staff/aaron-barrios/unsocial/compo/Heading.js b/staff/aaron-barrios/unsocial/compo/Heading.js new file mode 100644 index 00000000..f80c6824 --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/Heading.js @@ -0,0 +1,8 @@ +//HEADING INSTANCES +class Heading extends Compo { + constructor(text, level) { + super(document.createElement('h' + level)) + + this.container.innerText = text + } +} diff --git a/staff/aaron-barrios/unsocial/compo/Image.js b/staff/aaron-barrios/unsocial/compo/Image.js new file mode 100644 index 00000000..6820dc74 --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/Image.js @@ -0,0 +1,8 @@ +class Image extends Compo { + constructor(address) { + super(document.createElement('img')) + + this.container.src = address + this.container.style.width = '100%' + } +} \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/compo/Input.js b/staff/aaron-barrios/unsocial/compo/Input.js new file mode 100644 index 00000000..fc6f438d --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/Input.js @@ -0,0 +1,28 @@ +//INPUT Instances +class Input extends Compo { + constructor(type, id) { + super(document.createElement('input')) + this.container.style.width = '100%' + this.container.style.boxSizing = 'border-box' + + this.container.type = type + this.container.id = id + } + + + getValue = function () { + return this.container.value + } + + setValue = function (value) { + this.container.value = value + } + + getType = function () { + return this.container.type + } + + setType = function (type) { + this.container.type = type + } +} diff --git a/staff/aaron-barrios/unsocial/compo/Label.js b/staff/aaron-barrios/unsocial/compo/Label.js new file mode 100644 index 00000000..599dc14b --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/Label.js @@ -0,0 +1,9 @@ +//LABEL INSTANCES +class Label extends Compo { + constructor(text, id) { + super(document.createElement('label')) + + this.container.innerText = text + this.container.htmlFor = id + } +} diff --git a/staff/aaron-barrios/unsocial/compo/Link.js b/staff/aaron-barrios/unsocial/compo/Link.js new file mode 100644 index 00000000..61702ee0 --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/Link.js @@ -0,0 +1,9 @@ +//LINK INSTANCES +class Link extends Compo { + constructor(text) { + super(document.createElement('a')) + + this.container.innerText = text + this.container.href = '' + } +} diff --git a/staff/aaron-barrios/unsocial/compo/ListItem.js b/staff/aaron-barrios/unsocial/compo/ListItem.js new file mode 100644 index 00000000..32ecbbf1 --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/ListItem.js @@ -0,0 +1,5 @@ +class ListItem extends Compo { + constructor() { + super(document.createElement('li')) + } +} \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/compo/Paragraph.js b/staff/aaron-barrios/unsocial/compo/Paragraph.js new file mode 100644 index 00000000..f395f991 --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/Paragraph.js @@ -0,0 +1,15 @@ +class Paragraph extends Compo { + constructor(text) { + super(document.createElement('p')) + + this.container.innerText = text + } + + getText = function () { + return this.container.innerText + } + + setText = function (text) { + this.container.innerText = text + } +} \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/compo/PasswordInput.js b/staff/aaron-barrios/unsocial/compo/PasswordInput.js new file mode 100644 index 00000000..741b98d3 --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/PasswordInput.js @@ -0,0 +1,35 @@ +//PASSWORDINPUT INSTANCES +class PasswordInput extends Compo { + constructor(id) { + super(document.createElement('div')) + this.container.style.display = 'flex' + + var input = new Input('password', id) + input.container.style.paddingRight = '18px' + this.add(input) + + var span = new Span('🔒') + span.container.style.cursor = 'pointer' + span.container.style.position = 'absolute' + span.container.style.right = '10px' + this.add(span) + + span.addBehavior('click', function () { + if (span.getText() === '🔒') { + input.setType('text') + span.setText('👁️‍🗨️') + } else { + input.setType('password') + span.setText('🔒') + } + }) + } + + getValue = function () { + return this.children[0].container.value + } + + setValue = function (value) { + this.container.value = value + } +} \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/compo/Span.js b/staff/aaron-barrios/unsocial/compo/Span.js new file mode 100644 index 00000000..731eb35f --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/Span.js @@ -0,0 +1,16 @@ +//SPAN INSTANCES +class Span extends Compo { + constructor(text) { + super(document.createElement('span')) + + this.container.innerText = text + } + + getText = function () { + return this.container.innerText + } + + setText = function (text) { + this.container.innerText = text + } +} \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/compo/Time.js b/staff/aaron-barrios/unsocial/compo/Time.js new file mode 100644 index 00000000..ab754fc9 --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/Time.js @@ -0,0 +1,15 @@ +class Time extends Compo { + constructor(text) { + super(document.createElement('time')) + + this.container.innerText = text + } + + getText = function () { + return this.container.innerText + } + + setText = function (text) { + this.container.innerText = text + } +} diff --git a/staff/aaron-barrios/unsocial/compo/UnorderedList.js b/staff/aaron-barrios/unsocial/compo/UnorderedList.js new file mode 100644 index 00000000..dc9cda2a --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/UnorderedList.js @@ -0,0 +1,5 @@ +class UnorderedList extends Compo { + constructor() { + super(document.createElement('ul')) + } +} diff --git a/staff/aaron-barrios/unsocial/compo/index.html b/staff/aaron-barrios/unsocial/compo/index.html new file mode 100644 index 00000000..dab4cc55 --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/index.html @@ -0,0 +1,33 @@ + + + + + + + Compo v0.0.0 + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/compo/main.js b/staff/aaron-barrios/unsocial/compo/main.js new file mode 100644 index 00000000..6142f47d --- /dev/null +++ b/staff/aaron-barrios/unsocial/compo/main.js @@ -0,0 +1,28 @@ +const page = new Compo(document.body) + +const title = new Heading('Compo', 1) +page.add(title) + +{ + var inputTitle = new Heading('Input', 2) + page.add(inputTitle) + + var input = new Input('password', 'password') + page.add(input) +} + +{ + var linkTitle = new Heading('Link', 2) + page.add(linkTitle) + + var link = new Link('Google') + page.add(link) +} + +{ + var passwordInputTitle = new Heading('PasswordInput', 2) + page.add(passwordInputTitle) + + var passwordInput = new PasswordInput('password') + page.add(passwordInput) +} diff --git a/staff/aaron-barrios/unsocial/data.js b/staff/aaron-barrios/unsocial/data/posts.js similarity index 57% rename from staff/aaron-barrios/unsocial/data.js rename to staff/aaron-barrios/unsocial/data/posts.js index 99f98a89..2e7c3471 100644 --- a/staff/aaron-barrios/unsocial/data.js +++ b/staff/aaron-barrios/unsocial/data/posts.js @@ -1,10 +1,3 @@ -//USUARIOS POR DEFECTO -var users = [ - { name: 'Juan Pablo', email: 'juanpablo@gmail.com', username: 'juanpablo', password: '123' }, - { name: 'Aaron', email: 'aaron@gmail.com', username: 'aaron', password: '123' }, - { name: 'Claudi', email: 'Claudi@gmail.com', username: 'claudi', password: '123' } -] - var posts = [ { image: diff --git a/staff/aaron-barrios/unsocial/data/users.js b/staff/aaron-barrios/unsocial/data/users.js new file mode 100644 index 00000000..e75e8429 --- /dev/null +++ b/staff/aaron-barrios/unsocial/data/users.js @@ -0,0 +1,6 @@ +//USUARIOS POR DEFECTO +var users = [ + { name: 'Juan Pablo', email: 'juanpablo@gmail.com', username: 'juanpablo', password: '123' }, + { name: 'Aaron', email: 'aaron@gmail.com', username: 'aaron', password: '123' }, + { name: 'Claudi', email: 'Claudi@gmail.com', username: 'claudi', password: '123' } +] diff --git a/staff/aaron-barrios/unsocial/index.html b/staff/aaron-barrios/unsocial/index.html index 6a35d203..05a2ddb3 100644 --- a/staff/aaron-barrios/unsocial/index.html +++ b/staff/aaron-barrios/unsocial/index.html @@ -11,12 +11,43 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - diff --git a/staff/aaron-barrios/unsocial/logic/authenticateUser.js b/staff/aaron-barrios/unsocial/logic/authenticateUser.js new file mode 100644 index 00000000..3b48c6dc --- /dev/null +++ b/staff/aaron-barrios/unsocial/logic/authenticateUser.js @@ -0,0 +1,13 @@ +function authenticateUser(username, password) { + if (username.length < 4 || username.length > 12) + throw new Error('Invalid username') + + var user = users.find(function (user) { + return user.username === username && user.password === password + }) + + if (user === undefined) + throw new Error('Wrong credentials') + + return user +} diff --git a/staff/aaron-barrios/unsocial/logic/createPost.js b/staff/aaron-barrios/unsocial/logic/createPost.js new file mode 100644 index 00000000..0a57d790 --- /dev/null +++ b/staff/aaron-barrios/unsocial/logic/createPost.js @@ -0,0 +1,17 @@ +// ----- POSTS STUFF ------ +function createPost(username, image, text) { + + if (username.length < 4 || username.length > 12) + throw new Error('Invalid username') + + //TO DO: input validation (and throw error) + + var post = { + image: image, + text: text, + username: username, + date: new Date + } + + posts.push(post) +} \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/logic/getPosts.js b/staff/aaron-barrios/unsocial/logic/getPosts.js new file mode 100644 index 00000000..1f8816b9 --- /dev/null +++ b/staff/aaron-barrios/unsocial/logic/getPosts.js @@ -0,0 +1,3 @@ +function getPosts() { + return posts +} diff --git a/staff/aaron-barrios/unsocial/logic.js b/staff/aaron-barrios/unsocial/logic/registerUser.js similarity index 64% rename from staff/aaron-barrios/unsocial/logic.js rename to staff/aaron-barrios/unsocial/logic/registerUser.js index 44e8f50f..c60a6c7e 100644 --- a/staff/aaron-barrios/unsocial/logic.js +++ b/staff/aaron-barrios/unsocial/logic/registerUser.js @@ -38,43 +38,4 @@ function registerUser(name, email, username, password, passwordRepeat) { user = { name: name, email: email, username: username, password: password } users.push(user) -} - - -function authenticateUser(username, password) { - if (username.length < 4 || username.length > 12) - throw new Error('Invalid username') - - var user = users.find(function (user) { - return user.username === username && user.password === password - }) - - if (user === undefined) - throw new Error('Wrong credentials') - - return user -} - - -// ----- POSTS STUFF ------ -function createPost(username, image, text) { - - if (username.length < 4 || username.length > 12) - throw new Error('Invalid username') - - //TO DO: input validation (and throw error) - - var post = { - image: image, - text: text, - username: username, - date: new Date - } - - posts.push(post) -} - -function getPosts() { - return posts -} - +} \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/proto-chain/index.js b/staff/aaron-barrios/unsocial/proto-chain/index.js new file mode 100644 index 00000000..9f218a67 --- /dev/null +++ b/staff/aaron-barrios/unsocial/proto-chain/index.js @@ -0,0 +1,4 @@ +Function.prototype.extends = function (from) { + this.prototype = Object.create(from.prototype) + this.prototype.constructor = this +} \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/view.js b/staff/aaron-barrios/unsocial/view.js deleted file mode 100644 index 0cc08385..00000000 --- a/staff/aaron-barrios/unsocial/view.js +++ /dev/null @@ -1,281 +0,0 @@ -//CREACIÓN SECCIONES MEDIANTE JAVASCRIPT - -//----- LOGIN SECTION ----- -function Login() { - Compo.call(this, document.createElement('section')) - - var title = new Heading('Login', 2) - this.add(title) - - var form = new Form() - this.add(form) - - form.add(new Label('Username', 'username')) - var usernameInput = new Input('text', 'username') - form.add(usernameInput) - - form.add(new Label('Password', 'password')) - var passwordInput = new PasswordInput('password') - form.add(passwordInput) - - var submitButton = new Button('Login', 'submit') - form.add(submitButton) - - form.addBehavior('submit', function (event) { - event.preventDefault() - - var username = usernameInput.getValue() - var password = passwordInput.getValue() - - try { - loggedInUser = authenticateUser(username, password) - - form.reset() - - this.remove() - - home = new Home() - - page.add(home) - } catch (error) { - passwordInput.setValue('') - - alert(error.message) - - console.error(error) - } - }.bind(this)) - - - - var registerLink = new Link('Register') - this.add(registerLink) - - registerLink.addBehavior('click', function (event) { - event.preventDefault() - - this.remove() - - var register = new Register() - - page.add(register) - }.bind(this)) - -} - -Login.prototype = Object.create(Compo.prototype) -Login.prototype.constructor = Login - - -//REGISTER INSTANCES -function Register() { - Compo.call(this, document.createElement('section')) - - var title = new Heading('Register', 2) - this.add(title) - - var form = new Form() - this.add(form) - - form.add(new Label('Name', 'name')) - var nameInput = new Input('text', 'name') - form.add(nameInput) - - form.add(new Label('E-mail', 'email')) - var emailInput = new Input('email', 'email') - form.add(emailInput) - - form.add(new Label('Username', 'username')) - var usernameInput = new Input('text', 'username') - form.add(usernameInput) - - form.add(new Label('Password', 'password')) - var passwordInput = new PasswordInput('password') - form.add(passwordInput) - - form.add(new Label('Repeat Password', 'password-repeat')) - var passwordRepeatInput = new PasswordInput('password-repeat') - form.add(passwordRepeatInput) - - var submitButton = new Button('Register', 'submit') - form.add(submitButton) - - form.addBehavior('submit', function (event) { - event.preventDefault() - - var name = nameInput.getValue() - var email = emailInput.getValue() - var username = usernameInput.getValue() - var password = passwordInput.getValue() - var passwordRepeat = passwordRepeatInput.getValue() - - try { - registerUser(name, email, username, password, passwordRepeat) - - form.reset() - - this.remove() - - page.add(login) - } catch (error) { - alert(error.message) - - console.error(error) - } - }.bind(this)) - - - var loginLink = new Link('Login') - this.add(loginLink) - - loginLink.addBehavior('click', function (event) { - event.preventDefault() - - this.remove() - - page.add(login) - }.bind(this)) -} - -Register.prototype = Object.create(Compo.prototype) -Register.prototype.constructor = Register - - - -//HOME INSTANCES -function Home() { - Compo.call(this, document.createElement('section')) - - var title = new Heading('Home', 2) - this.add(title) - - var userTitle = new Heading('Hello, ' + loggedInUser.name + '!', 3) - this.add(userTitle) - - var logoutButton = new Button('Logout', 'button') - this.add(logoutButton) - - logoutButton.addBehavior('click', function (event) { - event.preventDefault() - - loggedInUser = null - - this.remove() - - page.add(login) - - }.bind(this)) - - - var addPostButton = new Button('+', 'button') - this.add(addPostButton) - - addPostButton.addBehavior('click', function (event) { - var createPost = new CreatePost() - - this.children[this.children.length - 1].remove() - - this.add(createPost) - }.bind(this)) - - var postList = new PostList() - this.add(postList) -} - -Home.prototype = Object.create(Compo.prototype) -Home.prototype.constructor = Home - -//Función que me crea posts -function CreatePost() { - Compo.call(this, document.createElement('div')) - - var title = new Heading('Create Post', 3) - this.add(title) - - var form = new Form() - - var imageLabel = new Label('Image', 'image') - var imageInput = new Input('text', 'image') - form.add(imageLabel) - form.add(imageInput) - - var textLabel = new Label('Text', 'text') - var textInput = new Input('text', 'text') - form.add(textLabel) - form.add(textInput) - - var submitButton = new Button('Create', 'submit') - form.add(submitButton) - - this.add(form) - - form.addBehavior('submit', function (event) { - event.preventDefault() - - var image = imageInput.getValue() - var text = textInput.getValue() - - try { - createPost(loggedInUser.username, image, text) - - this.remove() - - var postList = new PostList() - home.add(postList) - } catch (error) { - alert(error.message) - - console.error(error) - } - }.bind(this)) -} - -CreatePost.prototype = Object.create(Compo.prototype) -CreatePost.prototype.constructor = CreatePost - - -//FUNCIÓN PARA EL PROPIO POST -function Post(username, image, text, date) { - Compo.call(this, document.createElement('div')) - - var userTitle = new Heading(username, 4) - this.add(userTitle) - - var picture = new Image(image) - this.add(picture) - - var comment = new Paragraph(text) - this.add(comment) - - var time = new Time(date) - this.add(time) -} - -Post.prototype = Object.create(Compo.prototype) -Post.prototype.constructor = Post - - -//Función que me carga todos los posts -function PostList() { - Compo.call(this, document.createElement('div')) - - var title = new Heading('Posts', 3) - this.add(title) - - try { - var posts = getPosts().toReversed() - - posts.forEach(function (post) { - var _post = new Post(post.username, post.image, post.text, post.date) - - this.add(_post) - }.bind(this)) - } catch (error) { - alert(error.message) - - console.error(error) - } -} - -PostList.prototype = Object.create(Compo.prototype) -PostList.prototype.constructor = PostList \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/view/CreatePost.js b/staff/aaron-barrios/unsocial/view/CreatePost.js new file mode 100644 index 00000000..aab2dc12 --- /dev/null +++ b/staff/aaron-barrios/unsocial/view/CreatePost.js @@ -0,0 +1,46 @@ +//Función que me crea posts +class CreatePost extends Compo { + constructor() { + super(document.createElement('div')) + + var title = new Heading('Create Post', 3) + this.add(title) + + var form = new Form() + + var imageLabel = new Label('Image', 'image') + var imageInput = new Input('text', 'image') + form.add(imageLabel) + form.add(imageInput) + + var textLabel = new Label('Text', 'text') + var textInput = new Input('text', 'text') + form.add(textLabel) + form.add(textInput) + + var submitButton = new Button('Create', 'submit') + form.add(submitButton) + + this.add(form) + + form.addBehavior('submit', function (event) { + event.preventDefault() + + var image = imageInput.getValue() + var text = textInput.getValue() + + try { + createPost(loggedInUser.username, image, text) + + this.remove() + + var postList = new PostList() + home.add(postList) + } catch (error) { + alert(error.message) + + console.error(error) + } + }.bind(this)) + } +} \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/view/Home.js b/staff/aaron-barrios/unsocial/view/Home.js new file mode 100644 index 00000000..bcc75943 --- /dev/null +++ b/staff/aaron-barrios/unsocial/view/Home.js @@ -0,0 +1,41 @@ +//HOME INSTANCES +class Home extends Compo { + constructor() { + super(document.createElement('section')) + + var title = new Heading('Home', 2) + this.add(title) + + var userTitle = new Heading('Hello, ' + loggedInUser.name + '!', 3) + this.add(userTitle) + + var logoutButton = new Button('Logout', 'button') + this.add(logoutButton) + + logoutButton.addBehavior('click', function (event) { + event.preventDefault() + + loggedInUser = null + + this.remove() + + page.add(login) + + }.bind(this)) + + + var addPostButton = new Button('+', 'button') + this.add(addPostButton) + + addPostButton.addBehavior('click', function (event) { + var createPost = new CreatePost() + + this.children[this.children.length - 1].remove() + + this.add(createPost) + }.bind(this)) + + var postList = new PostList() + this.add(postList) + } +} \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/view/Login.js b/staff/aaron-barrios/unsocial/view/Login.js new file mode 100644 index 00000000..3cc0daae --- /dev/null +++ b/staff/aaron-barrios/unsocial/view/Login.js @@ -0,0 +1,64 @@ +//----- LOGIN SECTION ----- +class Login extends Compo { + constructor() { + super(document.createElement('section')) + + var title = new Heading('Login', 2) + this.add(title) + + var form = new Form() + this.add(form) + + form.add(new Label('Username', 'username')) + var usernameInput = new Input('text', 'username') + form.add(usernameInput) + + form.add(new Label('Password', 'password')) + var passwordInput = new PasswordInput('password') + form.add(passwordInput) + + var submitButton = new Button('Login', 'submit') + form.add(submitButton) + + form.addBehavior('submit', function (event) { + event.preventDefault() + + var username = usernameInput.getValue() + var password = passwordInput.getValue() + + try { + loggedInUser = authenticateUser(username, password) + + form.reset() + + this.remove() + + home = new Home() + + page.add(home) + } catch (error) { + passwordInput.setValue('') + + alert(error.message) + + console.error(error) + } + }.bind(this)) + + + + var registerLink = new Link('Register') + this.add(registerLink) + + registerLink.addBehavior('click', function (event) { + event.preventDefault() + + this.remove() + + var register = new Register() + + page.add(register) + }.bind(this)) + + } +} \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/view/PostItem.js b/staff/aaron-barrios/unsocial/view/PostItem.js new file mode 100644 index 00000000..a02bd822 --- /dev/null +++ b/staff/aaron-barrios/unsocial/view/PostItem.js @@ -0,0 +1,18 @@ +//FUNCIÓN PARA EL PROPIO POST +class PostItem extends Compo { + constructor(username, image, text, date) { + super(document.createElement('div')) + + var userTitle = new Heading(username, 4) + this.add(userTitle) + + var picture = new Image(image) + this.add(picture) + + var comment = new Paragraph(text) + this.add(comment) + + var time = new Time(date) + this.add(time) + } +} \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/view/PostList.js b/staff/aaron-barrios/unsocial/view/PostList.js new file mode 100644 index 00000000..c1c19441 --- /dev/null +++ b/staff/aaron-barrios/unsocial/view/PostList.js @@ -0,0 +1,23 @@ +//Función que me carga todos los posts +class PostList extends Compo { + constructor() { + super(document.createElement('div')) + + var title = new Heading('Posts', 3) + this.add(title) + + try { + var posts = getPosts().toReversed() + + posts.forEach(function (post) { + var postItem = new PostItem(post.username, post.image, post.text, post.date) + + this.add(postItem) + }.bind(this)) + } catch (error) { + alert(error.message) + + console.error(error) + } + } +} \ No newline at end of file diff --git a/staff/aaron-barrios/unsocial/view/Register.js b/staff/aaron-barrios/unsocial/view/Register.js new file mode 100644 index 00000000..1f1cc6ab --- /dev/null +++ b/staff/aaron-barrios/unsocial/view/Register.js @@ -0,0 +1,71 @@ +//REGISTER INSTANCES +class Register extends Compo { + constructor() { + super(document.createElement('section')) + + var title = new Heading('Register', 2) + this.add(title) + + var form = new Form() + this.add(form) + + form.add(new Label('Name', 'name')) + var nameInput = new Input('text', 'name') + form.add(nameInput) + + form.add(new Label('E-mail', 'email')) + var emailInput = new Input('email', 'email') + form.add(emailInput) + + form.add(new Label('Username', 'username')) + var usernameInput = new Input('text', 'username') + form.add(usernameInput) + + form.add(new Label('Password', 'password')) + var passwordInput = new PasswordInput('password') + form.add(passwordInput) + + form.add(new Label('Repeat Password', 'password-repeat')) + var passwordRepeatInput = new PasswordInput('password-repeat') + form.add(passwordRepeatInput) + + var submitButton = new Button('Register', 'submit') + form.add(submitButton) + + form.addBehavior('submit', function (event) { + event.preventDefault() + + var name = nameInput.getValue() + var email = emailInput.getValue() + var username = usernameInput.getValue() + var password = passwordInput.getValue() + var passwordRepeat = passwordRepeatInput.getValue() + + try { + registerUser(name, email, username, password, passwordRepeat) + + form.reset() + + this.remove() + + page.add(login) + } catch (error) { + alert(error.message) + + console.error(error) + } + }.bind(this)) + + + var loginLink = new Link('Login') + this.add(loginLink) + + loginLink.addBehavior('click', function (event) { + event.preventDefault() + + this.remove() + + page.add(login) + }.bind(this)) + } +}