diff --git a/javascript-practice/src/js/services/commonService.js b/javascript-practice/src/js/services/commonService.js index 934e78c..ed37218 100644 --- a/javascript-practice/src/js/services/commonService.js +++ b/javascript-practice/src/js/services/commonService.js @@ -7,13 +7,23 @@ export default class CommonService { this.firebaseService = FirebaseService; } + /** + * Connect to Firebase Databse + */ connectToDb() { this.firebaseService.reconnect(); } - async findKeyByProperty(property, value, path = this.defaultPath) { + /** + * Find the id of data on database + * @param {string} property The property want to get value + * @param {string} value The value of property + * @param {path} path The path of datbase + * @returns {string} The id of data object + */ + async findIdByProperty(property, value, path = this.defaultPath) { this.connectToDb(); - const existUser = this.firebaseService.findKeyByPropery( + const existUser = this.firebaseService.findIdByProperty( path, property, value, @@ -23,6 +33,11 @@ export default class CommonService { return result; } + /** + * Save data on database + * @param {*} data The data wants to save on database + * @param {string} path The path of database + */ async save(data, path = this.defaultPath) { this.connectToDb(); const saveUser = this.firebaseService.save(data, path); @@ -30,11 +45,17 @@ export default class CommonService { await timeOutConnect(saveUser); } + /** + * + * @param {string} id The string of data object + * @param {string} path The path of database + * @returns {Object || null} Return the object if has, otherwise return null + */ async getDataFromId(id, path = this.defaultPath) { this.connectToDb(); const result = await this.firebaseService.getDataFromId(id, path); const data = await timeOutConnect(result); - + if (data) return data; return null; diff --git a/javascript-practice/src/js/services/firebaseService.js b/javascript-practice/src/js/services/firebaseService.js index d65b544..9cb2b09 100644 --- a/javascript-practice/src/js/services/firebaseService.js +++ b/javascript-practice/src/js/services/firebaseService.js @@ -43,13 +43,13 @@ class FirebaseService { } /** - * Find the key of value by property in database + * Find id of value by property in database * @param {string} path The path of database to be found * @param {string} property The property of the value need to be found * @param {value} value The value to compare in database * @returns {Promise} Return the relsoves when find completed */ - findKeyByPropery(path, property, value) { + findIdByProperty(path, property, value) { return new Promise((resolve) => { onValue( ref(this.db, path), @@ -73,6 +73,12 @@ class FirebaseService { }); } + /** + * Get data object from Id + * @param {string} id The id of data object + * @param {string} path The path of data save in database + * @returns {Promise} Return new Promise + */ getDataFromId(id, path) { return new Promise((resolve) => { onValue( diff --git a/javascript-practice/src/js/services/userService.js b/javascript-practice/src/js/services/userService.js index fa4147f..8ebd00e 100644 --- a/javascript-practice/src/js/services/userService.js +++ b/javascript-practice/src/js/services/userService.js @@ -25,7 +25,7 @@ export default class UserService extends CommonService { * @returns {Promise || number} Return id user when exist, otherwise will undefined */ getUserIdByEmail(email) { - return this.findKeyByProperty('email', email); + return this.findIdByProperty('email', email); } /** diff --git a/javascript-practice/src/js/views/loginView.js b/javascript-practice/src/js/views/loginView.js index 2ac412e..ab97fce 100644 --- a/javascript-practice/src/js/views/loginView.js +++ b/javascript-practice/src/js/views/loginView.js @@ -30,7 +30,7 @@ export default class LoginView extends CommonLoginRegisterView { */ initErrorPopup(content) { const typePopup = CONSTANT.TYPE_POPUP.error; - const title = 'Error Credential!'; + const title = 'Error!'; const btnContent = 'Got it!'; this.initPopupContent(typePopup, title, content, btnContent); @@ -41,7 +41,8 @@ export default class LoginView extends CommonLoginRegisterView { /** * Add event listener for form input - * @param {Function} handler The function need to be set event + * @param {Function} getUserByEmail The function need to be set event + * @param {Function} createTokenUser The function need to be set event */ addHandlerForm(getUserByEmail, createTokenUser) { this.parentElement.addEventListener('submit', (e) => { @@ -51,6 +52,11 @@ export default class LoginView extends CommonLoginRegisterView { }); } + /** + * The action when submit form + * @param {Function} getUserByEmail The function need to be set event + * @param {Function} createTokenUser The function need to be set event + */ async submitForm(getUserByEmail, createTokenUser) { try { // Load spinner @@ -66,6 +72,7 @@ export default class LoginView extends CommonLoginRegisterView { if (userInput.password === user.password) { await createTokenUser(user); window.location.replace('/'); + return; } }