add parsing and displaying of transactions

This commit is contained in:
2022-03-24 09:50:47 +01:00
parent c0bbe81b02
commit 48c1aac18c
14 changed files with 324 additions and 74 deletions
+9 -2
View File
@@ -1,7 +1,14 @@
import '../styles/globals.css'
import '../styles/globals.scss'
import Script from 'next/script'
import Head from 'next/head'
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
return (
<>
<Script src="https://kit.fontawesome.com/7bb3411482.js" crossorigin="anonymous"/>
<Component {...pageProps } />
</>
)
}
export default MyApp
+60
View File
@@ -3,3 +3,63 @@
export default function handler(req, res) {
res.status(200).json({ name: 'John Doe' })
}
export function parseBeanCount(content) {
// console.log(content)
const lines = content.split(/\r?\n/)
var statements = []
var statement = { parts: [], type: null, _zs: 0 }
// stats
var stats = {}
stats.time = {}
stats.time.interval = [null, null]
for (const [ix, line] of lines.entries()) {
if (line.match(/^\s*$/)) {
if (statement.type !== null) {
if (statement.parts.every(({amount}) => {return amount === ''})) {
throw(`missing transaction amount (:${ix+1})`)
}
statements.push(statement)
statement = { parts: [], type: null, _zs: 0 }
}
} else if (statement.type === null) {
try {
const { date, type, payee, descr } = line.match(/(?<date>\d{4}-\d{2}-\d{2}) (?<type>(txn|\!|\*)) ("|')(?<payee>[\w\s\d]*)("|') ("|')(?<descr>[\w\d\s]*)("|')/).groups
statement.type = type
statement.date = date
statement.payee = payee
statement.descr = descr;
const d = new Date(date)
if (d && stats.time.interval[0] && stats.time.interval) {
stats.time.interval = [(d < stats.time.interval[0]) ? d :stats.time.interval[0], (d > stats.time.interval[1]) ? d : stats.time.interval[1]]
} else if (d) {
stats.time.interval = [d, d]
}
} catch (error) {
console.error(`could not parse an operation (line ${ix + 1})`)
throw(`Could not parse beancount file (:${ix+1}, ${error}])`)
}
} else {
const groups = line.match(/\s+(?<account>[:\w\d]+)(\s+(?<amount>[-.\d]+)\s+(?<currency>\w+))?/).groups
const amount = parseFloat(groups.amount) || null
const currency = groups.currency || null
console.log(amount, statement._zs)
const extract = groups.amount < 0
statement.parts.push({
...groups, amount, extract, currency
})
statement._zs += amount
}
}
stats.time.range = (stats.time.interval[1] - stats.time.interval[0]) / 1000 / 60 / 60 / 24 // now in days
stats.time.interval = [stats.time.interval[0].toISOString().slice(0,10), stats.time.interval[1].toISOString().slice(0,10)]
return { statements, stats }
}
+33 -55
View File
@@ -1,69 +1,47 @@
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
import Head from "next/head";
import Image from "next/image";
import styles from "../styles/Home.module.css";
import { parseBeanCount } from "./api/hello";
import Transaction from "../components/Transaction/Transaction";
import Graph from "../components/Graph/Graph";
import Overview from "../components/Overview/Overview";
export default function Home() {
export default function Home({statements, stats}) {
return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<title></title>
<meta name="description" content="" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>
<p className={styles.description}>
Get started by editing{' '}
<code className={styles.code}>pages/index.js</code>
</p>
<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h2>Documentation &rarr;</h2>
<p>Find in-depth information about Next.js features and API.</p>
</a>
<a href="https://nextjs.org/learn" className={styles.card}>
<h2>Learn &rarr;</h2>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>
<a
href="https://github.com/vercel/next.js/tree/canary/examples"
className={styles.card}
>
<h2>Examples &rarr;</h2>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>
<a
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
>
<h2>Deploy &rarr;</h2>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
<Graph transactions={statements}/>
<Overview transactions={statements} stats={stats} />
{statements && statements.map((item, index) => {
return <Transaction {...item} key={index}/>
})}
</div>
</main>
<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<span className={styles.logo}>
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</span>
</a>
</footer>
</div>
)
);
}
export async function getStaticProps() {
const { statements, stats } = parseBeanCount(`
2014-05-05 txn 'Cafe Mogador' 'Lamb tagine with wine'
Liabilities:CreditCard:CapitalOne -37.5 USD
Expenses:Restaurant
2013-06-04 * 'Cafe A' 'hi'
Liabilities:CapitalOne -37.45 USD
Expenses:Restaurant
`)
return {
props: { statements, stats }
}
}