-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
46 lines (42 loc) · 1.64 KB
/
store.js
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
import Dexie from 'dexie';
import { proxy } from 'valtio';
import { derive } from 'valtio/utils';
import { amountReducer, getTransactions, sortDateDesc, encryptData } from './utils/helpers';
export const state = proxy({
transactions: [],
session: null,
selectedTransaction: null,
selectedTransactions: null,
sessionModal: false,
addTransactionsModal: false,
newSessionModal: false,
editModal: false,
bulkEditModal: false,
downloadModal: false,
});
derive({
totalExpenses: (get) => get(state).transactions.reduce(amountReducer, 0),
accounts: (get) => getTransactions(get(state).transactions, 'Account').sort((a, b) => b.sum - a.sum),
months: (get) => getTransactions(get(state).transactions, 'Month'),
monthlyAverage: (get) => {
let monthlyTotals = 0;
get(state).months.forEach((month) => {
monthlyTotals += month.sum;
});
return monthlyTotals / get(state).months.length || 0;
},
categories: (get) => getTransactions(get(state).transactions, 'Category').sort((a, b) => b.sum - a.sum)
}, { proxy: state });
export const db = new Dexie('ExpenseReportDB');
db.version(1).stores({
sessions: '++id,sessionName,*transactions,created,modified'
});
export const updateSession = async ({ transactions, session, sessionName, callback }) => {
const sortedTransactions = transactions.map((t) => ({ ...t })).sort(sortDateDesc);
const encryptedData = await encryptData(sortedTransactions);
const updatedSesh = { sessionName, transactions: encryptedData, modified: new Date() };
db.sessions.update(session.id, updatedSesh).then(() => {
state.session = { ...session, ...updatedSesh };
if (callback) callback();
});
};