mirror of
https://github.com/Nezumi-2711/typescript-training.git
synced 2026-09-22 13:48:29 +00:00
Fix bug and improve code
This commit is contained in:
@@ -3,15 +3,15 @@ import Service from './services/index';
|
||||
import View from './views/index';
|
||||
|
||||
export default class App {
|
||||
private _controller: Controller;
|
||||
controller: Controller;
|
||||
|
||||
constructor() {
|
||||
this._controller = new Controller(new Service(), new View());
|
||||
this.controller = new Controller(new Service(), new View());
|
||||
}
|
||||
|
||||
start() {
|
||||
this._controller.registerController.init();
|
||||
this._controller.loginController.init();
|
||||
this._controller.homeController.init();
|
||||
this.controller.registerController.init();
|
||||
this.controller.loginController.init();
|
||||
this.controller.homeController.init();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ export default class HomeController {
|
||||
return this.service.userService.getInfoUserLogin();
|
||||
}
|
||||
|
||||
handlerGetWalletByIdUser(idUser: number): Promise<Wallet | null> {
|
||||
handlerGetWalletByIdUser(idUser: string): Promise<Wallet | null> {
|
||||
return this.service.walletService.getWalletByIdUser(idUser);
|
||||
}
|
||||
|
||||
@@ -37,11 +37,11 @@ export default class HomeController {
|
||||
return this.service.categoryService.getAllCategory();
|
||||
}
|
||||
|
||||
handlerGetAllTransactions(idUser: number): Promise<Transaction[] | null> {
|
||||
handlerGetAllTransactions(idUser: string): Promise<Transaction[] | null> {
|
||||
return this.service.transactionService.getListTransactionByIdUser(idUser);
|
||||
}
|
||||
|
||||
handlerDeleteTransaction(idTransaction: number): Promise<void> {
|
||||
handlerDeleteTransaction(idTransaction: string): Promise<void> {
|
||||
return this.service.transactionService.deleteTransaction(idTransaction);
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ export interface Data {
|
||||
}
|
||||
|
||||
export interface ItemTransaction {
|
||||
id: number;
|
||||
id: string;
|
||||
day: string;
|
||||
fullDateString: string;
|
||||
note: string;
|
||||
|
||||
@@ -5,8 +5,8 @@ import Transaction from 'models/transaction';
|
||||
import TransactionDetail from 'models/transactionDetail';
|
||||
import Category from 'models/category';
|
||||
|
||||
export const generateId = (): number => {
|
||||
return new Date().getTime();
|
||||
export const generateId = (): string => {
|
||||
return new Date().getTime().toString();
|
||||
};
|
||||
|
||||
export const convertDataObjectToModel = <T>(dataObj: DataObject<T>): T => {
|
||||
@@ -110,7 +110,7 @@ export const createTransactionDetailObject = (
|
||||
return tempData;
|
||||
});
|
||||
|
||||
results.sort((a, b) => b.id - a.id);
|
||||
results.sort((a, b) => parseInt(b.id) - parseInt(a.id));
|
||||
|
||||
return results;
|
||||
};
|
||||
|
||||
@@ -1,25 +1,13 @@
|
||||
export default class Category {
|
||||
private readonly _id: number;
|
||||
readonly id: string;
|
||||
|
||||
private _url: string;
|
||||
url: string;
|
||||
|
||||
private _name: string;
|
||||
name: string;
|
||||
|
||||
constructor(id: number, url: string, name: string) {
|
||||
this._id = id;
|
||||
this._url = url;
|
||||
this._name = name;
|
||||
}
|
||||
|
||||
get id() {
|
||||
return this._id;
|
||||
}
|
||||
|
||||
get url() {
|
||||
return this._url;
|
||||
}
|
||||
|
||||
get name() {
|
||||
return this._name;
|
||||
constructor(id: string, url: string, name: string) {
|
||||
this.id = id;
|
||||
this.url = url;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@ import { generateId } from 'helpers/data';
|
||||
|
||||
export default class Transaction {
|
||||
constructor(
|
||||
public id: number,
|
||||
public id: string,
|
||||
public categoryName: string,
|
||||
public date: string,
|
||||
public note: string,
|
||||
public amount: number,
|
||||
public idUser: number,
|
||||
public idUser: string,
|
||||
) {
|
||||
this.id = id !== 0 ? id : generateId();
|
||||
this.id = id ? id : generateId();
|
||||
this.categoryName = categoryName;
|
||||
this.date = date;
|
||||
this.note = note;
|
||||
|
||||
@@ -1,47 +1,18 @@
|
||||
import { generateId } from '../helpers/data';
|
||||
|
||||
export default class User {
|
||||
private readonly _id: number;
|
||||
id: string;
|
||||
|
||||
private _email: string;
|
||||
email: string;
|
||||
|
||||
private _password: string;
|
||||
password: string;
|
||||
|
||||
private _accessToken: string;
|
||||
accessToken: string;
|
||||
|
||||
constructor(email: string, password: string, accessToken?: string) {
|
||||
this._id = generateId();
|
||||
this._email = email;
|
||||
this._password = password || '';
|
||||
this._accessToken = accessToken || '';
|
||||
}
|
||||
|
||||
get password() {
|
||||
return this._password;
|
||||
}
|
||||
|
||||
set accessToken(accessToken: string) {
|
||||
this._accessToken = accessToken;
|
||||
}
|
||||
|
||||
get accessToken() {
|
||||
return this._accessToken;
|
||||
}
|
||||
|
||||
get email() {
|
||||
return this._email;
|
||||
}
|
||||
|
||||
get id() {
|
||||
return this._id;
|
||||
}
|
||||
|
||||
get toObject() {
|
||||
return {
|
||||
id: this._id,
|
||||
email: this._email,
|
||||
password: this._password,
|
||||
accessToken: this.accessToken,
|
||||
} as User;
|
||||
this.id = generateId();
|
||||
this.email = email;
|
||||
this.password = password || '';
|
||||
this.accessToken = accessToken || '';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { generateId } from '../helpers/data';
|
||||
|
||||
export default class Wallet {
|
||||
id: number;
|
||||
id: string;
|
||||
|
||||
walletName: string;
|
||||
|
||||
@@ -9,13 +9,13 @@ export default class Wallet {
|
||||
|
||||
outflow: number;
|
||||
|
||||
idUser: number;
|
||||
idUser: string;
|
||||
|
||||
constructor(
|
||||
walletName: string,
|
||||
inflow: number,
|
||||
outflow: number,
|
||||
idUser: number,
|
||||
idUser: string,
|
||||
) {
|
||||
this.id = generateId();
|
||||
this.walletName = walletName;
|
||||
|
||||
@@ -68,7 +68,7 @@ export default class CommonService<T> {
|
||||
) {
|
||||
this.connectToDb();
|
||||
const data = this.firebaseService.getDataFromProp(path, property, value);
|
||||
const result = (await timeOutConnect(data)) as DataObject<T>;
|
||||
const result = <DataObject<T>>await timeOutConnect(data);
|
||||
|
||||
if (result && typeof result === 'object') {
|
||||
return convertDataObjectToModel(result);
|
||||
@@ -98,7 +98,7 @@ export default class CommonService<T> {
|
||||
|
||||
async getListDataFromProp(
|
||||
property: string,
|
||||
value: string,
|
||||
value: string | number,
|
||||
path: string = this.defaultPath,
|
||||
): Promise<T[] | null> {
|
||||
this.connectToDb();
|
||||
@@ -121,7 +121,7 @@ export default class CommonService<T> {
|
||||
}
|
||||
|
||||
async deleteData(
|
||||
id: number,
|
||||
id: string,
|
||||
path = this.defaultPath,
|
||||
): Promise<string | void> {
|
||||
this.connectToDb();
|
||||
|
||||
@@ -12,16 +12,16 @@ import {
|
||||
import { DATABASE_URL } from '../constants/config';
|
||||
|
||||
class FirebaseService {
|
||||
private _app: FirebaseApp;
|
||||
app: FirebaseApp;
|
||||
|
||||
private _db: Database;
|
||||
db: Database;
|
||||
|
||||
constructor() {
|
||||
const firebaseConfig = {
|
||||
databaseURL: DATABASE_URL,
|
||||
};
|
||||
this._app = initializeApp(firebaseConfig);
|
||||
this._db = getDatabase(this._app);
|
||||
this.app = initializeApp(firebaseConfig);
|
||||
this.db = getDatabase(this.app);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,25 +31,25 @@ class FirebaseService {
|
||||
* @returns {Promise} Return the resolves when write to database completed
|
||||
*/
|
||||
save(data: object, path: string): Promise<void> {
|
||||
return set(ref(this._db, path), data);
|
||||
return set(ref(this.db, path), data);
|
||||
}
|
||||
|
||||
delete(id: number, path: string): Promise<void> {
|
||||
return remove(ref(this._db, path + id));
|
||||
delete(id: string, path: string): Promise<void> {
|
||||
return remove(ref(this.db, path + id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect to database
|
||||
*/
|
||||
disconnect(): void {
|
||||
goOffline(this._db);
|
||||
goOffline(this.db);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconnect to database
|
||||
*/
|
||||
reconnect(): void {
|
||||
goOnline(this._db);
|
||||
goOnline(this.db);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,7 +66,7 @@ class FirebaseService {
|
||||
): Promise<object | null> {
|
||||
return new Promise((resolve) => {
|
||||
onValue(
|
||||
ref(this._db, path),
|
||||
ref(this.db, path),
|
||||
(snapshot) => {
|
||||
let id: string | null = null;
|
||||
let data: object | null = null;
|
||||
@@ -104,7 +104,7 @@ class FirebaseService {
|
||||
getDataFromId(id: string, path: string): Promise<object> {
|
||||
return new Promise((resolve) => {
|
||||
onValue(
|
||||
ref(this._db, path + id),
|
||||
ref(this.db, path + id),
|
||||
(snapshot) => {
|
||||
resolve(snapshot.val());
|
||||
},
|
||||
@@ -118,7 +118,7 @@ class FirebaseService {
|
||||
getAllDataFromPath(path: string): Promise<object[]> {
|
||||
return new Promise((resolve) => {
|
||||
onValue(
|
||||
ref(this._db, path),
|
||||
ref(this.db, path),
|
||||
(snapshot) => {
|
||||
const listData: object[] = [];
|
||||
|
||||
@@ -145,11 +145,11 @@ class FirebaseService {
|
||||
getListDataFromProp(
|
||||
path: string,
|
||||
property: string,
|
||||
value: string,
|
||||
value: string | number,
|
||||
): Promise<object[]> {
|
||||
return new Promise((resolve) => {
|
||||
onValue(
|
||||
ref(this._db, path),
|
||||
ref(this.db, path),
|
||||
(snapshot) => {
|
||||
let id: string;
|
||||
let data: object;
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
class LocalStorageService {
|
||||
private _localStorage: Storage;
|
||||
localStorage: Storage;
|
||||
|
||||
constructor() {
|
||||
this._localStorage = localStorage;
|
||||
this.localStorage = localStorage;
|
||||
}
|
||||
|
||||
add(key: string, value: string): void {
|
||||
this._localStorage.setItem(key, value);
|
||||
this.localStorage.setItem(key, value);
|
||||
}
|
||||
|
||||
get(key: string): string | null {
|
||||
return this._localStorage.getItem(key);
|
||||
return this.localStorage.getItem(key);
|
||||
}
|
||||
|
||||
remove(key: string): void {
|
||||
this._localStorage.removeItem(key);
|
||||
this.localStorage.removeItem(key);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this._localStorage.clear();
|
||||
this.localStorage.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,18 +17,18 @@ export default class TransactionService extends CommonService<Transaction> {
|
||||
}
|
||||
|
||||
async getListTransactionByIdUser(
|
||||
idUser: number,
|
||||
idUser: string,
|
||||
): Promise<Transaction[] | null> {
|
||||
const results = this.getListDataFromProp(
|
||||
const results = await this.getListDataFromProp(
|
||||
'idUser',
|
||||
idUser.toString(),
|
||||
idUser,
|
||||
this.defaultPath,
|
||||
);
|
||||
|
||||
return results || null;
|
||||
}
|
||||
|
||||
async deleteTransaction(idTransaction: number) {
|
||||
async deleteTransaction(idTransaction: string) {
|
||||
await this.deleteData(idTransaction);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@ import CommonService from './commonService';
|
||||
import LocalStorageService from './localStorageService';
|
||||
|
||||
export default class UserService {
|
||||
private _commonService: CommonService<User>;
|
||||
private commonService: CommonService<User>;
|
||||
|
||||
constructor() {
|
||||
this._commonService = new CommonService<User>('users/');
|
||||
this.commonService = new CommonService<User>('users/');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -16,7 +16,7 @@ export default class UserService {
|
||||
* @param {Object} user The user object need to be saved into database
|
||||
*/
|
||||
async saveUser(user: User): Promise<void> {
|
||||
await this._commonService.save(user);
|
||||
await this.commonService.save(user);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,7 +36,7 @@ export default class UserService {
|
||||
* @returns {Object || null} Return new User Object if find, otherwise return null.
|
||||
*/
|
||||
async getUserByEmail(email: string): Promise<User | null> {
|
||||
const result = await this._commonService.getDataFromProp('email', email);
|
||||
const result = await this.commonService.getDataFromProp('email', email);
|
||||
|
||||
return result || null;
|
||||
}
|
||||
@@ -73,7 +73,7 @@ export default class UserService {
|
||||
// Add token to user object
|
||||
newUserData.accessToken = createToken();
|
||||
|
||||
this._commonService.save(newUserData);
|
||||
this.commonService.save(newUserData);
|
||||
|
||||
// Add access token to local storage
|
||||
LocalStorageService.add(
|
||||
@@ -102,7 +102,7 @@ export default class UserService {
|
||||
* @returns {Object || null} Return new User Object if find, otherwise return null.
|
||||
*/
|
||||
async getUserByToken(accessToken: string): Promise<User | null> {
|
||||
const result = await this._commonService.getDataFromProp(
|
||||
const result = await this.commonService.getDataFromProp(
|
||||
'accessToken',
|
||||
accessToken,
|
||||
);
|
||||
|
||||
@@ -21,7 +21,7 @@ export default class WalletService extends CommonService<Wallet> {
|
||||
* @param {string} idUser The id user to find user's wallet
|
||||
* @returns {boolean} Return true if find, otherwise return false
|
||||
*/
|
||||
async isValidWallet(idUser: number): Promise<boolean> {
|
||||
async isValidWallet(idUser: string): Promise<boolean> {
|
||||
const wallet = await this.getWalletByIdUser(idUser);
|
||||
|
||||
return !!wallet;
|
||||
@@ -32,11 +32,8 @@ export default class WalletService extends CommonService<Wallet> {
|
||||
* @param {string} email The id user to find user's wallet
|
||||
* @returns {Object || null} Return new Wallet Object if find, otherwise return null.
|
||||
*/
|
||||
async getWalletByIdUser(idUser: number): Promise<Wallet | null> {
|
||||
const result = (await this.getDataFromProp(
|
||||
'idUser',
|
||||
idUser.toString(),
|
||||
)) as Wallet;
|
||||
async getWalletByIdUser(idUser: string): Promise<Wallet | null> {
|
||||
const result = (await this.getDataFromProp('idUser', idUser)) as Wallet;
|
||||
|
||||
return result || null;
|
||||
}
|
||||
|
||||
@@ -125,12 +125,12 @@ export default class BudgetView {
|
||||
this.budgetDialog!.close(); // Close dialog
|
||||
|
||||
const transaction = new Transaction(
|
||||
0,
|
||||
'',
|
||||
DEFAULT_CATEGORY.INCOME,
|
||||
date,
|
||||
note,
|
||||
+amount,
|
||||
+this.wallet!.idUser,
|
||||
this.wallet!.idUser,
|
||||
);
|
||||
|
||||
await this.saveTransaction!(transaction);
|
||||
|
||||
@@ -51,15 +51,15 @@ export default class HomeView extends CommonView {
|
||||
|
||||
getInfoUserLogin: (() => Promise<User | null>) | null = null;
|
||||
|
||||
getWalletByIdUser: ((idUser: number) => Promise<Wallet | null>) | null = null;
|
||||
getWalletByIdUser: ((idUser: string) => Promise<Wallet | null>) | null = null;
|
||||
|
||||
getAllCategory: (() => Promise<Category[] | null>) | null = null;
|
||||
|
||||
getAllTransactions:
|
||||
| ((idUser: number) => Promise<Transaction[] | null>)
|
||||
| ((idUser: string) => Promise<Transaction[] | null>)
|
||||
| null = null;
|
||||
|
||||
deleteTransaction: ((idTransaction: number) => Promise<void>) | null = null;
|
||||
deleteTransaction: ((idTransaction: string) => Promise<void>) | null = null;
|
||||
|
||||
saveWallet: ((wallet: Wallet) => Promise<void>) | null = null;
|
||||
|
||||
@@ -91,12 +91,12 @@ export default class HomeView extends CommonView {
|
||||
|
||||
initFunction(
|
||||
getInfoUserLogin: () => Promise<User | null>,
|
||||
getWalletByIdUser: (idUser: number) => Promise<Wallet | null>,
|
||||
getWalletByIdUser: (idUser: string) => Promise<Wallet | null>,
|
||||
getAllCategory: () => Promise<Category[] | null>,
|
||||
getAllTransactions: (idUser: number) => Promise<Transaction[] | null>,
|
||||
getAllTransactions: (idUser: string) => Promise<Transaction[] | null>,
|
||||
saveWallet: (wallet: Wallet) => Promise<void>,
|
||||
saveTransaction: (transaction: Transaction) => Promise<void>,
|
||||
deleteTransaction: (idTransaction: number) => Promise<void>,
|
||||
deleteTransaction: (idTransaction: string) => Promise<void>,
|
||||
transform: Transform,
|
||||
) {
|
||||
this.getInfoUserLogin = getInfoUserLogin;
|
||||
@@ -191,6 +191,8 @@ export default class HomeView extends CommonView {
|
||||
this.summaryTabView.load();
|
||||
|
||||
this.transactionTabView.loadTransactionTab();
|
||||
|
||||
await this.updateAmountWallet();
|
||||
}
|
||||
|
||||
async loadPage() {
|
||||
@@ -204,7 +206,7 @@ export default class HomeView extends CommonView {
|
||||
} else {
|
||||
// If user already login
|
||||
this.user = user;
|
||||
this.wallet = await this.getWalletByIdUser!(user.id);
|
||||
this.wallet = <Wallet>await this.getWalletByIdUser!(user.id);
|
||||
|
||||
this.sendData();
|
||||
// Check user's wallet if have or not
|
||||
|
||||
@@ -92,7 +92,9 @@ export default class TransactionTabView {
|
||||
},
|
||||
);
|
||||
|
||||
tempList.sort((a, b) => b.transactions[0].id - a.transactions[0].id);
|
||||
tempList.sort(
|
||||
(a, b) => parseInt(b.transactions[0].id) - parseInt(a.transactions[0].id),
|
||||
);
|
||||
|
||||
return tempList;
|
||||
}
|
||||
@@ -176,7 +178,7 @@ export default class TransactionTabView {
|
||||
)!;
|
||||
|
||||
if (transactionTime) {
|
||||
const idTransaction = +(<HTMLElement>transactionTime).dataset.id!;
|
||||
const idTransaction = (<HTMLElement>transactionTime).dataset.id!;
|
||||
|
||||
// If it is income transaction, don't show dialog
|
||||
if (
|
||||
|
||||
@@ -29,7 +29,7 @@ export default class TransactionView {
|
||||
|
||||
toggleLoaderSpinner: (() => void) | null = null;
|
||||
|
||||
deleteTransaction: ((idTransaction: number) => Promise<void>) | null = null;
|
||||
deleteTransaction: ((idTransaction: string) => Promise<void>) | null = null;
|
||||
|
||||
loadTransactionData: (() => Promise<void>) | null = null;
|
||||
|
||||
@@ -61,7 +61,7 @@ export default class TransactionView {
|
||||
|
||||
initFunction(
|
||||
toggleLoaderSpinner: () => void,
|
||||
deleteTransaction: (idTransaction: number) => Promise<void>,
|
||||
deleteTransaction: (idTransaction: string) => Promise<void>,
|
||||
loadTransactionData: () => Promise<void>,
|
||||
updateAmountWallet: () => Promise<void>,
|
||||
loadData: () => Promise<void>,
|
||||
@@ -165,7 +165,7 @@ export default class TransactionView {
|
||||
this.transactionDialog!.close();
|
||||
this.toggleLoaderSpinner!();
|
||||
|
||||
await this.deleteTransaction!(+idEl.value);
|
||||
await this.deleteTransaction!(idEl.value);
|
||||
|
||||
// Reload data
|
||||
await this.loadTransactionData!();
|
||||
@@ -199,7 +199,7 @@ export default class TransactionView {
|
||||
);
|
||||
}
|
||||
|
||||
initValueTransactionDialog(idTransaction: number | null) {
|
||||
initValueTransactionDialog(idTransaction: string | null) {
|
||||
let categoryName: string;
|
||||
|
||||
if (idTransaction) {
|
||||
@@ -281,12 +281,12 @@ export default class TransactionView {
|
||||
this.transactionDialog!.close();
|
||||
|
||||
const transaction = new Transaction(
|
||||
+idEl.value,
|
||||
idEl.value,
|
||||
categoryNameEl.value,
|
||||
dateEl.value,
|
||||
noteEl.value,
|
||||
+amount,
|
||||
+this.wallet!.idUser,
|
||||
this.wallet!.idUser,
|
||||
);
|
||||
|
||||
await this.saveTransaction!(transaction);
|
||||
@@ -353,7 +353,7 @@ export default class TransactionView {
|
||||
this.transactionForm!.reset();
|
||||
}
|
||||
|
||||
showTransactionDialog(idTransaction: number | null = null) {
|
||||
showTransactionDialog(idTransaction: string | null = null) {
|
||||
this.clearInputTransactionForm();
|
||||
|
||||
// Init data transaction to dialog
|
||||
|
||||
@@ -8,35 +8,32 @@ import { FIRST_ADD_WALLET_NOTE } from 'constants/defaultVariable';
|
||||
import { ADD_WALLET_SUCCESS, DEFAULT_MESSAGE } from 'constants/messages/dialog';
|
||||
|
||||
export default class WalletView {
|
||||
private _walletDialog: HTMLDialogElement | null = null;
|
||||
walletDialog: HTMLDialogElement | null = null;
|
||||
|
||||
private _transform: Transform | null = null;
|
||||
transform: Transform | null = null;
|
||||
|
||||
private _toggleLoaderSpinner: (() => void) | null = null;
|
||||
toggleLoaderSpinner: (() => void) | null = null;
|
||||
|
||||
private _saveWallet: ((wallet: Wallet) => Promise<void>) | null = null;
|
||||
saveWallet: ((wallet: Wallet) => Promise<void>) | null = null;
|
||||
|
||||
private _saveTransaction:
|
||||
| ((transaction: Transaction) => Promise<void>)
|
||||
| null = null;
|
||||
saveTransaction: ((transaction: Transaction) => Promise<void>) | null = null;
|
||||
|
||||
private _loadTransactionData: (() => Promise<void>) | null = null;
|
||||
loadTransactionData: (() => Promise<void>) | null = null;
|
||||
|
||||
private _loadData: (() => Promise<void>) | null = null;
|
||||
loadData: (() => Promise<void>) | null = null;
|
||||
|
||||
private _loadEvent: (() => void) | null = null;
|
||||
loadEvent: (() => void) | null = null;
|
||||
|
||||
private _showSuccessToast: ((title: string, message: string) => void) | null =
|
||||
null;
|
||||
showSuccessToast: ((title: string, message: string) => void) | null = null;
|
||||
|
||||
private _showErrorToast: ((error: string | TError) => void) | null = null;
|
||||
showErrorToast: ((error: string | TError) => void) | null = null;
|
||||
|
||||
private _user: User | null = null;
|
||||
user: User | null = null;
|
||||
|
||||
private _wallet: Wallet | null = null;
|
||||
wallet: Wallet | null = null;
|
||||
|
||||
constructor() {
|
||||
this._walletDialog = document.getElementById(
|
||||
this.walletDialog = document.getElementById(
|
||||
'walletDialog',
|
||||
) as HTMLDialogElement;
|
||||
|
||||
@@ -54,49 +51,49 @@ export default class WalletView {
|
||||
showSuccessToast: (title: string, message: string) => void,
|
||||
showErrorToast: (error: string | TError) => void,
|
||||
) {
|
||||
this._transform = transform;
|
||||
this._toggleLoaderSpinner = toggleLoaderSpinner;
|
||||
this._saveWallet = saveWallet;
|
||||
this._saveTransaction = saveTransaction;
|
||||
this._loadTransactionData = loadTransactionData;
|
||||
this._loadData = loadData;
|
||||
this._loadEvent = loadEvent;
|
||||
this._showSuccessToast = showSuccessToast;
|
||||
this._showErrorToast = showErrorToast;
|
||||
this.transform = transform;
|
||||
this.toggleLoaderSpinner = toggleLoaderSpinner;
|
||||
this.saveWallet = saveWallet;
|
||||
this.saveTransaction = saveTransaction;
|
||||
this.loadTransactionData = loadTransactionData;
|
||||
this.loadData = loadData;
|
||||
this.loadEvent = loadEvent;
|
||||
this.showSuccessToast = showSuccessToast;
|
||||
this.showErrorToast = showErrorToast;
|
||||
}
|
||||
|
||||
subscribe() {
|
||||
this._transform!.create('walletView', this.updateData.bind(this));
|
||||
this.transform!.create('walletView', this.updateData.bind(this));
|
||||
}
|
||||
|
||||
sendData() {
|
||||
const data = {
|
||||
wallet: this._wallet!,
|
||||
user: this._user!,
|
||||
wallet: this.wallet!,
|
||||
user: this.user!,
|
||||
};
|
||||
|
||||
this._transform!.onSendSignal('walletView', data);
|
||||
this.transform!.onSendSignal('walletView', data);
|
||||
}
|
||||
|
||||
updateData(data: Data) {
|
||||
if (data.wallet) this._wallet = data.wallet;
|
||||
if (data.wallet) this.wallet = data.wallet;
|
||||
|
||||
if (data.user) this._user = data.user;
|
||||
if (data.user) this.user = data.user;
|
||||
}
|
||||
|
||||
showDialog() {
|
||||
this._walletDialog!.showModal();
|
||||
this.walletDialog!.showModal();
|
||||
}
|
||||
|
||||
addHandlerEventWalletForm() {
|
||||
this._walletDialog!.addEventListener('submit', (e) => {
|
||||
this.walletDialog!.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
this.clearErrorStyleWalletDialog();
|
||||
this.submitWalletForm();
|
||||
});
|
||||
|
||||
this._walletDialog!.addEventListener('input', (e) => {
|
||||
this.walletDialog!.addEventListener('input', (e) => {
|
||||
const bodyDialog = (<Element>e.target!).closest('.dialog__body');
|
||||
|
||||
this.changeBtnStyleWalletDialog(bodyDialog!);
|
||||
@@ -106,8 +103,8 @@ export default class WalletView {
|
||||
|
||||
clearErrorStyleWalletDialog() {
|
||||
const inputFieldEls =
|
||||
this._walletDialog!.querySelectorAll('.form__input-field');
|
||||
const errorTextEls = this._walletDialog!.querySelectorAll('.error-text');
|
||||
this.walletDialog!.querySelectorAll('.form__input-field');
|
||||
const errorTextEls = this.walletDialog!.querySelectorAll('.error-text');
|
||||
|
||||
inputFieldEls.forEach((item) => {
|
||||
if (item.classList.contains('error-input'))
|
||||
@@ -128,45 +125,45 @@ export default class WalletView {
|
||||
const amount = walletForm.get('amount') as string;
|
||||
|
||||
if (this.validateWalletDialog(walletName, +amount)) {
|
||||
this._walletDialog!.close();
|
||||
this._toggleLoaderSpinner!();
|
||||
this.walletDialog!.close();
|
||||
this.toggleLoaderSpinner!();
|
||||
|
||||
const wallet = new Wallet(walletName, +amount, 0, this._user!.id);
|
||||
this._wallet = wallet;
|
||||
const wallet = new Wallet(walletName, +amount, 0, this.user!.id);
|
||||
this.wallet = wallet;
|
||||
|
||||
this.sendData();
|
||||
|
||||
await this._saveWallet!(wallet);
|
||||
await this.saveWallet!(wallet);
|
||||
// Transaction info
|
||||
const transaction = new Transaction(
|
||||
0,
|
||||
'',
|
||||
'Income',
|
||||
new Date().toISOString().slice(0, 10),
|
||||
FIRST_ADD_WALLET_NOTE,
|
||||
+amount,
|
||||
this._user!.id,
|
||||
this.user!.id,
|
||||
);
|
||||
|
||||
await this._saveTransaction!(transaction);
|
||||
await this.saveTransaction!(transaction);
|
||||
// Load data and event
|
||||
await this._loadTransactionData!();
|
||||
await this._loadData!();
|
||||
this._loadEvent!();
|
||||
await this.loadTransactionData!();
|
||||
await this.loadData!();
|
||||
this.loadEvent!();
|
||||
|
||||
this._showSuccessToast!(ADD_WALLET_SUCCESS, DEFAULT_MESSAGE);
|
||||
this.showSuccessToast!(ADD_WALLET_SUCCESS, DEFAULT_MESSAGE);
|
||||
|
||||
this._toggleLoaderSpinner!();
|
||||
this.toggleLoaderSpinner!();
|
||||
}
|
||||
} catch (error) {
|
||||
// Show toast error
|
||||
this._showErrorToast!(error as string | TError);
|
||||
this._toggleLoaderSpinner!();
|
||||
this.showErrorToast!(error as string | TError);
|
||||
this.toggleLoaderSpinner!();
|
||||
}
|
||||
}
|
||||
|
||||
validateWalletDialog(walletName: string, amount: number) {
|
||||
const inputFieldEls =
|
||||
this._walletDialog!.querySelectorAll('.form__input-field');
|
||||
this.walletDialog!.querySelectorAll('.form__input-field');
|
||||
|
||||
if (!walletName || !amount) {
|
||||
if (!walletName) {
|
||||
|
||||
@@ -152,7 +152,7 @@ export default class RegisterView extends AuthenticationView {
|
||||
if (userExist) {
|
||||
throw Error(USER_EXIST_ERROR);
|
||||
} else {
|
||||
await saveUser(user.toObject);
|
||||
await saveUser(user);
|
||||
// Show toast success
|
||||
this.showRegisterSuccessToast();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user