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