|
1 | | -import { format, parse, parseISO } from 'date-fns'; |
2 | | -import * as store from '../store'; |
3 | | -import * as actions from './../features/business/businessSlice'; |
4 | | -import * as uiactions from './../features/ui/uiSlice'; |
5 | | -import db from '../db'; |
6 | | -import { ReqRes } from '../../types'; |
7 | | - |
8 | | -const historyController = { |
9 | | - addHistoryToIndexedDb(reqRes: ReqRes): void { |
10 | | - db.table('history') |
11 | | - .put(reqRes) |
12 | | - .catch((err: string) => console.log('Error in addHistoryToIndexedDb', err)); |
13 | | - }, |
14 | | - |
15 | | - deleteHistoryFromIndexedDb(id: string): void { |
16 | | - db.table('history') |
17 | | - .delete(id) |
18 | | - .catch((err: string) => console.log('Error in deleteFromHistory', err)); |
19 | | - }, |
20 | | - |
21 | | - clearHistoryFromIndexedDb(): void { |
22 | | - db.table('history').clear().catch((err: string) => console.log(err)); |
23 | | - }, |
24 | | - |
25 | | - async getHistory(): Promise<void> { |
26 | | - try { |
27 | | - const history: ReqRes[] = await db.table('history').toArray() |
28 | | - const historyGroupsObj = history.reduce((groups: Record<string, ReqRes[]>, hist: ReqRes) => { |
29 | | - const date = format(hist.createdAt, 'MM/dd/yyyy'); |
30 | | - if (!groups[date]) { |
31 | | - groups[date] = []; |
32 | | - } |
33 | | - groups[date].push(hist); |
34 | | - return groups; |
35 | | - }, {}); |
36 | | - const historyGroupsArr = Object.keys(historyGroupsObj) |
37 | | - .sort((a, b) => parse(b, 'MM/dd/yyyy', new Date()).valueOf() - parse(a, 'MM/dd/yyyy', new Date()).valueOf()) |
38 | | - .map((date: string) => ({ // this returns an array of objects with K:date T:string and K:array of history objects |
39 | | - date, |
40 | | - history: historyGroupsObj[date].sort( |
41 | | - (a: ReqRes, b: ReqRes) => b.createdAt.valueOf() - a.createdAt.valueOf()), |
42 | | - })); |
43 | | - console.log(historyGroupsArr) |
44 | | - store.default.dispatch(actions.getHistory(historyGroupsArr)); |
45 | | - } catch { |
46 | | - ((err: string) => console.log('Error in getHistory', err)) |
47 | | - }; |
48 | | - } |
49 | | -}; |
50 | | - |
51 | | -export default historyController; |
52 | | - |
53 | | - |
| 1 | +import { format, parse, parseISO } from 'date-fns'; |
| 2 | +import * as store from '../store'; |
| 3 | +import * as actions from './../features/business/businessSlice'; |
| 4 | +import * as uiactions from './../features/ui/uiSlice'; |
| 5 | +import db from '../db'; |
| 6 | +import { ReqRes } from '../../types'; |
| 7 | + |
| 8 | +const historyController = { |
| 9 | + addHistoryToIndexedDb(reqRes: ReqRes): void { |
| 10 | + db.table('history') |
| 11 | + .put(reqRes) |
| 12 | + .catch((err: string) => console.log('Error in addHistoryToIndexedDb', err)); |
| 13 | + }, |
| 14 | + |
| 15 | + deleteHistoryFromIndexedDb(id: string): void { |
| 16 | + db.table('history') |
| 17 | + .delete(id) |
| 18 | + .catch((err: string) => console.log('Error in deleteFromHistory', err)); |
| 19 | + }, |
| 20 | + |
| 21 | + clearHistoryFromIndexedDb(): void { |
| 22 | + db.table('history').clear().catch((err: string) => console.log(err)); |
| 23 | + }, |
| 24 | + |
| 25 | + async getHistory(): Promise<void> { |
| 26 | + try { |
| 27 | + const history: ReqRes[] = await db.table('history').toArray() |
| 28 | + const historyGroupsObj = history.reduce((groups: Record<string, ReqRes[]>, hist: ReqRes) => { |
| 29 | + const date = format(hist.createdAt, 'MM/dd/yyyy'); |
| 30 | + if (!groups[date]) { |
| 31 | + groups[date] = []; |
| 32 | + } |
| 33 | + groups[date].push(hist); |
| 34 | + return groups; |
| 35 | + }, {}); |
| 36 | + const historyGroupsArr = Object.keys(historyGroupsObj) |
| 37 | + .sort((a, b) => parse(b, 'MM/dd/yyyy', new Date()).valueOf() - parse(a, 'MM/dd/yyyy', new Date()).valueOf()) |
| 38 | + .map((date: string) => ({ // this returns an array of objects with K:date T:string and K:array of history objects |
| 39 | + date, |
| 40 | + history: historyGroupsObj[date].sort( |
| 41 | + (a: ReqRes, b: ReqRes) => b.createdAt.valueOf() - a.createdAt.valueOf()), |
| 42 | + })); |
| 43 | + store.default.dispatch(actions.getHistory(historyGroupsArr)); |
| 44 | + } catch { |
| 45 | + ((err: string) => console.log('Error in getHistory', err)) |
| 46 | + }; |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +export default historyController; |
| 51 | + |
| 52 | + |
0 commit comments