From e6dc0294056d3016945196cca035ce3c470b16f6 Mon Sep 17 00:00:00 2001 From: Loi Phan Date: Tue, 3 Oct 2023 20:50:53 +0700 Subject: [PATCH] Fix comments --- .../src/ts/constants/messages.ts | 50 ++++++++----------- .../src/ts/controllers/homeController.ts | 9 ++-- .../src/ts/controllers/index.ts | 9 ++-- .../src/ts/controllers/loginController.ts | 8 ++- .../src/ts/controllers/registerController.ts | 8 ++- typescript-practice/src/ts/helpers/connect.ts | 4 +- .../src/ts/helpers/validatorForm.ts | 4 +- .../src/ts/views/authenticationView.ts | 15 ++---- .../src/ts/views/home/budgetView.ts | 7 ++- .../src/ts/views/home/homeView.ts | 9 ++-- .../src/ts/views/home/transactionView.ts | 18 ++++--- .../src/ts/views/home/walletView.ts | 5 +- typescript-practice/src/ts/views/loginView.ts | 12 ++--- .../src/ts/views/registerView.ts | 15 ++---- 14 files changed, 76 insertions(+), 97 deletions(-) diff --git a/typescript-practice/src/ts/constants/messages.ts b/typescript-practice/src/ts/constants/messages.ts index a3c69df..38b5b28 100644 --- a/typescript-practice/src/ts/constants/messages.ts +++ b/typescript-practice/src/ts/constants/messages.ts @@ -1,31 +1,23 @@ -export const PASSWORD_NOT_MATCH = 'Password not match!'; - -export const PASSWORD_NOT_STRONG = - 'Password must at least one uppercase, one lowercase letter, one number and one special character!'; - -export const ERROR_CREDENTIAL = { - title: 'Error Credential', - message: 'Email or password not match! Please try again!', +export const VALIDATE_FORM = { + INVALID_EMAIL_FORMAT: `The email is not correct!`, + PASSWORD_NOT_MATCH: 'Password not match!', + PASSWORD_NOT_STRONG: + 'Password must at least one uppercase, one lowercase letter, one number and one special character!', + ERROR_CREDENTIAL: { + title: 'Error Credential', + message: 'Email or password not match! Please try again!', + }, + REQUIRED_MESSAGE: (field: string) => `The ${field} is required!`, }; -export const INVALID_EMAIL_FORMAT = `The email is not correct!`; - -export const REQUIRED_MESSAGE = (field: string) => `The ${field} is required!`; - -export const ERROR_MESSAGE_DEFAULT = ['Something went wrong!']; - -export const TIME_OUT_ERROR = 'Connection time out! Please try again!'; - -export const DEFAULT_TITLE_ERROR_TOAST = 'Error'; - -export const USER_EXIST_ERROR = 'User is exists! Please try another email!'; - -export const ADD_WALLET_SUCCESS = 'Add wallet success!'; - -export const DEFAULT_MESSAGE = 'Press OK to continue!'; - -export const ADD_TRANSACTION_SUCCESS = 'Add success!'; - -export const UPDATE_TRANSACTION_SUCCESS = 'Update success!'; - -export const REGISTER_SUCCESS = 'Register Success'; +export const TOAST = { + ERROR_MESSAGE_DEFAULT: ['Something went wrong!'], + TIME_OUT_ERROR: 'Connection time out! Please try again!', + DEFAULT_TITLE_ERROR_TOAST: 'Error', + USER_EXIST_ERROR: 'User is exists! Please try another email!', + ADD_WALLET_SUCCESS: 'Add wallet success!', + DEFAULT_MESSAGE: 'Press OK to continue!', + ADD_TRANSACTION_SUCCESS: 'Add success!', + UPDATE_TRANSACTION_SUCCESS: 'Update success!', + REGISTER_SUCCESS: 'Register Success', +}; diff --git a/typescript-practice/src/ts/controllers/homeController.ts b/typescript-practice/src/ts/controllers/homeController.ts index f6c6a65..54b1ddf 100644 --- a/typescript-practice/src/ts/controllers/homeController.ts +++ b/typescript-practice/src/ts/controllers/homeController.ts @@ -1,20 +1,17 @@ import HomeView from 'views/home/homeView'; import Service from 'services'; -import View from 'views'; import Wallet from 'models/wallet'; import Transaction from 'models/transaction'; import Category from 'models/category'; -import { IHomeFunc } from 'global/types'; +import { IHomeFunc, Nullable } from 'global/types'; export default class HomeController { - public homeView: HomeView | null = null; - constructor( public service: Service, - public view: View, + public homeView: Nullable, ) { this.service = service; - this.homeView = view.homeView; + this.homeView = homeView; } handlerGetInfoUserLogin() { diff --git a/typescript-practice/src/ts/controllers/index.ts b/typescript-practice/src/ts/controllers/index.ts index 2d54357..ce7bc54 100644 --- a/typescript-practice/src/ts/controllers/index.ts +++ b/typescript-practice/src/ts/controllers/index.ts @@ -15,8 +15,11 @@ export default class Controller { public service: Service, public view: View, ) { - this.registerController = new RegisterController(service, view); - this.loginController = new LoginController(service, view); - this.homeController = new HomeController(service, view); + this.registerController = new RegisterController( + service, + view.registerView, + ); + this.loginController = new LoginController(service, view.loginView); + this.homeController = new HomeController(service, view.homeView); } } diff --git a/typescript-practice/src/ts/controllers/loginController.ts b/typescript-practice/src/ts/controllers/loginController.ts index cf1a724..a6c79f6 100644 --- a/typescript-practice/src/ts/controllers/loginController.ts +++ b/typescript-practice/src/ts/controllers/loginController.ts @@ -1,17 +1,15 @@ +import { Nullable } from 'global/types'; import User from 'models/user'; import Service from 'services'; -import View from 'views'; import LoginView from 'views/loginView'; export default class LoginController { - public loginView: LoginView | null = null; - constructor( public service: Service, - public view: View, + public loginView: Nullable, ) { this.service = service; - this.loginView = view.loginView; + this.loginView = loginView; } handlerLoginUser(email: string, password: string): Promise { diff --git a/typescript-practice/src/ts/controllers/registerController.ts b/typescript-practice/src/ts/controllers/registerController.ts index 642173b..8c94fa7 100644 --- a/typescript-practice/src/ts/controllers/registerController.ts +++ b/typescript-practice/src/ts/controllers/registerController.ts @@ -1,17 +1,15 @@ +import { Nullable } from 'global/types'; import User from 'models/user'; import Service from 'services'; -import View from 'views'; import RegisterView from 'views/registerView'; export default class RegisterController { - public registerView: RegisterView | null = null; - constructor( public service: Service, - public view: View, + public registerView: Nullable, ) { this.service = service; - this.registerView = view.registerView; + this.registerView = registerView; } handlerCheckUserValid(email: string): Promise { diff --git a/typescript-practice/src/ts/helpers/connect.ts b/typescript-practice/src/ts/helpers/connect.ts index 53d62e9..7560fb1 100644 --- a/typescript-practice/src/ts/helpers/connect.ts +++ b/typescript-practice/src/ts/helpers/connect.ts @@ -1,4 +1,4 @@ -import { TIME_OUT_ERROR } from '../constants/messages'; +import { TOAST } from '../constants/messages'; import { TIME_OUT_SEC } from '../constants/config'; import FirebaseService from '../services/firebaseService'; @@ -11,7 +11,7 @@ export const timeout = (s: number): Promise => { return new Promise((_, reject) => { setTimeout(() => { FirebaseService.disconnect(); - reject(TIME_OUT_ERROR); + reject(TOAST.TIME_OUT_ERROR); }, s * 1000); }); }; diff --git a/typescript-practice/src/ts/helpers/validatorForm.ts b/typescript-practice/src/ts/helpers/validatorForm.ts index 9cf70a5..ef158e1 100644 --- a/typescript-practice/src/ts/helpers/validatorForm.ts +++ b/typescript-practice/src/ts/helpers/validatorForm.ts @@ -1,5 +1,5 @@ import { REGEX } from '../constants/config'; -import { REQUIRED_MESSAGE } from '../constants/messages'; +import { VALIDATE_FORM } from '../constants/messages'; /** * Validate password @@ -23,7 +23,7 @@ export const compare2Password = ( export const renderRequiredText = (field: string, element: Element) => { const markup: string = ` -

${REQUIRED_MESSAGE(field)}

+

${VALIDATE_FORM.REQUIRED_MESSAGE(field)}

`; element.insertAdjacentHTML('afterend', markup); diff --git a/typescript-practice/src/ts/views/authenticationView.ts b/typescript-practice/src/ts/views/authenticationView.ts index 77089c7..59ccb17 100644 --- a/typescript-practice/src/ts/views/authenticationView.ts +++ b/typescript-practice/src/ts/views/authenticationView.ts @@ -1,10 +1,5 @@ import CommonView from './commonView'; -import { - INVALID_EMAIL_FORMAT, - PASSWORD_NOT_MATCH, - PASSWORD_NOT_STRONG, - ERROR_MESSAGE_DEFAULT, -} from 'constants/messages'; +import { TOAST, VALIDATE_FORM } from 'constants/messages'; import { compare2Password, isValidPassword, @@ -31,7 +26,7 @@ export default class AuthenticationView extends CommonView { this.formEl = document.querySelector('.form'); this.emailEl = document.querySelector("[name='email']"); - this.messageDefault = ERROR_MESSAGE_DEFAULT; + this.messageDefault = TOAST.ERROR_MESSAGE_DEFAULT; this.inputPasswordEl = document.querySelector('input[name="password"]'); this.inputPasswordConfirmEl = document.querySelector( 'input[name="password_confirm"]', @@ -44,7 +39,7 @@ export default class AuthenticationView extends CommonView { validateEmail(email: string): boolean { if (email) { if (!isValidateEmail(email)) { - this.listError.push(INVALID_EMAIL_FORMAT); + this.listError.push(VALIDATE_FORM.INVALID_EMAIL_FORMAT); return false; } @@ -58,7 +53,7 @@ export default class AuthenticationView extends CommonView { validatePassword(password: string): boolean { if (password) { if (!isValidPassword(password)) { - this.listError.push(PASSWORD_NOT_STRONG); + this.listError.push(VALIDATE_FORM.PASSWORD_NOT_STRONG); return false; } @@ -74,7 +69,7 @@ export default class AuthenticationView extends CommonView { validatePasswordConfirm(password: string, passwordConfirm: string): boolean { if (passwordConfirm) { if (!compare2Password(password, passwordConfirm)) { - this.listError.push(PASSWORD_NOT_MATCH); + this.listError.push(VALIDATE_FORM.PASSWORD_NOT_MATCH); return false; } diff --git a/typescript-practice/src/ts/views/home/budgetView.ts b/typescript-practice/src/ts/views/home/budgetView.ts index 383fd7d..4bcc37f 100644 --- a/typescript-practice/src/ts/views/home/budgetView.ts +++ b/typescript-practice/src/ts/views/home/budgetView.ts @@ -4,7 +4,7 @@ import EventDataTrigger from 'helpers/evDataTrigger'; import Wallet from 'models/wallet'; import User from 'models/user'; import { DEFAULT_CATEGORY } from 'constants/config'; -import { ADD_TRANSACTION_SUCCESS, DEFAULT_MESSAGE } from 'constants/messages'; +import { TOAST } from 'constants/messages'; import { IBudgetViewFunc, Data, @@ -140,7 +140,10 @@ export default class BudgetView { this.toggleLoaderSpinner!(); // Show success message - this.showSuccessToast!(ADD_TRANSACTION_SUCCESS, DEFAULT_MESSAGE); + this.showSuccessToast!( + TOAST.ADD_TRANSACTION_SUCCESS, + TOAST.DEFAULT_MESSAGE, + ); (document.getElementById('formAddBudget')!).reset(); } diff --git a/typescript-practice/src/ts/views/home/homeView.ts b/typescript-practice/src/ts/views/home/homeView.ts index fea1d8f..60d15b0 100644 --- a/typescript-practice/src/ts/views/home/homeView.ts +++ b/typescript-practice/src/ts/views/home/homeView.ts @@ -17,7 +17,7 @@ import { PromiseOrNull, TError, } from 'global/types'; -import { DEFAULT_TITLE_ERROR_TOAST } from 'constants/messages'; +import { TOAST } from 'constants/messages'; import { redirectToLoginPage } from 'helpers/url'; import Category from 'models/category'; import CategoryView from './categoryView'; @@ -191,13 +191,10 @@ export default class HomeView extends CommonView { this.sendData(); await this.categoryView.loadCategory(); - - await this.loadWalletUser(); - this.summaryTabView.load(); - this.transactionTabView.loadTransactionTab(); + await this.loadWalletUser(); await this.updateAmountWallet(); } @@ -327,7 +324,7 @@ export default class HomeView extends CommonView { const title = typeof error === 'object' && error.title ? error.title - : DEFAULT_TITLE_ERROR_TOAST; + : TOAST.DEFAULT_TITLE_ERROR_TOAST; const content = typeof error === 'object' && error.message diff --git a/typescript-practice/src/ts/views/home/transactionView.ts b/typescript-practice/src/ts/views/home/transactionView.ts index dd806eb..4b55389 100644 --- a/typescript-practice/src/ts/views/home/transactionView.ts +++ b/typescript-practice/src/ts/views/home/transactionView.ts @@ -12,11 +12,7 @@ import { } from 'global/types'; import Wallet from 'models/wallet'; import Category from 'models/category'; -import { - ADD_TRANSACTION_SUCCESS, - DEFAULT_MESSAGE, - UPDATE_TRANSACTION_SUCCESS, -} from 'constants/messages'; +import { TOAST } from 'constants/messages'; import { renderRequiredText } from 'helpers/validatorForm'; export default class TransactionView { @@ -170,7 +166,7 @@ export default class TransactionView { await this.updateAmountWallet!(); await this.loadData!(); - this.showSuccessToast!('Delete success!', DEFAULT_MESSAGE); + this.showSuccessToast!('Delete success!', TOAST.DEFAULT_MESSAGE); } catch (error) { this.showErrorToast!(error as TError); } @@ -296,10 +292,16 @@ export default class TransactionView { if (!idEl.value) { // Add success - this.showSuccessToast!(ADD_TRANSACTION_SUCCESS, DEFAULT_MESSAGE); + this.showSuccessToast!( + TOAST.ADD_TRANSACTION_SUCCESS, + TOAST.DEFAULT_MESSAGE, + ); } else { // Update success - this.showSuccessToast!(UPDATE_TRANSACTION_SUCCESS, DEFAULT_MESSAGE); + this.showSuccessToast!( + TOAST.UPDATE_TRANSACTION_SUCCESS, + TOAST.DEFAULT_MESSAGE, + ); } this.toggleLoaderSpinner!(); diff --git a/typescript-practice/src/ts/views/home/walletView.ts b/typescript-practice/src/ts/views/home/walletView.ts index d2ca12e..8954740 100644 --- a/typescript-practice/src/ts/views/home/walletView.ts +++ b/typescript-practice/src/ts/views/home/walletView.ts @@ -12,7 +12,7 @@ import { } from 'global/types'; import User from 'models/user'; import { FIRST_ADD_WALLET_NOTE } from 'constants/defaultVariable'; -import { ADD_WALLET_SUCCESS, DEFAULT_MESSAGE } from 'constants/messages'; +import { TOAST } from 'constants/messages'; export default class WalletView { walletDialog: Nullable = null; @@ -148,7 +148,7 @@ export default class WalletView { await this.loadData!(); this.loadEvent!(); - this.showSuccessToast!(ADD_WALLET_SUCCESS, DEFAULT_MESSAGE); + this.showSuccessToast!(TOAST.ADD_WALLET_SUCCESS, TOAST.DEFAULT_MESSAGE); this.toggleLoaderSpinner!(); } @@ -180,7 +180,6 @@ export default class WalletView { return true; } - // eslint-disable-next-line class-methods-use-this changeBtnStyleWalletDialog(bodyDialog: Element) { const walletName = (( bodyDialog.querySelector('.form__input-text') diff --git a/typescript-practice/src/ts/views/loginView.ts b/typescript-practice/src/ts/views/loginView.ts index 984bcb7..e581b55 100644 --- a/typescript-practice/src/ts/views/loginView.ts +++ b/typescript-practice/src/ts/views/loginView.ts @@ -1,10 +1,7 @@ import AuthenticationView from './authenticationView'; import { TypeToast, BTN_CONTENT } from '../constants/config'; import User from 'models/user'; -import { - DEFAULT_TITLE_ERROR_TOAST, - ERROR_CREDENTIAL, -} from 'constants/messages'; +import { TOAST, VALIDATE_FORM } from 'constants/messages'; import { CustomError, Nullable, PromiseOrNull, TError } from 'global/types'; import { redirectToLoginPage } from 'helpers/url'; @@ -34,7 +31,7 @@ export default class LoginView extends AuthenticationView { const title = typeof error === 'object' && error.title ? error.title - : DEFAULT_TITLE_ERROR_TOAST; + : TOAST.DEFAULT_TITLE_ERROR_TOAST; const content = typeof error === 'object' && error.message @@ -88,7 +85,10 @@ export default class LoginView extends AuthenticationView { window.location.replace('/'); return; } - throw new CustomError(ERROR_CREDENTIAL.title, ERROR_CREDENTIAL.message); + throw new CustomError( + VALIDATE_FORM.ERROR_CREDENTIAL.title, + VALIDATE_FORM.ERROR_CREDENTIAL.message, + ); } } catch (error) { // Show toast error diff --git a/typescript-practice/src/ts/views/registerView.ts b/typescript-practice/src/ts/views/registerView.ts index 055d522..3cda090 100644 --- a/typescript-practice/src/ts/views/registerView.ts +++ b/typescript-practice/src/ts/views/registerView.ts @@ -2,12 +2,7 @@ import { TypeToast, BTN_CONTENT } from '../constants/config'; import AuthenticationView from './authenticationView'; import User from '../models/user'; import { redirectToLoginPage } from '../helpers/url'; -import { - DEFAULT_MESSAGE, - DEFAULT_TITLE_ERROR_TOAST, - REGISTER_SUCCESS, - USER_EXIST_ERROR, -} from 'constants/messages'; +import { TOAST } from 'constants/messages'; import { Nullable, PromiseOrNull, TError } from 'global/types'; interface UserInput { @@ -95,8 +90,8 @@ export default class RegisterView extends AuthenticationView { */ showRegisterSuccessToast() { const typeToast = TypeToast.success; - const title = REGISTER_SUCCESS; - const content = DEFAULT_MESSAGE; + const title = TOAST.REGISTER_SUCCESS; + const content = TOAST.DEFAULT_MESSAGE; const btnContent = BTN_CONTENT.OK; this.initToastContent(typeToast, title, content, btnContent); @@ -117,7 +112,7 @@ export default class RegisterView extends AuthenticationView { const title = typeof error === 'object' && error.title ? error.title - : DEFAULT_TITLE_ERROR_TOAST; + : TOAST.DEFAULT_TITLE_ERROR_TOAST; const content = typeof error === 'object' && error.message @@ -168,7 +163,7 @@ export default class RegisterView extends AuthenticationView { // Check user exist const userExist = await checkExistUser(user.email); if (userExist) { - throw Error(USER_EXIST_ERROR); + throw Error(TOAST.USER_EXIST_ERROR); } else { await saveUser(user); // Show toast success