Optimized code

This commit is contained in:
2023-09-04 15:38:58 +07:00
parent a62b97981d
commit 99034fb95f
5 changed files with 41 additions and 26 deletions
@@ -1,8 +1,35 @@
/* eslint-disable class-methods-use-this */ import { timeOutConnect } from '../helpers/helpers';
import FirebaseService from './firebaseService'; import FirebaseService from './firebaseService';
export default class CommonService { export default class CommonService {
constructor() {
this.defaultPath = '/';
this.firebaseService = FirebaseService;
}
connectToDb() { connectToDb() {
FirebaseService.reconnect(); this.firebaseService.reconnect();
}
async findKeyByProperty(property, value, path = this.defaultPath) {
const existUser = this.firebaseService.findKeyByPropery(
path,
property,
value,
);
const result = await timeOutConnect(existUser);
return result;
}
async save(data, path = this.path) {
this.connectToDb();
const saveUser = this.firebaseService.save(data, path);
await timeOutConnect(saveUser);
}
async getDataFromId(id, path = this.path) {
const data = await this.firebaseService.getDataFromId(id, path);
if (data) return data;
return null;
} }
} }
@@ -1,25 +1,20 @@
import { timeOutConnect } from '../helpers/helpers'; import User from '../models/user';
import FirebaseService from './firebaseService';
import User from '../models/userModel';
import CommonService from './commonService'; import CommonService from './commonService';
export default class UserService extends CommonService { export default class UserService extends CommonService {
constructor() { constructor() {
super(); super();
this.path = 'users/';
this.defaultPath = 'users/';
} }
/** /**
* Save user into database * Save user into database
* @param {Object} user The user object need to be saved into databae * @param {Object} user The user object need to be saved into databae
*/ */
async saveUser(user) { saveUser(user) {
this.connectToDb(); const pathData = this.defaultPath + User.createIdUser();
const saveUser = FirebaseService.save( this.save(user, pathData);
user,
this.path + User.createIdUser(),
);
await timeOutConnect(saveUser);
} }
/** /**
@@ -27,16 +22,8 @@ export default class UserService extends CommonService {
* @param {string} email Email need to be check * @param {string} email Email need to be check
* @returns {Promise || number} Return id user when exist, otherwise will undefined * @returns {Promise || number} Return id user when exist, otherwise will undefined
*/ */
async getUserIdByEmail(email) { getUserIdByEmail(email) {
this.connectToDb(); return this.findKeyByProperty('email', email);
const existUser = FirebaseService.findKeyByPropery(
this.path,
'email',
email,
);
const result = await timeOutConnect(existUser);
return result;
} }
/** /**
@@ -60,7 +47,7 @@ export default class UserService extends CommonService {
async getUserByEmail(email) { async getUserByEmail(email) {
const id = await this.getUserIdByEmail(email); const id = await this.getUserIdByEmail(email);
if (id) { if (id) {
const user = await FirebaseService.getDataFromId(id, this.path); const user = await this.getDataFromId(id);
return new User(user); return new User(user);
} }
return null; return null;
@@ -25,9 +25,11 @@ export default class CommonLoginRegisterView extends CommonView {
return true; return true;
} }
this.showError(CONSTANT.MESSAGE.PASSWORD_NOT_STRONG); this.showError(CONSTANT.MESSAGE.PASSWORD_NOT_STRONG);
this.toogleErrorStyleInputPass();
return false; return false;
} }
this.showError(CONSTANT.MESSAGE.PASSWORD_NOT_MATCH); this.showError(CONSTANT.MESSAGE.PASSWORD_NOT_MATCH);
this.toogleErrorStyleInputPass();
return false; return false;
} }
@@ -49,7 +51,6 @@ export default class CommonLoginRegisterView extends CommonView {
// If have error message on page, remove it // If have error message on page, remove it
if (this.errorMessageEl) { if (this.errorMessageEl) {
this.errorMessageEl.remove(); this.errorMessageEl.remove();
this.toogleErrorStyleInputPass();
} }
} }
@@ -1,6 +1,6 @@
import { TYPE_POPUP } from '../constants/constant'; import { TYPE_POPUP } from '../constants/constant';
import CommonLoginRegisterView from './commonLoginRegisterView'; import CommonLoginRegisterView from './commonLoginRegisterView';
import User from '../models/userModel'; import User from '../models/user';
export default class RegisterView extends CommonLoginRegisterView { export default class RegisterView extends CommonLoginRegisterView {
constructor() { constructor() {