diff --git a/Dockerfile b/Dockerfile index 9e42103..f2b005d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,3 @@ FROM nginx:alpine -COPY index.html styles.css app.js /usr/share/nginx/html/ +COPY index.html styles.css app.js manifest.json sw.js icon-192.png icon-512.png /usr/share/nginx/html/ EXPOSE 80 diff --git a/README.md b/README.md index 09e97a1..0b89d9d 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,12 @@ To use another port: `PORT=8080 python3 server.py`. ## Features - **Sidebar views** — Daily, Weekly, Monthly and All expenses with live counts, each group with its own total. -- **Categories** — Food, Transport, Household, Utilities, Shopping, Health, Beauty, Entertainment, Other. +- **Categories** — Food, Transport, Household, Utilities, Shopping, Health, Beauty, Entertainment, Other. Drag them up or down to rearrange; the order is remembered. +- **Period totals** — the sidebar footer shows total spending for today, this week or this month at a tap. - **Repeats** — mark an expense Daily, Weekly or Monthly. Repeating entries appear under “Repeating expenses” with the next due date and a one-click **Log next occurrence** button. - **Summary** — totals plus a per-category spending breakdown for whatever is shown. - **INR amounts** — everything is shown in rupees (₹) with Indian digit grouping. -- **Search and filters** — by text, category and repeat type. +- **Search and filters** — by text and category. - **Edit / delete** — every entry can be changed or removed. - **Sample data** — realistic entries are loaded on first run so each view has content. Use **Clear all** to start empty and **Load sample data** to add them back. @@ -38,6 +39,11 @@ Cloudflare caches `app.js`/`styles.css` at the edge, so every deploy that changes them must bump the `?v=` query in `index.html` or visitors keep getting the old files until the edge entry expires. +The app is also a PWA: `sw.js` caches the shell for offline use and shows an +"Update to latest version" button when a deploy ships. Every deploy that +changes the app must bump the `CACHE` name in `sw.js` (`tally-v3`, `tally-v4`, +…) or installed clients never see the update. + ## Files - `index.html` — page structure diff --git a/app.js b/app.js index 034ea45..dfa6cd1 100644 --- a/app.js +++ b/app.js @@ -18,8 +18,37 @@ var CATS = { var FREQ_LABEL = { once: "One-time", daily: "Daily", weekly: "Weekly", monthly: "Monthly" }; var STORE_KEY = "expense-tracker.expenses.v1"; +var ORDER_KEY = "expense-tracker.catorder.v1"; -var state = { tab: "daily", q: "", cat: "all", freq: "all", editingId: null, screen: "add", selMonth: null, selYear: null, selWeek: null, selDate: null }; +/* Category order, persisted so drag-reorder survives reloads. Unknown saved + names are dropped; new categories append in their default position. */ +function getCatOrder() { + var names = Object.keys(CATS); + try { + var saved = JSON.parse(localStorage.getItem(ORDER_KEY) || "[]"); + if (!Array.isArray(saved)) return names; + var known = saved.filter(function (n) { return CATS[n] !== undefined; }); + names.forEach(function (n) { if (known.indexOf(n) === -1) known.push(n); }); + return known; + } catch (e) { + return names; + } +} + +function saveCatOrder(names) { + try { localStorage.setItem(ORDER_KEY, JSON.stringify(names)); } catch (e) { /* session-only order */ } +} + +function refillCatOptions() { + var sel = $("fCat"); + var cur = sel.value; + sel.innerHTML = getCatOrder().map(function (c) { + return '"; + }).join(""); + sel.value = cur; +} + +var state = { tab: "daily", q: "", cat: "all", period: "month", editingId: null, screen: "add", selMonth: null, selYear: null, selWeek: null, selDate: null }; var MONTHS_SHORT = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; /* All amounts display in INR. */ @@ -170,7 +199,6 @@ function filtered(list) { if (mondayOf(e.date) !== state.selWeek) return false; } if (state.cat !== "all" && e.category !== state.cat) return false; - if (state.freq !== "all" && e.frequency !== state.freq) return false; if (q && (e.description + " " + e.category).toLowerCase().indexOf(q) === -1) return false; return true; }); @@ -335,23 +363,77 @@ function renderNav(list) { $("countWeekly").textContent = c.weekly; $("countMonthly").textContent = c.monthly; $("countAll").textContent = c.all; - var names = Object.keys(CATS); + var names = getCatOrder(); var html = '
"; html += names.map(function (n) { - return '"; }).join(""); $("catList").innerHTML = html; - var m = 0; - list.forEach(function (e) { if (e.date.slice(0, 7) === mo) m += e.amount; }); - $("sideMonth").textContent = money.format(m); + var d = 0, w = 0, m = 0; + list.forEach(function (e) { + if (e.date === t) d += e.amount; + if (mondayOf(e.date) === wk) w += e.amount; + if (e.date.slice(0, 7) === mo) m += e.amount; + }); + var labels = { day: "Today", week: "This week", month: "This month" }; + var totals = { day: d, week: w, month: m }; + $("periodLabel").textContent = labels[state.period] || labels.month; + $("sideMonth").textContent = money.format(totals[state.period] === undefined ? m : totals[state.period]); + Array.prototype.forEach.call(document.querySelectorAll("[data-period]"), function (b) { + b.classList.toggle("active", b.getAttribute("data-period") === state.period); + }); $("topMonth").textContent = money.format(m); } +/* Drag-to-reorder for the sidebar categories. Only the named categories move; + "All categories" stays pinned at the top. */ +var dragName = null; +function wireCatDnD() { + var list = $("catList"); + list.addEventListener("dragstart", function (e) { + var li = e.target.closest("li[data-order]"); + if (!li) { e.preventDefault(); return; } + dragName = li.getAttribute("data-order"); + e.dataTransfer.effectAllowed = "move"; + try { e.dataTransfer.setData("text/plain", dragName); } catch (err) { /* some browsers need the try */ } + li.classList.add("dragging"); + }); + list.addEventListener("dragend", function () { + dragName = null; + Array.prototype.forEach.call(list.querySelectorAll(".dragging, .drag-over"), function (el) { + el.classList.remove("dragging", "drag-over"); + }); + }); + list.addEventListener("dragover", function (e) { + var li = e.target.closest("li[data-order]"); + if (!li || !dragName) return; + e.preventDefault(); + e.dataTransfer.dropEffect = "move"; + Array.prototype.forEach.call(list.querySelectorAll(".drag-over"), function (el) { + if (el !== li) el.classList.remove("drag-over"); + }); + li.classList.add("drag-over"); + }); + list.addEventListener("drop", function (e) { + var li = e.target.closest("li[data-order]"); + if (!li || !dragName) return; + e.preventDefault(); + var target = li.getAttribute("data-order"); + if (target === dragName) return; + var names = getCatOrder(); + names.splice(names.indexOf(dragName), 1); + names.splice(names.indexOf(target), 0, dragName); + saveCatOrder(names); + refillCatOptions(); + renderAll(); + }); +} + /* Landing shows only the add form; records live behind the menu drawer. */ var RECORD_SECTIONS = ["viewHead", "statsSection", "sumCard", "listSection", "templatesCard"]; @@ -489,10 +571,7 @@ function showErr(msg) { } function init() { - var catSel = $("fCat"); - catSel.innerHTML = Object.keys(CATS).map(function (c) { - return '"; - }).join(""); + refillCatOptions(); $("fDate").value = todayStr(); document.querySelectorAll(".nav-item[data-tab]").forEach(function (btn) { @@ -591,7 +670,13 @@ function init() { } $("datePicker").addEventListener("change", function (e) { pickDateInput(e.target.closest("[data-dinput]")); }); $("datePicker").addEventListener("input", function (e) { pickDateInput(e.target.closest("[data-dinput]")); }); - $("fFreqFilter").addEventListener("change", function (e) { state.freq = e.target.value; renderAll(); }); + document.querySelector(".period-pick").addEventListener("click", function (e) { + var b = e.target.closest("[data-period]"); + if (!b) return; + state.period = b.getAttribute("data-period"); + renderAll(); + }); + wireCatDnD(); $("expForm").addEventListener("submit", function (e) { e.preventDefault(); diff --git a/expo-app/App.js b/expo-app/App.js index e5a6276..1bf4bd3 100644 --- a/expo-app/App.js +++ b/expo-app/App.js @@ -9,7 +9,7 @@ import ExpenseForm from "./src/components/ExpenseForm"; import ExpenseGroups from "./src/components/ExpenseGroups"; import RepeatTemplates from "./src/components/RepeatTemplates"; import SummaryBars from "./src/components/SummaryBars"; -import { CATEGORIES } from "./src/categories"; +import { CATEGORIES, applyCatOrder } from "./src/categories"; import { addDaysStr, groupKey, @@ -21,7 +21,7 @@ import { todayStr, uid, } from "./src/logic"; -import { loadExpenses, saveExpenses } from "./src/storage"; +import { loadExpenses, saveExpenses, loadCatOrder, saveCatOrder } from "./src/storage"; import s from "./src/styles"; const TABS = ["daily", "weekly", "monthly"]; @@ -37,7 +37,9 @@ export default function App() { const [tab, setTab] = useState("daily"); const [query, setQuery] = useState(""); const [catFilter, setCatFilter] = useState("all"); - const [freqFilter, setFreqFilter] = useState("all"); + const [period, setPeriod] = useState("month"); + const [catOrder, setCatOrder] = useState(null); + const [arranging, setArranging] = useState(false); const [editingId, setEditingId] = useState(null); const [pendingDeleteId, setPendingDeleteId] = useState(null); const currency = "INR"; @@ -52,6 +54,7 @@ export default function App() { setExpenses(list); setReady(true); }); + loadCatOrder().then(setCatOrder); }, []); function update(list) { @@ -103,11 +106,10 @@ export default function App() { const q = query.trim().toLowerCase(); return expenses.filter((e) => { if (catFilter !== "all" && e.category !== catFilter) return false; - if (freqFilter !== "all" && e.frequency !== freqFilter) return false; if (q && (e.description + " " + e.category).toLowerCase().indexOf(q) === -1) return false; return true; }); - }, [expenses, query, catFilter, freqFilter]); + }, [expenses, query, catFilter]); const groups = useMemo(() => { const map = {}; @@ -144,6 +146,21 @@ export default function App() { return { d, w, m, counts: { daily: dc, weekly: wc, monthly: mc }, catCounts: cats }; }, [expenses]); + const orderedCats = useMemo(() => applyCatOrder(CATEGORIES, catOrder), [catOrder]); + const periodTotal = period === "day" ? stats.d : period === "week" ? stats.w : stats.m; + const periodNoun = period === "day" ? "today" : period === "week" ? "this week" : "this month"; + + function moveCat(index, dir) { + const names = orderedCats.map((c) => c.name); + const j = index + dir; + if (j < 0 || j >= names.length) return; + const t = names[index]; + names[index] = names[j]; + names[j] = t; + setCatOrder(names); + saveCatOrder(names); + } + function editExpense(id) { setEditingId(id); setPendingDeleteId(null); @@ -161,7 +178,7 @@ export default function App() {