-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathJSONdata.tsx
175 lines (147 loc) · 7.35 KB
/
JSONdata.tsx
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import axios from "axios"
import { useEffect, useState } from "react"
import { getMonth, getYear } from "../utils/DateFunctions"
export const JSONdata = () => {
const [url, setUrl] = useState<string>(localStorage.getItem('theURL') || "")
const [allow, setAllow] = useState<boolean>(false)
const [data, setData] = useState<any>([])
const [fetching, setFetch] = useState<boolean>(true)
// use Axios to fetch data
function fetchData() {
axios.get(url)
.then(res => {
setData(res.data)
})
.catch(err => {
console.log(err)
})
setFetch(false)
}
// load data if url is not empty
useEffect(() => {
if (url !== '') {
setAllow(true)
}
}, [url])
fetching && fetchData()
if (data.transactions !== undefined) {
// ACCOUNTS array, calculate SUM from all transactions
const accountData = data.accounts.map((account: any) => {
const sum = data.transactions.filter((item: any) => item.accountId === account.id)
.filter((item: any) => item.dateTime !== undefined) // items with NO DATETIME
.map((item: any) => item.type === "INCOME" ? item.amount : item.type === "EXPENSE" ? -item.amount : 0) // EXPENSE or INCOME items
.reduce((acc: any, item: any) => acc + item, 0) // reduce to SUM
account.sum = sum
return account
})
// CATEGORIES array, calculate SUM from all
const categoryData = data.categories.map((category: any) => {
const sum = data.transactions.filter((item: any) => item.categoryId === category.id)
.filter((item: any) => item.dateTime !== undefined) // items with NO DATETIME
.map((item: any) => item.type === "INCOME" ? item.amount : item.type === "EXPENSE" ? -item.amount : 0) // EXPENSE or INCOME items
.reduce((acc: any, item: any) => acc + item, 0) // reduce to SUM
category.sum = sum
return category
})
// SAVE ACCOUNTS, CATEGORIES to local storage
localStorage.setItem("accountData", JSON.stringify(accountData))
localStorage.setItem("categoryData", JSON.stringify(categoryData))
}
if (data.transactions !== undefined) {
const transactionsData = data.transactions.filter((item: any) => item.type !== "TRANSFER")
// get name from ACCOUNTS array using ID from TRANSACTIONS
transactionsData.map((item: any) => {
const account = data.accounts.find((account: any) => account.id === item.accountId)
item.accountName = account.name
item.accountColor = account.color
item.accountCurrency = account.currency
item.accountIcon = account.icon
return item
})
// get name from CATEGORIES array using ID from TRANSACTIONS
transactionsData.map((item: any) => {
const category = data.categories.find((category: any) => category.id === item.categoryId)
item.categoryName = category.name
item.categoryColor = category.color
item.categoryIcon = category.icon
return item
})
// filter out items with NO DATETIME
transactionsData.filter((item: any) => item.dateTime !== undefined)
// get YEAR and MONTH from DATETIME
transactionsData.map((item: any) => {
const dt = item.dateTime
if (dt === undefined) {
item.transactionYear = "N/A"
item.transactionMonth = "N/A"
return item
}
item.transactionYear = getYear(dt)
item.transactionMonth = getMonth(dt)
return item
})
const username = data.settings[0].name
localStorage.setItem('username', username)
}
const saveToLocalStorage = () => {
localStorage.setItem("theURL", url)
localStorage.setItem("theData", JSON.stringify(data))
}
if (data.transactions !== undefined) {
saveToLocalStorage()
}
return (
<div className="text-black dark:text-white">
<p className="text-xl my-2">Your Data</p>
{
allow ?
<div className="w-4/5">
<input
className="w-full mx-2 px-3 py-1.5 text-base font-normal text-gray-800 dark:text-gray-300 bg-clip-padding border focus:border-2 border-solid border-blue-100 dark:border-gray-700 transition ease-in-out m-0 focus:text-gray-900 dark:focus:text-gray-200 focus:border-blue-600 focus:outline-none bg-gray-100 dark:bg-gray-800 shadow-md hover:shadow-lg rounded-lg hover:rounded-lg focus:rounded-lg"
type="text"
value={url}
onChange={(e) => {
setUrl(e.target.value)
localStorage.setItem("theURL", url)
}} />
</div>
:
<div className="w-4/5">
<input disabled
className="w-full mx-2 px-3 py-1.5 text-base font-normal text-red-800 dark:text-red-300 bg-clip-padding border focus:border-2 border-solid border-blue-100 dark:border-red-700 m-0 focus:text-red-900 dark:focus:text-red-200 focus:border-blue-600 focus:outline-none bg-red-100 dark:bg-red-900 shadow-md rounded-lg focus:rounded-lg hover:cursor-not-allowed"
type="text"
/>
</div>
}
<label className="m-3 mb-5">
<div className="flex flex-row justify-center align-middle items-center">
<input type="checkbox"
className="m-3"
onChange={() => {
setAllow(!allow)
setUrl('')
}}
checked={allow} />
<div className="block dark:text-white">
By clicking here, you agree
<button
className="text-sky-700 dark:text-sky-300 hover:brightness-150 hover:underline mx-2">
ivy-wallet-web
</button>
can store cookies on your device.
</div>
{
allow ?
<button onClick={saveToLocalStorage} type="button" className="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-2 py-1 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800 w-fit h-fit">
Save
</button>
:
<button disabled type="button" className="text-white bg-blue-500 hover:bg-blue-600 focus:ring-4 focus:ring-blue-100 font-medium rounded-lg text-sm px-2 py-1 dark:bg-blue-600 dark:hover:bg-blue-500 focus:outline-none dark:focus:ring-blue-600 w-fit h-fit hover:cursor-not-allowed">
Save
</button>
}
</div>
</label>
</div>
)
}