Format code, add comments and fix typo

This commit is contained in:
2023-09-05 10:15:00 +07:00
parent bfede29371
commit 51c84a7c82
4 changed files with 42 additions and 8 deletions
@@ -7,13 +7,23 @@ export default class CommonService {
this.firebaseService = FirebaseService; this.firebaseService = FirebaseService;
} }
/**
* Connect to Firebase Databse
*/
connectToDb() { connectToDb() {
this.firebaseService.reconnect(); 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(); this.connectToDb();
const existUser = this.firebaseService.findKeyByPropery( const existUser = this.firebaseService.findIdByProperty(
path, path,
property, property,
value, value,
@@ -23,6 +33,11 @@ export default class CommonService {
return result; 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) { async save(data, path = this.defaultPath) {
this.connectToDb(); this.connectToDb();
const saveUser = this.firebaseService.save(data, path); const saveUser = this.firebaseService.save(data, path);
@@ -30,6 +45,12 @@ export default class CommonService {
await timeOutConnect(saveUser); 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) { async getDataFromId(id, path = this.defaultPath) {
this.connectToDb(); this.connectToDb();
const result = await this.firebaseService.getDataFromId(id, path); const result = await this.firebaseService.getDataFromId(id, path);
@@ -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} path The path of database to be found
* @param {string} property The property of the value need to be found * @param {string} property The property of the value need to be found
* @param {value} value The value to compare in database * @param {value} value The value to compare in database
* @returns {Promise} Return the relsoves when find completed * @returns {Promise} Return the relsoves when find completed
*/ */
findKeyByPropery(path, property, value) { findIdByProperty(path, property, value) {
return new Promise((resolve) => { return new Promise((resolve) => {
onValue( onValue(
ref(this.db, path), 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) { getDataFromId(id, path) {
return new Promise((resolve) => { return new Promise((resolve) => {
onValue( onValue(
@@ -25,7 +25,7 @@ export default class UserService extends CommonService {
* @returns {Promise || number} Return id user when exist, otherwise will undefined * @returns {Promise || number} Return id user when exist, otherwise will undefined
*/ */
getUserIdByEmail(email) { getUserIdByEmail(email) {
return this.findKeyByProperty('email', email); return this.findIdByProperty('email', email);
} }
/** /**
@@ -30,7 +30,7 @@ export default class LoginView extends CommonLoginRegisterView {
*/ */
initErrorPopup(content) { initErrorPopup(content) {
const typePopup = CONSTANT.TYPE_POPUP.error; const typePopup = CONSTANT.TYPE_POPUP.error;
const title = 'Error Credential!'; const title = 'Error!';
const btnContent = 'Got it!'; const btnContent = 'Got it!';
this.initPopupContent(typePopup, title, content, btnContent); this.initPopupContent(typePopup, title, content, btnContent);
@@ -41,7 +41,8 @@ export default class LoginView extends CommonLoginRegisterView {
/** /**
* Add event listener for form input * 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) { addHandlerForm(getUserByEmail, createTokenUser) {
this.parentElement.addEventListener('submit', (e) => { 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) { async submitForm(getUserByEmail, createTokenUser) {
try { try {
// Load spinner // Load spinner
@@ -66,6 +72,7 @@ export default class LoginView extends CommonLoginRegisterView {
if (userInput.password === user.password) { if (userInput.password === user.password) {
await createTokenUser(user); await createTokenUser(user);
window.location.replace('/'); window.location.replace('/');
return; return;
} }
} }