File size: 4,984 Bytes
1503282 afa50fd 1503282 afa50fd 1503282 afa50fd 1503282 afa50fd 1503282 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | // Sample data - in a real app, this would come from a database
let transactions = [
{
id: 1,
date: '2023-11-15',
time: '10:30 AM',
description: 'Vegetables for home',
amount: 350,
type: 'expense',
person: 'Mother',
balance: 4650
},
{
id: 2,
date: '2023-11-14',
time: '5:45 PM',
description: 'Grocery shopping',
amount: 1200,
type: 'expense',
person: 'Father',
balance: 5000
},
{
id: 3,
date: '2023-11-12',
time: '12:15 PM',
description: 'Received money',
amount: 2000,
type: 'income',
person: 'Brother',
balance: 6200
}
];
let familyMembers = [
{ id: 1, name: 'Anubhav', balance: 0, avatar: 'http://static.photos/people/200x200/1' },
{ id: 2, name: 'Anidhya', balance: 0, avatar: 'http://static.photos/people/200x200/2' },
{ id: 3, name: 'Mumma', balance: 0, avatar: 'http://static.photos/people/200x200/3' },
{ id: 4, name: 'Papa Ji', balance: 0, avatar: 'http://static.photos/people/200x200/4' }
];
// Calculate current balance
function calculateBalance() {
return transactions.reduce((total, transaction) => {
return transaction.type === 'income' ? total + transaction.amount : total - transaction.amount;
}, 0);
}
// Load initial data
function loadInitialData() {
setupDarkMode();
// Update balance
const balance = calculateBalance();
document.getElementById('current-balance').textContent = `₹${balance.toFixed(2)}`;
// Load recent transactions (last 3)
const recentTransactions = transactions.slice(0, 3);
const transactionsContainer = document.getElementById('recent-transactions');
recentTransactions.forEach(transaction => {
const transactionEl = document.createElement('div');
transactionEl.className = 'transaction-card bg-gray-50 p-4 rounded-md';
transactionEl.innerHTML = `
<div class="flex justify-between items-center">
<div>
<p class="font-medium">${transaction.description}</p>
<p class="text-sm text-gray-500">${transaction.person} • ${transaction.date} ${transaction.time}</p>
</div>
<p class="font-semibold ${transaction.type === 'income' ? 'text-primary' : 'text-red-500'}">
${transaction.type === 'income' ? '+' : '-'}₹${transaction.amount.toFixed(2)}
</p>
</div>
`;
transactionsContainer.appendChild(transactionEl);
});
// Load family members
const familyContainer = document.getElementById('family-members');
familyMembers.forEach(member => {
const memberEl = document.createElement('div');
memberEl.className = 'family-member-card bg-gray-50 p-4 rounded-md text-center';
memberEl.innerHTML = `
<img src="${member.avatar}" alt="${member.name}" class="w-12 h-12 rounded-full mx-auto mb-2">
<p class="font-medium">${member.name}</p>
<p class="text-sm ${member.balance > 0 ? 'text-primary' : member.balance < 0 ? 'text-red-500' : 'text-gray-500'}">
${member.balance > 0 ? 'You owe' : member.balance < 0 ? 'Owes you' : 'Settled'} ${member.balance !== 0 ? '₹' + Math.abs(member.balance).toFixed(2) : ''}
</p>
`;
familyContainer.appendChild(memberEl);
});
}
// Dark mode toggle
function setupDarkMode() {
const themeToggle = document.getElementById('theme-toggle');
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const currentTheme = localStorage.getItem('theme') || (prefersDark ? 'dark' : 'light');
if (currentTheme === 'dark') {
document.documentElement.setAttribute('data-theme', 'dark');
themeToggle.innerHTML = '<i data-feather="sun" class="w-5 h-5"></i>';
} else {
document.documentElement.removeAttribute('data-theme');
themeToggle.innerHTML = '<i data-feather="moon" class="w-5 h-5"></i>';
}
feather.replace();
themeToggle.addEventListener('click', () => {
const currentTheme = document.documentElement.getAttribute('data-theme');
if (currentTheme === 'dark') {
document.documentElement.removeAttribute('data-theme');
localStorage.setItem('theme', 'light');
themeToggle.innerHTML = '<i data-feather="moon" class="w-5 h-5"></i>';
} else {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark');
themeToggle.innerHTML = '<i data-feather="sun" class="w-5 h-5"></i>';
}
feather.replace();
});
}
// Shared functions
function formatDate(dateString) {
const options = { year: 'numeric', month: 'short', day: 'numeric' };
return new Date(dateString).toLocaleDateString('en-US', options);
} |