Merge pull request #12 from Nez27/feat/optimized-code

Optimized code project.
This commit is contained in:
Loi Phan
2023-09-05 09:31:14 +07:00
committed by GitHub
9 changed files with 140 additions and 109 deletions
@@ -13,7 +13,7 @@ export default class RegisterController {
} }
init() { init() {
if (this.registerView.checkRegisterFormElExist()) { if (this.registerView.isRegisterPage()) {
this.registerView.addHandlerForm( this.registerView.addHandlerForm(
this.hanlderCheckUserExist.bind(this), this.hanlderCheckUserExist.bind(this),
this.handlerSaveUser.bind(this), this.handlerSaveUser.bind(this),
+1
View File
@@ -3,5 +3,6 @@ import App from './app';
// Sure that scripts called after DOM loaded // Sure that scripts called after DOM loaded
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
const myApp = new App(); const myApp = new App();
myApp.start(); myApp.start();
}); });
@@ -1,8 +1,41 @@
/* 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) {
this.connectToDb();
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) {
this.connectToDb();
const data = await this.firebaseService.getDataFromId(id, path);
if (data) return data;
return null;
} }
} }
@@ -1,25 +1,21 @@
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(
user, this.save(user, pathData);
this.path + User.createIdUser(),
);
await timeOutConnect(saveUser);
} }
/** /**
@@ -27,16 +23,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;
} }
/** /**
@@ -46,9 +34,11 @@ export default class UserService extends CommonService {
*/ */
async checkUserExist(email) { async checkUserExist(email) {
const userExist = await this.getUserIdByEmail(email); const userExist = await this.getUserIdByEmail(email);
if (userExist) { if (userExist) {
return true; return true;
} }
return false; return false;
} }
@@ -59,10 +49,13 @@ 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;
} }
} }
@@ -46,7 +46,7 @@ export default class CommonLoginRegisterView extends CommonView {
// Reassign again to check error message element haved on page or not // Reassign again to check error message element haved on page or not
this.errorMessageEl = document.querySelector('.form__error-message'); this.errorMessageEl = document.querySelector('.form__error-message');
// If have error message on page, remove it // If have error message on page, remove it with style error input password
if (this.errorMessageEl) { if (this.errorMessageEl) {
this.errorMessageEl.remove(); this.errorMessageEl.remove();
this.toogleErrorStyleInputPass(); this.toogleErrorStyleInputPass();
@@ -1,12 +1,12 @@
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() {
super(); super();
this.registerForm = document.getElementById('registerForm'); this.registerPage = document.getElementById('registerPage');
} }
/** /**
@@ -24,8 +24,10 @@ export default class RegisterView extends CommonLoginRegisterView {
if (this.validateForm(account)) { if (this.validateForm(account)) {
const user = new User(account); const user = new User(account);
return user; return user;
} }
return null; return null;
} }
@@ -100,7 +102,7 @@ export default class RegisterView extends CommonLoginRegisterView {
this.toogleLoaderSpinner(); this.toogleLoaderSpinner();
} }
checkRegisterFormElExist() { isRegisterPage() {
return this.registerForm !== null; return this.registerPage !== null;
} }
} }
+37 -32
View File
@@ -7,38 +7,43 @@
<title>Login - Money Lover</title> <title>Login - Money Lover</title>
</head> </head>
<body> <body>
<!-- <div class="loader"></div> --> <main id="loginPage">
<div class="background"> <div class="background">
<div class="wrapper"> <div class="wrapper">
<img class="logo-web" src="../assets/images/logo.svg" alt="Logo web" /> <img
<!-- Log in form --> class="logo-web"
<form action="#" class="form" method="post"> src="../assets/images/logo.svg"
<div class="form__container"> alt="Logo web"
<h1 class="form__title">Log In</h1> />
<p class="form__description">Using Money Lover account</p> <!-- Log in form -->
<input <form action="#" class="form" method="post">
type="email" <div class="form__container">
class="form__input" <h1 class="form__title">Log In</h1>
placeholder="Email" <p class="form__description">Using Money Lover account</p>
name="email" <input
required type="email"
/> class="form__input"
<input placeholder="Email"
type="password" name="email"
name="password" required
class="form__input" />
placeholder="Password" <input
required type="password"
/> name="password"
<button type="submit" class="form__submit-btn">Login</button> class="form__input"
<p class="form__redirect-signup-text"> placeholder="Password"
Don't have an account? required
<a href="/register" class="form__link">Register</a> />
</p> <button type="submit" class="form__submit-btn">Login</button>
</div> <p class="form__redirect-signup-text">
</form> Don't have an account?
<!-- End form --> <a href="/register" class="form__link">Register</a>
</p>
</div>
</form>
<!-- End form -->
</div>
</div> </div>
</div> </main>
</body> </body>
</html> </html>
+44 -47
View File
@@ -7,54 +7,51 @@
<title>Register - Money Lover</title> <title>Register - Money Lover</title>
</head> </head>
<body> <body>
<!-- <div class="loader hidden"></div> --> <main id="registerPage">
<!-- <div class="overlay"></div> <div class="background">
<div class="modal-box success-form"> <div class="wrapper">
<div class="mark check"></div> <img
<h2 class="modal-box__title">Register success!</h2> class="logo-web"
<p class="modal-box__message">Please login to continue.</p> src="../assets/images/logo.svg"
alt="Logo web"
<button class="modal-box__redirect-btn">OK!</button> />
</div> --> <!-- Register form -->
<div class="background"> <form action="#" class="form" method="post" id="registerForm">
<div class="wrapper"> <div class="form__container">
<img class="logo-web" src="../assets/images/logo.svg" alt="Logo web" /> <h1 class="form__title">Register</h1>
<!-- Register form --> <p class="form__description">Using Money Lover account</p>
<form action="#" class="form" method="post" id="registerForm"> <input
<div class="form__container"> type="email"
<h1 class="form__title">Register</h1> class="form__input"
<p class="form__description">Using Money Lover account</p> placeholder="Email"
<input name="email"
type="email" required
class="form__input" />
placeholder="Email" <input
name="email" type="password"
required name="password"
/> class="form__input"
<input placeholder="Password"
type="password" required
name="password" />
class="form__input" <input
placeholder="Password" type="password"
required name="password_confirm"
/> class="form__input"
<input placeholder="Confirm Password"
type="password" required
name="password_confirm" />
class="form__input" <button type="submit" class="form__submit-btn">Register</button>
placeholder="Confirm Password" <p class="form__redirect-signup-text">
required Have you an account?
/> <a href="/login" class="form__link">Sign in</a>
<button type="submit" class="form__submit-btn">Register</button> </p>
<p class="form__redirect-signup-text"> </div>
Have you an account? </form>
<a href="/login" class="form__link">Sign in</a> <!-- End form -->
</p> </div>
</div>
</form>
<!-- End form -->
</div> </div>
</div> </main>
<script type="module" src="../js/main.js"></script> <script type="module" src="../js/main.js"></script>
</body> </body>
</html> </html>