Merge code from feat/javascript-practice branch

This commit is contained in:
2023-09-13 14:16:07 +07:00
11 changed files with 29 additions and 32 deletions
@@ -8,8 +8,8 @@ export default class HomeController {
return this.service.userService.getInfoUserLogin();
}
handlerCheckWalletExist(idUser) {
return this.service.walletService.checkWalletExist(idUser);
handlerCheckWalletValid(idUser) {
return this.service.walletService.isValidWallet(idUser);
}
handlerGetWalletUser(idUser) {
@@ -30,7 +30,7 @@ export default class HomeController {
this.homeView.loadPage(
this.handlerGetInfoUserLogin.bind(this),
this.handlerCheckWalletExist.bind(this),
this.handlerCheckWalletValid.bind(this),
this.handlerGetWalletUser.bind(this),
);
@@ -4,8 +4,8 @@ export default class RegisterController {
this.service = service;
}
handlerCheckUserExist(email) {
return this.service.userService.checkUserExist(email);
handlerCheckUserValid(email) {
return this.service.userService.isValidUser(email);
}
handlerSaveUser(user) {
@@ -15,7 +15,7 @@ export default class RegisterController {
init() {
if (this.registerView) {
this.registerView.addHandlerForm(
this.handlerCheckUserExist.bind(this),
this.handlerCheckUserValid.bind(this),
this.handlerSaveUser.bind(this),
);
this.registerView.addHandlerInputFormChange();
@@ -7,7 +7,7 @@ import FirebaseService from '../services/firebaseService';
* @param {string} password Password input
* @returns {boolean} Return true if validate password success, otherwise return false
*/
export const validatePassword = (password) => {
export const isValidatePassword = (password) => {
return REGEX.PASSWORD.test(password);
};
@@ -20,12 +20,8 @@ export default class CommonService {
async getDataFromProp(property, value, path = this.defaultPath) {
this.connectToDb();
const dataExists = this.firebaseService.getDataFromProp(
path,
property,
value,
);
const result = await timeOutConnect(dataExists);
const data = this.firebaseService.getDataFromProp(path, property, value);
const result = await timeOutConnect(data);
if (result.id && result.data) {
return convertDataObjectToModel(result);
@@ -22,7 +22,7 @@ class FirebaseService {
* Save data in database
* @param {Object} data The object need to save into database
* @param {string} path The path of database need to be save
* @returns {Promise} Return the relsoves when write to database completed
* @returns {Promise} Return the resolves when write to database completed
*/
save(data, path) {
return set(ref(this.db, path), data);
@@ -23,7 +23,7 @@ export default class UserService extends CommonService {
* @param {string} email Email to find user
* @returns {boolean} Return true if find, otherwise return false
*/
async checkUserExist(email) {
async isValidUser(email) {
const userExist = await this.getUserByEmail(email);
if (userExist) {
@@ -21,10 +21,10 @@ export default class WalletService extends CommonService {
* @param {string} idUser The id user to find user's wallet
* @returns {boolean} Return true if find, otherwise return false
*/
async checkWalletExist(idUser) {
const walletExist = await this.getWalletByIdUser(idUser);
async isValidWallet(idUser) {
const wallet = await this.getWalletByIdUser(idUser);
if (walletExist) {
if (wallet) {
return true;
}
@@ -1,6 +1,6 @@
import CommonView from './commonView';
import * as MESSAGE from '../constants/message';
import { validatePassword } from '../helpers/helpers';
import { isValidatePassword } from '../helpers/helpers';
export default class CommonLoginRegisterView extends CommonView {
constructor() {
@@ -19,9 +19,9 @@ export default class CommonLoginRegisterView extends CommonView {
* @param {Object} account The account object with email, password, passwordConfirm field
* @returns {boolean} Return true if validate success and return false if validate not success
*/
validateForm(account) {
isValidateAccount(account) {
if (account.password === account.passwordConfirm) {
if (validatePassword(account.passwordConfirm)) {
if (isValidatePassword(account.passwordConfirm)) {
return true;
}
this.showError(MESSAGE.PASSWORD_NOT_STRONG);
@@ -56,13 +56,13 @@ export default class CommonView {
*/
initToastContent(typeToast, title, content, btnContent) {
// Remove old typeToast class if haved
Object.keys(TYPE_TOAST).forEach((value) => {
CommonView.removeClassElement(value, this.toast);
Object.keys(TYPE_TOAST).forEach((key) => {
CommonView.removeClassElement(TYPE_TOAST[key], this.toast);
});
// Remove old icon toast if haved
Object.keys(MARK_ICON).forEach((value) => {
CommonView.removeClassElement(value, this.toastIcon);
Object.keys(MARK_ICON).forEach((key) => {
CommonView.removeClassElement(MARK_ICON[key], this.toastIcon);
});
// Init content toast
+7 -6
View File
@@ -13,6 +13,7 @@ export default class HomeView extends CommonView {
this.addTransactionBtn = document.getElementById('addTransaction');
this.addBudgetBtn = document.getElementById('addBudget');
this.cancelBtns = document.querySelectorAll('.form__cancel-btn');
this.saveBtns = document.querySelectorAll('.form__save-btn');
this.dialogs = document.querySelectorAll('.dialog');
this.categoryField = document.getElementById('selectCategory');
this.closeIcon = document.querySelector('.close-icon');
@@ -23,7 +24,7 @@ export default class HomeView extends CommonView {
this.walletDialog = document.getElementById('walletDialog');
}
async loadPage(getInfoUserLogin, checkWalletExist, getWalletByIdUser) {
async loadPage(getInfoUserLogin, isValidWallet, getWalletByIdUser) {
this.getWalletByIdUser = getWalletByIdUser; // Init function
this.toggleLoaderSpinner();
const user = await getInfoUserLogin();
@@ -32,10 +33,10 @@ export default class HomeView extends CommonView {
window.location.replace('/login');
} else {
this.user = user;
const walletExist = await checkWalletExist(user.id);
const wallet = await isValidWallet(user.id);
// Check user's wallet if have or not
if (!walletExist) {
if (!wallet) {
// Show add wallet dialog
this.walletDialog.showModal();
} else {
@@ -126,12 +127,12 @@ export default class HomeView extends CommonView {
validateWalletForm(bodyDialog) {
this.walletName = bodyDialog.querySelector('.form__input-text').value;
this.amount = bodyDialog.querySelector('.form__input-balance').value;
const saveBtn = bodyDialog.querySelector('.form__save-btn');
const saveBtns = bodyDialog.querySelector('.form__save-btn');
if (this.walletName.length >= 3 && this.amount.length >= 1) {
saveBtn.classList.add('active');
saveBtns.classList.add('active');
} else {
saveBtn.classList.remove('active');
saveBtns.classList.remove('active');
}
}
@@ -23,7 +23,7 @@ export default class RegisterView extends CommonLoginRegisterView {
const account = { email, password, passwordConfirm };
if (this.validateForm(account)) {
if (this.isValidateAccount(account)) {
const user = new User(account);
return user;