Merge pull request #11 from Nez27/feat/add-method-and-optimized-code

Add method in firebaseService, userService and optimized code in registerController
This commit is contained in:
Loi Phan
2023-08-31 17:17:51 +07:00
committed by GitHub
5 changed files with 110 additions and 52 deletions
@@ -1,46 +1,24 @@
export default class RegisterController { export default class RegisterController {
constructor(service, view) { constructor(service, view) {
this.view = view; this.registerView = view.registerView;
this.service = service; this.service = service;
} }
async controlRegister() { hanlderCheckUserExist(email) {
try { return this.service.userService.checkUserExist(email);
// Load spinner
this.view.registerView.toogleLoaderSpinner();
// Get data from form
const user = this.view.registerView.getDataFromForm();
// Save user
if (user) {
// Check user exist
const userExist = await this.service.userService.checkExistUserByEmail(
user.email,
);
if (userExist) {
throw Error('User is exists! Please try another email!');
} else {
await this.service.userService.saveUser(user);
// Show popup success
this.view.registerView.initRegisterSuccessPopup();
this.view.registerView.tooglePopupForm();
}
}
} catch (error) {
// Show popup error
this.view.registerView.initErrorPopup(error);
this.view.registerView.tooglePopupForm();
} }
// Close spinner handlerSaveUser(user) {
this.view.registerView.toogleLoaderSpinner(); return this.service.userService.saveUser(user);
} }
init() { init() {
if (this.view.registerView.registerForm !== null) { if (this.registerView.checkRegisterFormElExist()) {
this.view.registerView.addHandlerForm(this.controlRegister.bind(this)); this.registerView.addHandlerForm(
this.view.registerView.addHandlerInputFormChange(); this.hanlderCheckUserExist.bind(this),
this.handlerSaveUser.bind(this),
);
this.registerView.addHandlerInputFormChange();
} }
} }
} }
@@ -54,14 +54,14 @@ class FirebaseService {
onValue( onValue(
ref(this.db, path), ref(this.db, path),
(snapshot) => { (snapshot) => {
let result = false; let result;
// snapshot is a type of data by Firebase define // snapshot is a type of data by Firebase define
snapshot.forEach((childSnapshot) => { snapshot.forEach((childSnapshot) => {
const data = childSnapshot.val(); const data = childSnapshot.val();
if (data[property] === value) { if (data[property] === value) {
result = true; result = childSnapshot.key;
} }
}); });
resolve(result); resolve(result);
@@ -72,6 +72,20 @@ class FirebaseService {
); );
}); });
} }
getDataFromId(id, path) {
return new Promise((resolve) => {
onValue(
ref(this.db, path + id),
(snapshot) => {
resolve(snapshot.val());
},
{
onlyOnce: true,
},
);
});
}
} }
export default new FirebaseService(); export default new FirebaseService();
@@ -23,11 +23,11 @@ export default class UserService extends CommonService {
} }
/** /**
* Check user exist in database by email * Get user id by email
* @param {string} email Email need to be check * @param {string} email Email need to be check
* @returns {boolean} Return true if email exist and otherwise is false * @returns {Promise || number} Return id user when exist, otherwise will undefined
*/ */
async checkExistUserByEmail(email) { async getUserIdByEmail(email) {
this.connectToDb(); this.connectToDb();
const existUser = FirebaseService.findKeyByPropery( const existUser = FirebaseService.findKeyByPropery(
this.path, this.path,
@@ -38,4 +38,31 @@ export default class UserService extends CommonService {
return result; return result;
} }
/**
* Check user exist in database
* @param {string} email Email to find user
* @returns {boolean} Return true if find, otherwise return false
*/
async checkUserExist(email) {
const userExist = await this.getUserIdByEmail(email);
if (userExist) {
return true;
}
return false;
}
/**
* Get user data from email
* @param {string} email Email user
* @returns {Object || null} Return new User Object if find, otherwise return null.
*/
async getUserByEmail(email) {
const id = await this.getUserIdByEmail(email);
if (id) {
const user = await FirebaseService.getDataFromId(id, this.path);
return new User(user);
}
return null;
}
} }
@@ -31,18 +31,6 @@ export default class CommonLoginRegisterView extends CommonView {
return false; return false;
} }
/**
* Add event listener for form input
* @param {Function} handler The function need to be set event
*/
addHandlerForm(handler) {
this.parentElement.addEventListener('submit', (e) => {
e.preventDefault();
this.clearErrorMessage();
handler();
});
}
/** /**
* Show or hide style error input password * Show or hide style error input password
*/ */
@@ -32,13 +32,16 @@ export default class RegisterView extends CommonLoginRegisterView {
/** /**
* Implement register success popup in site * Implement register success popup in site
*/ */
initRegisterSuccessPopup() { showRegisterSuccessPopup() {
const typePopup = TYPE_POPUP.success; const typePopup = TYPE_POPUP.success;
const title = 'Register Commpleted'; const title = 'Register Commpleted';
const content = 'Please login to continue!'; const content = 'Please login to continue!';
const btnContent = 'OK'; const btnContent = 'OK';
this.initPopupContent(typePopup, title, content, btnContent); this.initPopupContent(typePopup, title, content, btnContent);
// Show popup
this.tooglePopupForm();
} }
/** /**
@@ -51,5 +54,53 @@ export default class RegisterView extends CommonLoginRegisterView {
const btnContent = 'Got it!'; const btnContent = 'Got it!';
this.initPopupContent(typePopup, title, content, btnContent); this.initPopupContent(typePopup, title, content, btnContent);
// Show popup
this.tooglePopupForm();
}
/**
* Add event listener for form input
* @param {Function} handler The function need to be set event
*/
addHandlerForm(checkExistUser, saveUser) {
this.parentElement.addEventListener('submit', (e) => {
e.preventDefault();
this.clearErrorMessage();
this.submitForm(checkExistUser, saveUser);
});
}
async submitForm(checkExistUser, saveUser) {
try {
// Load spinner
this.toogleLoaderSpinner();
// Get data from form
const user = this.getDataFromForm();
// Save user
if (user) {
// Check user exist
const userExist = await checkExistUser(user.email);
if (userExist) {
throw Error('User is exists! Please try another email!');
} else {
await saveUser(user);
// Show popup success
this.showRegisterSuccessPopup();
}
}
} catch (error) {
// Show popup error
this.initErrorPopup(error);
}
// Close spinner
this.toogleLoaderSpinner();
}
checkRegisterFormElExist() {
return this.registerForm !== null;
} }
} }