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';
export default class CommonService {
constructor() {
this.defaultPath = '/';
this.firebaseService = FirebaseService;
}
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 FirebaseService from './firebaseService';
import User from '../models/userModel';
import User from '../models/user';
import CommonService from './commonService';
export default class UserService extends CommonService {
constructor() {
super();
this.path = 'users/';
this.defaultPath = 'users/';
}
/**
* Save user into database
* @param {Object} user The user object need to be saved into databae
*/
async saveUser(user) {
this.connectToDb();
const saveUser = FirebaseService.save(
user,
this.path + User.createIdUser(),
);
await timeOutConnect(saveUser);
saveUser(user) {
const pathData = this.defaultPath + User.createIdUser();
this.save(user, pathData);
}
/**
@@ -27,16 +22,8 @@ export default class UserService extends CommonService {
* @param {string} email Email need to be check
* @returns {Promise || number} Return id user when exist, otherwise will undefined
*/
async getUserIdByEmail(email) {
this.connectToDb();
const existUser = FirebaseService.findKeyByPropery(
this.path,
'email',
email,
);
const result = await timeOutConnect(existUser);
return result;
getUserIdByEmail(email) {
return this.findKeyByProperty('email', email);
}
/**
@@ -60,7 +47,7 @@ export default class UserService extends CommonService {
async getUserByEmail(email) {
const id = await this.getUserIdByEmail(email);
if (id) {
const user = await FirebaseService.getDataFromId(id, this.path);
const user = await this.getDataFromId(id);
return new User(user);
}
return null;
@@ -25,9 +25,11 @@ export default class CommonLoginRegisterView extends CommonView {
return true;
}
this.showError(CONSTANT.MESSAGE.PASSWORD_NOT_STRONG);
this.toogleErrorStyleInputPass();
return false;
}
this.showError(CONSTANT.MESSAGE.PASSWORD_NOT_MATCH);
this.toogleErrorStyleInputPass();
return false;
}
@@ -49,7 +51,6 @@ export default class CommonLoginRegisterView extends CommonView {
// If have error message on page, remove it
if (this.errorMessageEl) {
this.errorMessageEl.remove();
this.toogleErrorStyleInputPass();
}
}
@@ -1,6 +1,6 @@
import { TYPE_POPUP } from '../constants/constant';
import CommonLoginRegisterView from './commonLoginRegisterView';
import User from '../models/userModel';
import User from '../models/user';
export default class RegisterView extends CommonLoginRegisterView {
constructor() {