mirror of
https://github.com/Nezumi-2711/javascript-training.git
synced 2026-09-22 20:01:31 +00:00
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:
@@ -1,46 +1,24 @@
|
||||
export default class RegisterController {
|
||||
constructor(service, view) {
|
||||
this.view = view;
|
||||
this.registerView = view.registerView;
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
async controlRegister() {
|
||||
try {
|
||||
// Load spinner
|
||||
this.view.registerView.toogleLoaderSpinner();
|
||||
hanlderCheckUserExist(email) {
|
||||
return this.service.userService.checkUserExist(email);
|
||||
}
|
||||
|
||||
// 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
|
||||
this.view.registerView.toogleLoaderSpinner();
|
||||
handlerSaveUser(user) {
|
||||
return this.service.userService.saveUser(user);
|
||||
}
|
||||
|
||||
init() {
|
||||
if (this.view.registerView.registerForm !== null) {
|
||||
this.view.registerView.addHandlerForm(this.controlRegister.bind(this));
|
||||
this.view.registerView.addHandlerInputFormChange();
|
||||
if (this.registerView.checkRegisterFormElExist()) {
|
||||
this.registerView.addHandlerForm(
|
||||
this.hanlderCheckUserExist.bind(this),
|
||||
this.handlerSaveUser.bind(this),
|
||||
);
|
||||
this.registerView.addHandlerInputFormChange();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,14 +54,14 @@ class FirebaseService {
|
||||
onValue(
|
||||
ref(this.db, path),
|
||||
(snapshot) => {
|
||||
let result = false;
|
||||
let result;
|
||||
|
||||
// snapshot is a type of data by Firebase define
|
||||
snapshot.forEach((childSnapshot) => {
|
||||
const data = childSnapshot.val();
|
||||
|
||||
if (data[property] === value) {
|
||||
result = true;
|
||||
result = childSnapshot.key;
|
||||
}
|
||||
});
|
||||
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();
|
||||
|
||||
@@ -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
|
||||
* @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();
|
||||
const existUser = FirebaseService.findKeyByPropery(
|
||||
this.path,
|
||||
@@ -38,4 +38,31 @@ export default class UserService extends CommonService {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
@@ -32,13 +32,16 @@ export default class RegisterView extends CommonLoginRegisterView {
|
||||
/**
|
||||
* Implement register success popup in site
|
||||
*/
|
||||
initRegisterSuccessPopup() {
|
||||
showRegisterSuccessPopup() {
|
||||
const typePopup = TYPE_POPUP.success;
|
||||
const title = 'Register Commpleted';
|
||||
const content = 'Please login to continue!';
|
||||
const btnContent = 'OK';
|
||||
|
||||
this.initPopupContent(typePopup, title, content, btnContent);
|
||||
|
||||
// Show popup
|
||||
this.tooglePopupForm();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,5 +54,53 @@ export default class RegisterView extends CommonLoginRegisterView {
|
||||
const btnContent = 'Got it!';
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user