Skip to content

Commit

Permalink
Restructure pages, fix transaction sum calculation and update `lo…
Browse files Browse the repository at this point in the history
…ading component`
  • Loading branch information
pratikkabade committed Mar 8, 2024
1 parent 6ecdfd8 commit 7a8cbe1
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 26 deletions.
28 changes: 24 additions & 4 deletions src/components/LoadingComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,39 @@ export const LoadingComponent = () => {
// fetch url from local storage
const url = localStorage.getItem('theURL') || ''

console.log(URL)
// change chn id
function change() {
setTimeout(() => {
document.getElementById('chn1')!.innerHTML = 'Something went wrong..'
document.getElementById('chn2')!.innerHTML = 'Please check the link again'
}, 5000)
}

change()

if (url === '') {
return (
<div className="container my-32 flex flex-wrap flex-col mx-auto justify-center items-center text-center text-4xl font-bold">
<h1>Seems like you haven't added the URL</h1>
<h1>Seems like you haven't added the URL</h1>
</div>
)
}
else {
return (
<div className="container my-32 flex flex-wrap flex-col mx-auto justify-center items-center text-center text-4xl font-bold">
<h1>Something went <span className="text-red-800 dark:text-red-300">wrong</span>...</h1>
<h1>Please check the link again</h1>
<div id="chn1" className="flex flex-row align-center">Loading

<div role="status" className="mx-3 mt-0.5">
<svg aria-hidden="true" className="w-8 h-8 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor" />
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill" />
</svg>
<span className="sr-only">Loading...</span>
</div>


</div>
<div id="chn2"></div>
</div>
)
}
Expand Down
30 changes: 21 additions & 9 deletions src/pages/Accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ export const Accounts = () => {
const [account, setAccount] = useState<any>(acName)
const [fetching, setFetch] = useState<boolean>(true)

// fetch the url from browser
var currentLocation = window.location.href;
console.log(currentLocation);

// use axios
function fetchData() {
axios.get(url)
Expand All @@ -42,9 +38,7 @@ export const Accounts = () => {
)
}


// only show 100 transactions
const currentData = data.transactions.slice(0, 100)
const currentData = data.transactions.filter((item: any) => item.type !== "TRANSFER")

// get name from accounts array using id from transactions
currentData.map((item: any) => {
Expand All @@ -56,6 +50,8 @@ export const Accounts = () => {
item.accountColor = color.color
item.accountCurrency = currency.currency
item.accountIcon = icon.icon

return item
})

// get name form categories array using id from transactions
Expand All @@ -66,6 +62,8 @@ export const Accounts = () => {
item.category = category.name
item.color = color.color
item.icon = icon.icon

return item
})

// show all accounts
Expand All @@ -77,10 +75,24 @@ export const Accounts = () => {
setAccount(nm)
}

// find the sum of all transaction in the account
const sum = currentData.filter((item: any) => item.accountName === account)
.reduce((acc: any, item: any) => acc + item.amount, 0)


return (
<div className="container mb-32 mx-auto justify-center items-center">
<div className='flex flex-wrap flex-row'>
{sum === 0 ? (
<div className="text-4xl font-bold text-center my-10">
No transactions found
</div>
) : (

<div className="text-4xl font-bold text-center my-10">
Total: {sum} {currentData[0].accountCurrency}
</div>
)}
{
accounts.map((item: any) => (
<div className="p-5 lg:w-48 md:w-72 m-1 bg-slate-100 dark:bg-slate-800 rounded-2xl hover:shadow-lg cursor-pointer hover:brightness-110 transition duration-300 ease-in-out"
Expand Down Expand Up @@ -126,7 +138,7 @@ export const Accounts = () => {
</p>

<div className="flex flex-row flex-wrap justify-around text-lg font-bold my-5">
<Link to={`/Category/${item.category}`}
<Link to={`/Category/`}
onClick={() => localStorage.setItem('category', item.category)}>
<p key={item.id + 'category'}
className="bg-green-100 dark:bg-green-800 p-2 px-10 m-1 rounded-full hover:shadow-md cursor-pointer">
Expand All @@ -135,7 +147,7 @@ export const Accounts = () => {
{item.category}
</p>
</Link>
<Link to={`/Accounts/${item.accountName}`}
<Link to={`/Accounts/`}
onClick={() => localStorage.setItem('account', item.accountName)}>
<p key={item.id + 'account name'}
className="bg-white p-2 dark:bg-gray-900 px-10 m-1 rounded-full hover:shadow-md cursor-pointer">
Expand Down
34 changes: 27 additions & 7 deletions src/pages/Category.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const Category = () => {

const [data, setData] = useState<any>([])
const [group, setGroup] = useState<any>(nm)
const [fetching, setFetch] = useState<boolean>(true)

// use axios
function fetchData() {
Expand All @@ -22,10 +23,11 @@ export const Category = () => {
.catch(err => {
console.log(err)
})
setFetch(false)
}

useEffect(() => {
fetchData()
fetching && fetchData()
})

// load page after data is fetched
Expand All @@ -35,20 +37,21 @@ export const Category = () => {
)
}


// only show 100 transactions
const currentData = data.transactions.slice(0, 100)
const currentData = data.transactions.filter((item: any) => item.type !== "TRANSFER")

// get name from accounts array using id from transactions
currentData.map((item: any) => {
const account = data.accounts.find((account: any) => account.id === item.accountId)
const color = data.accounts.find((account: any) => account.id === item.accountId)
const currency = data.accounts.find((account: any) => account.id === item.accountId)
const icon = data.accounts.find((account: any) => account.id === item.accountId)

item.accountName = account.name
item.accountColor = color.color
item.accountCurrency = currency.currency
item.accountIcon = icon.icon

return item
})

// get name form categories array using id from transactions
Expand All @@ -59,6 +62,8 @@ export const Category = () => {
item.category = category.name
item.color = color.color
item.icon = icon.icon

return item
})

// show all categories
Expand All @@ -69,11 +74,26 @@ export const Category = () => {
localStorage.setItem('category', nm)
setGroup(nm)
}

// get sum of transactions
const sum = currentData.filter((item: any) => item.category === group)
.map((item: any) => item.amount)
.reduce((acc: any, item: any) => acc + item, 0)


return (
<div className="container mb-32 mx-auto justify-center items-center">
<div className='flex flex-wrap flex-row'>
{sum === 0 ? (
<div className="text-4xl font-bold text-center my-10">
No transactions found
</div>
) : (

<div className="text-4xl font-bold text-center my-10">
Total: {sum} {currentData[0].accountCurrency}
</div>
)}
{
categories.map((item: any) => (
<div className="p-5 lg:w-48 md:w-72 m-1 bg-slate-100 dark:bg-slate-800 rounded-2xl hover:shadow-lg cursor-pointer hover:brightness-110 transition duration-300 ease-in-out"
Expand Down Expand Up @@ -118,7 +138,7 @@ export const Category = () => {
</p>

<div className="flex flex-row flex-wrap justify-around text-lg font-bold my-5">
<Link to={`/Category/${item.category}`}
<Link to={`/Category/`}
onClick={() => localStorage.setItem('category', item.category)}>
<p key={item.id + 'category'}
className="bg-green-100 dark:bg-green-800 p-2 px-10 m-1 rounded-full hover:shadow-md cursor-pointer">
Expand All @@ -127,7 +147,7 @@ export const Category = () => {
{item.category}
</p>
</Link>
<Link to={`/Accounts/${item.accountName}`}
<Link to={`/Accounts/`}
onClick={() => localStorage.setItem('account', item.accountName)}>
<p key={item.id + 'account name'}
className="bg-white p-2 dark:bg-gray-900 px-10 m-1 rounded-full hover:shadow-md cursor-pointer">
Expand Down
19 changes: 14 additions & 5 deletions src/components/Dashboard.tsx → src/pages/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import axios from 'axios'
import React, { useEffect, useState } from 'react'
import { getDate, getDay } from '../hooks/Functions'
import { PlusButton } from './add-functionalities/PlusButton'
import { LoadingComponent } from './LoadingComponent'
import { PlusButton } from '../components/add-functionalities/PlusButton'
import { LoadingComponent } from '../components/LoadingComponent'
import { Link } from 'react-router-dom'

export const Dashboard = () => {
const [data, setData] = useState<any>([])
const [fetching, setFetch] = useState<boolean>(true)

// fetch url from local storage
const url = localStorage.getItem('theURL') || ''
Expand All @@ -20,10 +21,11 @@ export const Dashboard = () => {
.catch(err => {
console.log(err)
})
setFetch(false)
}

useEffect(() => {
fetchData()
fetching && fetchData()
})

// load page after data is fetched
Expand All @@ -33,6 +35,9 @@ export const Dashboard = () => {
)
}

const username = data.settings[0].name
localStorage.setItem('username', username)
// console.log(username)

// only show 100 transactions
const currentData = data.transactions.slice(0, 100)
Expand All @@ -47,6 +52,8 @@ export const Dashboard = () => {
item.accountColor = color.color
item.accountCurrency = currency.currency
item.accountIcon = icon.icon

return item
})

// get name form categories array using id from transactions
Expand All @@ -57,6 +64,8 @@ export const Dashboard = () => {
item.category = category.name
item.color = color.color
item.icon = icon.icon

return item
})


Expand All @@ -79,7 +88,7 @@ export const Dashboard = () => {
</p>

<div className="flex flex-row flex-wrap justify-around text-lg font-bold my-5">
<Link to={`/Category/${item.category}`}
<Link to={`/Category/`}
onClick={() => localStorage.setItem('category', item.category)}>
<p key={item.id + 'category'}
className="bg-green-100 dark:bg-green-800 p-2 px-10 m-1 rounded-full hover:shadow-md cursor-pointer">
Expand All @@ -88,7 +97,7 @@ export const Dashboard = () => {
{item.category}
</p>
</Link>
<Link to={`/Accounts/${item.accountName}`}
<Link to={`/Accounts/`}
onClick={() => localStorage.setItem('account', item.accountName)}>
<p key={item.id + 'account name'}
className="bg-white p-2 dark:bg-gray-900 px-10 m-1 rounded-full hover:shadow-md cursor-pointer">
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from "react"
import { Tutorial } from "../components/Tutorial"
import { Dashboard } from "../components/Dashboard"
import { Dashboard } from "./Dashboard"
import { Button } from "flowbite-react"

export const Home = () => {
Expand Down

0 comments on commit 7a8cbe1

Please sign in to comment.