aboutsummaryrefslogtreecommitdiff
path: root/public/javascripts/favorite.js
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2016-12-14 12:57:59 +0100
committerNoah Loomans <noahloomans@gmail.com>2016-12-14 12:57:59 +0100
commit7c4d1ca2d771ee527201ccd6447e70da97ad33be (patch)
treee151354bb2182d9788c0e2e8e85d560ccb862f3f /public/javascripts/favorite.js
parenta016343e0a88e1bb46a2deed0b27dda7b6bc7e44 (diff)
parentb7027583efff12e1bd5acdbb4077ab36c5226839 (diff)
Merge branch 'refactor' into beta
Diffstat (limited to 'public/javascripts/favorite.js')
-rw-r--r--public/javascripts/favorite.js73
1 files changed, 73 insertions, 0 deletions
diff --git a/public/javascripts/favorite.js b/public/javascripts/favorite.js
new file mode 100644
index 0000000..f10d954
--- /dev/null
+++ b/public/javascripts/favorite.js
@@ -0,0 +1,73 @@
+/* global USERS */
+
+const EventEmitter = require('events')
+
+const self = new EventEmitter()
+
+self._nodes = {
+ toggle: document.querySelector('.fav')
+}
+
+self.get = function () {
+ const localStorageUser = JSON.parse(window.localStorage.getItem('fav'))
+ if (localStorageUser == null) return
+
+ const correctedUser = USERS.filter(user =>
+ user.type === localStorageUser.type &&
+ user.value === localStorageUser.value)[0]
+ return correctedUser
+}
+
+self.set = function (user) {
+ window.localStorage.setItem('fav', JSON.stringify(user))
+ self._nodes.innerHTML = '&#xE838;'
+}
+
+self.delete = function () {
+ window.localStorage.removeItem('fav')
+}
+
+self.updateDom = function (isFavorite) {
+ if (isFavorite) {
+ self._nodes.toggle.innerHTML = '&#xE838;'
+ } else {
+ self._nodes.toggle.innerHTML = '&#xE83A'
+ }
+}
+
+self.update = function (selectedUser) {
+ const currentUser = self.get()
+
+ if (currentUser == null) {
+ self.updateDom(false)
+ return
+ }
+
+ const isEqual = currentUser.type === selectedUser.type &&
+ currentUser.index === selectedUser.index
+
+ self.updateDom(isEqual)
+}
+
+self.toggle = function (selectedUser) {
+ const currentUser = self.get()
+ const isEqual = currentUser != null &&
+ currentUser.type === selectedUser.type &&
+ currentUser.index === selectedUser.index
+
+ if (isEqual) {
+ self.delete()
+ self.updateDom(false)
+ } else {
+ self.set(selectedUser)
+ self.updateDom(true)
+ }
+}
+
+self._handleClick = function () {
+ self.emit('click')
+}
+
+self._nodes.toggle.addEventListener('click', self._handleClick)
+
+module.exports = self