From 085716340505570ae8350a1c70fdae49dba74549 Mon Sep 17 00:00:00 2001 From: jennfa63626 Date: Sat, 30 May 2020 16:09:08 +0200 Subject: [PATCH] init --- public/assets/js/index.js | 117 ++++++++++++++++++++++++++++++++++++++ public/index.html | 92 ++++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 public/assets/js/index.js create mode 100644 public/index.html diff --git a/public/assets/js/index.js b/public/assets/js/index.js new file mode 100644 index 0000000..2c3d07c --- /dev/null +++ b/public/assets/js/index.js @@ -0,0 +1,117 @@ +$('#contactForm').submit(function(event) { + // prevent page reload + event.preventDefault(); + + var name = $('#name').val(); + var email = $('#email').val(); + var mobile = $('#mobile').val(); + + // Save the contact to the database with Hoodie + hoodie.store.add({ + name: name, + mobile: mobile, + email: email + }); + + $('#contactForm')[0].reset(); +}); + +$('#signup').click(function (event) { + signUp(); +}); + +$('#signin').click(function (event) { + signIn(); +}); + +$('#signout').click(function (event) { + signOut(); +}); + +if (hoodie.account.isSignedIn()) { + showAuthenticated(); +} else { + showAnonymous(); +} + +// when the site loads in the browser, +// we load all previously saved contacts from hoodie +loadContacts(); + +//when a new entry is added to the database, run the corresponding function +hoodie.store.on('add', addNewContactToList); + +function loadContacts() { + hoodie.store.findAll().then(function(contacts) { + var tbody = ''; + $.each(contacts, function (i, contact) { + var row = '' + contact.name + '' + contact.mobile + '' + contact.email + ''; + tbody += row; + }); + + $("#contactList tbody").html('').html(tbody); + }); +} + +function addNewContactToList(contact) { + var newContact = '' + contact.name + '' + contact.mobile + '' + contact.email + '' + $("#contactList tbody").append(newContact); +} + +function signUp() { + var username = prompt('username'); + var password = prompt('password'); + hoodie.account.signUp({ + username: username, + password: password + }) + .then(function() { + return hoodie.account.signIn({ + username: username, + password: password + }); + }) + .then(function() { + showAuthenticated(); + }) + .catch(function(errror) { + alert('Ooops, something went wrong: ' + error.message); + }) +} + +function signIn(){ + var username = prompt('username'); + var password = prompt('password'); + + hoodie.account.signIn({ + username: username, + password: password + }) + .then(function() { + showAuthenticated(); + }) + .catch(function(error) { + alert('ooops: ' + error.message); + }); +} + +function signOut(){ + hoodie.account.signOut() + .then(function() { + showAnonymous(); + }) + .catch(function(error) { + alert('ooops: ' + error.message); + }); +} + +function showAuthenticated(){ + $('#username').text('signed in as ' + hoodie.account.username); + $('#authenticated').show(); + $('#anonymous').hide(); +} + +function showAnonymous(){ + $('#authenticated').hide(); + $('#anonymous').show(); +} \ No newline at end of file diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..3bd9b14 --- /dev/null +++ b/public/index.html @@ -0,0 +1,92 @@ + + + + + + My Hoodie App + + + + + + +
+ +
+
+

Add new contact

+
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
+
+
+
+
+ +
+
+

Contact List

+
+ + + + + + + + + + + + +
NameMobileEmail
+
+
+ + + + + + + + + + \ No newline at end of file