mirror of
https://github.com/Nezumi-2711/javascript-training.git
synced 2026-09-22 20:01:31 +00:00
Fix amount wallet user show not correctly
This commit is contained in:
@@ -2,6 +2,8 @@ export const DATABASE_URL =
|
|||||||
'https://javascript-training-81f7a-default-rtdb.asia-southeast1.firebasedatabase.app';
|
'https://javascript-training-81f7a-default-rtdb.asia-southeast1.firebasedatabase.app';
|
||||||
export const TIME_OUT_SEC = 3;
|
export const TIME_OUT_SEC = 3;
|
||||||
|
|
||||||
|
export const FIRST_ADD_WALLET_NOTE = 'Init wallet';
|
||||||
|
|
||||||
export const URL = {
|
export const URL = {
|
||||||
LOGIN: 'login',
|
LOGIN: 'login',
|
||||||
REGISTER: 'register',
|
REGISTER: 'register',
|
||||||
|
|||||||
@@ -1,14 +1,7 @@
|
|||||||
export default class Wallet {
|
export default class Wallet {
|
||||||
constructor({
|
constructor({ walletName = '', inflow = 0, outflow = 0, idUser }) {
|
||||||
walletName = '',
|
|
||||||
amount = 0,
|
|
||||||
inflow = 0,
|
|
||||||
outflow = 0,
|
|
||||||
idUser,
|
|
||||||
}) {
|
|
||||||
this.id = Wallet.createIdWallet();
|
this.id = Wallet.createIdWallet();
|
||||||
this.walletName = walletName;
|
this.walletName = walletName;
|
||||||
this.amount = amount;
|
|
||||||
this.inflow = inflow;
|
this.inflow = inflow;
|
||||||
this.outflow = outflow;
|
this.outflow = outflow;
|
||||||
this.idUser = idUser;
|
this.idUser = idUser;
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
/* eslint-disable class-methods-use-this */
|
/* eslint-disable class-methods-use-this */
|
||||||
import { TYPE_TOAST, BTN_CONTENT, DEFAULT_CATEGORY } from '../constants/config';
|
import {
|
||||||
|
TYPE_TOAST,
|
||||||
|
BTN_CONTENT,
|
||||||
|
DEFAULT_CATEGORY,
|
||||||
|
FIRST_ADD_WALLET_NOTE,
|
||||||
|
} from '../constants/config';
|
||||||
import * as MESSAGE from '../constants/message';
|
import * as MESSAGE from '../constants/message';
|
||||||
import CommonView from './commonView';
|
import CommonView from './commonView';
|
||||||
import Wallet from '../models/wallet';
|
import Wallet from '../models/wallet';
|
||||||
@@ -91,9 +96,8 @@ export default class HomeView extends CommonView {
|
|||||||
async loadData() {
|
async loadData() {
|
||||||
await this.loadWalletUser();
|
await this.loadWalletUser();
|
||||||
await this.loadTransactionData();
|
await this.loadTransactionData();
|
||||||
await this.loadCategory(this.getAllCategory);
|
|
||||||
this.loadSummaryTab();
|
this.loadSummaryTab();
|
||||||
this.loadTransactionTab();
|
await this.loadTransactionTab();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------LOAD DATA---------------------//
|
// ---------------------LOAD DATA---------------------//
|
||||||
@@ -101,38 +105,46 @@ export default class HomeView extends CommonView {
|
|||||||
* Load wallet user
|
* Load wallet user
|
||||||
*/
|
*/
|
||||||
async loadWalletUser() {
|
async loadWalletUser() {
|
||||||
const wallet = await this.getWalletByIdUser(this.user.id);
|
const wallet = this.wallet
|
||||||
|
? this.wallet
|
||||||
|
: await this.getWalletByIdUser(this.user.id);
|
||||||
const walletName = document.querySelector('.wallet__name');
|
const walletName = document.querySelector('.wallet__name');
|
||||||
const walletPrice = document.querySelector('.wallet__price');
|
const walletPrice = document.querySelector('.wallet__price');
|
||||||
const sign = wallet.amount >= 0 ? '+' : '-';
|
|
||||||
const walletNameValue = wallet.walletName;
|
const walletNameValue = wallet.walletName;
|
||||||
// Math.abs(wallet.amount) to keep the value always > 0
|
const walletAmountValue = wallet.inflow + wallet.outflow;
|
||||||
const walletAmountValue = formatNumber(Math.abs(wallet.amount));
|
|
||||||
|
const sign = walletAmountValue >= 0 ? '+' : '-';
|
||||||
|
|
||||||
this.wallet = wallet; // Make wallet into global variable
|
this.wallet = wallet; // Make wallet into global variable
|
||||||
|
|
||||||
walletName.textContent = walletNameValue;
|
walletName.textContent = walletNameValue;
|
||||||
walletPrice.textContent = `${sign}$ ${walletAmountValue}`;
|
walletPrice.textContent = `${sign}$ ${formatNumber(
|
||||||
|
Math.abs(walletAmountValue),
|
||||||
|
)}`; // Math.abs(walletAmountValue) to keep the value always > 0
|
||||||
|
|
||||||
|
// Update amount wallet into database
|
||||||
|
this.saveWallet(this.wallet);
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateAmountWallet(amount, saveWallet) {
|
async updateAmountWallet(amount) {
|
||||||
this.wallet.amount += amount;
|
if (amount >= 0) {
|
||||||
|
|
||||||
if (amount > 0) {
|
|
||||||
this.wallet.inflow += amount;
|
this.wallet.inflow += amount;
|
||||||
} else {
|
} else {
|
||||||
this.wallet.outflow -= amount;
|
this.wallet.outflow += amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
await saveWallet(this.wallet);
|
await this.saveWallet(this.wallet);
|
||||||
|
|
||||||
|
// Reload data
|
||||||
|
await this.loadData();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load category data
|
* Load category data
|
||||||
* @param {function} getAllCategory Get all category function
|
* @param {function} getAllCategory Get all category function
|
||||||
*/
|
*/
|
||||||
async loadCategory(getAllCategory) {
|
async loadCategory() {
|
||||||
this.listCategory = await getAllCategory();
|
this.listCategory = await this.getAllCategory();
|
||||||
|
|
||||||
if (this.listCategory) {
|
if (this.listCategory) {
|
||||||
this.renderCategoryItem();
|
this.renderCategoryItem();
|
||||||
@@ -146,10 +158,10 @@ export default class HomeView extends CommonView {
|
|||||||
|
|
||||||
const { inflow } = this.wallet;
|
const { inflow } = this.wallet;
|
||||||
const { outflow } = this.wallet;
|
const { outflow } = this.wallet;
|
||||||
const total = inflow - outflow;
|
const total = inflow + outflow;
|
||||||
|
|
||||||
inflowValue.textContent = `+$ ${formatNumber(inflow)}`;
|
inflowValue.textContent = `+$ ${formatNumber(inflow)}`;
|
||||||
outflowValue.textContent = `-$ ${formatNumber(outflow)}`;
|
outflowValue.textContent = `-$ ${formatNumber(Math.abs(outflow))}`;
|
||||||
totalValue.textContent = `${total >= 0 ? '+' : '-'}$ ${formatNumber(
|
totalValue.textContent = `${total >= 0 ? '+' : '-'}$ ${formatNumber(
|
||||||
Math.abs(total),
|
Math.abs(total),
|
||||||
)}`;
|
)}`;
|
||||||
@@ -160,6 +172,9 @@ export default class HomeView extends CommonView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async loadTransactionTab() {
|
async loadTransactionTab() {
|
||||||
|
// Load category
|
||||||
|
await this.loadCategory();
|
||||||
|
|
||||||
// Init data first
|
// Init data first
|
||||||
const transactionDetails = this.loadTransactionDetailsData();
|
const transactionDetails = this.loadTransactionDetailsData();
|
||||||
const transactionEl = document.querySelector('.transaction');
|
const transactionEl = document.querySelector('.transaction');
|
||||||
@@ -176,6 +191,9 @@ export default class HomeView extends CommonView {
|
|||||||
|
|
||||||
listTransactionDetailEl.innerHTML = '';
|
listTransactionDetailEl.innerHTML = '';
|
||||||
listTransactionDetailEl.insertAdjacentHTML('afterbegin', markup.join('\n'));
|
listTransactionDetailEl.insertAdjacentHTML('afterbegin', markup.join('\n'));
|
||||||
|
|
||||||
|
// Load event
|
||||||
|
this.addEventTransactionItem();
|
||||||
}
|
}
|
||||||
|
|
||||||
loadTransactionDetailsData() {
|
loadTransactionDetailsData() {
|
||||||
@@ -283,6 +301,7 @@ export default class HomeView extends CommonView {
|
|||||||
this.transactionDialog.addEventListener('submit', (e) => {
|
this.transactionDialog.addEventListener('submit', (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
this.submitTransactionDialog();
|
this.submitTransactionDialog();
|
||||||
|
this.transactionDialog.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.transactionDialog.addEventListener('input', () => {
|
this.transactionDialog.addEventListener('input', () => {
|
||||||
@@ -320,7 +339,6 @@ export default class HomeView extends CommonView {
|
|||||||
this.showSuccessToast('Delete success!', MESSAGE.DEFAULT_MESSAGE);
|
this.showSuccessToast('Delete success!', MESSAGE.DEFAULT_MESSAGE);
|
||||||
|
|
||||||
await this.loadData();
|
await this.loadData();
|
||||||
this.addEventTransactionItem();
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.showErrorToast(error);
|
this.showErrorToast(error);
|
||||||
}
|
}
|
||||||
@@ -362,7 +380,6 @@ export default class HomeView extends CommonView {
|
|||||||
|
|
||||||
async submitTransactionDialog() {
|
async submitTransactionDialog() {
|
||||||
this.toggleLoaderSpinner();
|
this.toggleLoaderSpinner();
|
||||||
this.transactionDialog.close();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const dateEl = this.transactionForm.querySelector(
|
const dateEl = this.transactionForm.querySelector(
|
||||||
@@ -387,8 +404,9 @@ export default class HomeView extends CommonView {
|
|||||||
|
|
||||||
await this.saveTransaction(transaction);
|
await this.saveTransaction(transaction);
|
||||||
|
|
||||||
// Update wallet info
|
// Reload data
|
||||||
this.updateAmountWallet(-+amountEl.value, this.saveWallet);
|
await this.loadData();
|
||||||
|
this.updateAmountWallet(-+amountEl.value);
|
||||||
|
|
||||||
this.showSuccessToast(
|
this.showSuccessToast(
|
||||||
MESSAGE.ADD_TRANSACTION_SUCCESS,
|
MESSAGE.ADD_TRANSACTION_SUCCESS,
|
||||||
@@ -396,10 +414,6 @@ export default class HomeView extends CommonView {
|
|||||||
);
|
);
|
||||||
|
|
||||||
this.clearInputTransactionForm(this.transactionForm);
|
this.clearInputTransactionForm(this.transactionForm);
|
||||||
|
|
||||||
// Reload data
|
|
||||||
await this.loadData();
|
|
||||||
this.addEventTransactionItem();
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.showErrorToast(error);
|
this.showErrorToast(error);
|
||||||
}
|
}
|
||||||
@@ -511,10 +525,9 @@ export default class HomeView extends CommonView {
|
|||||||
|
|
||||||
await this.saveTransaction(transaction);
|
await this.saveTransaction(transaction);
|
||||||
|
|
||||||
await this.updateAmountWallet(+amount, this.saveWallet); // Update wallet
|
// Reload data
|
||||||
|
|
||||||
await this.loadData();
|
await this.loadData();
|
||||||
this.addEventTransactionItem();
|
await this.updateAmountWallet(+amount); // Update wallet
|
||||||
|
|
||||||
// Hide loader spinner
|
// Hide loader spinner
|
||||||
this.toggleLoaderSpinner();
|
this.toggleLoaderSpinner();
|
||||||
@@ -563,6 +576,7 @@ export default class HomeView extends CommonView {
|
|||||||
this.toggleLoaderSpinner();
|
this.toggleLoaderSpinner();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Wallet info
|
||||||
const { walletForm } = document.forms;
|
const { walletForm } = document.forms;
|
||||||
const form = new FormData(walletForm);
|
const form = new FormData(walletForm);
|
||||||
const walletName = form.get('walletName');
|
const walletName = form.get('walletName');
|
||||||
@@ -577,6 +591,21 @@ export default class HomeView extends CommonView {
|
|||||||
|
|
||||||
await this.saveWallet(wallet);
|
await this.saveWallet(wallet);
|
||||||
|
|
||||||
|
// Transaction info
|
||||||
|
const transaction = new Transaction({
|
||||||
|
categoryName: 'Income',
|
||||||
|
date: new Date().toLocaleDateString('en-US'),
|
||||||
|
note: FIRST_ADD_WALLET_NOTE,
|
||||||
|
amount: +amount,
|
||||||
|
idUser: this.user.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
await this.saveTransaction(transaction);
|
||||||
|
|
||||||
|
// Load data and event
|
||||||
|
await this.loadData();
|
||||||
|
this.loadEvent();
|
||||||
|
|
||||||
this.showSuccessToast(
|
this.showSuccessToast(
|
||||||
MESSAGE.ADD_WALLET_SUCCESS,
|
MESSAGE.ADD_WALLET_SUCCESS,
|
||||||
MESSAGE.DEFAULT_MESSAGE,
|
MESSAGE.DEFAULT_MESSAGE,
|
||||||
@@ -728,9 +757,11 @@ export default class HomeView extends CommonView {
|
|||||||
transactionItemEl.forEach((item) => {
|
transactionItemEl.forEach((item) => {
|
||||||
item.addEventListener('click', (e) => {
|
item.addEventListener('click', (e) => {
|
||||||
const transactionTime = e.target.closest('.transaction__time');
|
const transactionTime = e.target.closest('.transaction__time');
|
||||||
const idTransaction = transactionTime.dataset.id;
|
if (transactionTime) {
|
||||||
|
const idTransaction = transactionTime.dataset.id;
|
||||||
|
|
||||||
this.showTransactionDialog(idTransaction);
|
this.showTransactionDialog(idTransaction);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user