-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
174 lines (161 loc) · 5.31 KB
/
App.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
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
import _ from 'lodash';
import React, {useRef, useState, useEffect, useMemo, useCallback} from 'react';
import ColorPalette from './ColorPalette';
import {FlatList, StyleSheet, View} from 'react-native';
import IncomeHeader from './components/IncomeHeader';
import {TYPE_EXPENSE, TYPE_INCOME} from './Constants';
import balanceSheet from './styles/balanceSheet.less';
import typography from './styles/typography.less';
import BalanceSheetDate from './components/BalanceSheetDate';
import Button from './components/Button';
import footerStyles from './styles/footer.less';
import Modal from 'react-native-modalbox';
import ExpenseModal from './components/ExpenseModal';
import si from './storage/storage';
import {formatDateMonth} from './utils/dates';
import MonthPicker from 'react-native-month-year-picker';
import IncomeModal from './components/IncomeModal';
const App = () => {
const [editingEntry, setEditingEntry] = useState(null);
const addExpenseModal = useRef(null);
const addIncomeModal = useRef(null);
const [viewingMonth, setViewingMonth] = useState(new Date());
const [showDatePicker, setShowDatePicker] = useState(false);
const showPicker = useCallback(value => setShowDatePicker(value), []);
const [sheetItems, setSheetItems] = useState({});
// Flat list of all items for the header.
const allItems = useMemo(() => _.flatten(_.map(sheetItems, 'items')), [sheetItems]);
useEffect(() => {
si.getEntriesForMonth(viewingMonth).then(entries => {
setSheetItems(entries);
});
}, [viewingMonth]);
const resetViewingMonth = useCallback(
() => setViewingMonth(new Date(viewingMonth)),
[viewingMonth],
);
const submitEntry = useCallback(
data => {
if (_.isNull(editingEntry)) {
si.addEntry({date: new Date(), ...data}).then(() =>
// Setting the viewing month (despite no-op) forces a rerender & fetch.
resetViewingMonth(),
);
} else {
si.editEntry({date: editingEntry.date, ...data}).then(() => resetViewingMonth());
}
},
[resetViewingMonth, editingEntry],
);
const onDateChanged = useCallback(
(event, newDate) => {
const selectedDate = newDate || viewingMonth;
showPicker(false);
setViewingMonth(selectedDate);
},
[showPicker, viewingMonth],
);
return (
<View style={styles.container}>
<IncomeHeader items={allItems} />
<FlatList
style={[balanceSheet.balanceSheet]}
data={sheetItems}
renderItem={({item}) =>
!_.isEmpty(item.items) ? (
<BalanceSheetDate
onPressItem={i => {
setEditingEntry(i);
if (i.type === TYPE_INCOME) {
addIncomeModal.current?.open();
} else {
addExpenseModal.current?.open();
}
}}
item={item}
/>
) : null
}
/>
<View style={footerStyles.footer}>
<Button
style={typography.largest}
text={formatDateMonth(viewingMonth)}
onPress={() => showPicker(true)}
/>
<View style={footerStyles.buttons}>
<Button
onPress={() => addIncomeModal.current?.open()}
style={{marginLeft: 4}}
image={require('./images/piggy-bank-outline.png')}
/>
<Button
onPress={() => addExpenseModal.current?.open()}
style={{marginLeft: 4}}
image={require('./images/receipt-outline.png')}
/>
</View>
</View>
{showDatePicker && (
<MonthPicker
onChange={onDateChanged}
value={viewingMonth}
minimumDate={new Date(1998, 4)}
maximumDate={new Date()}
mode="short"
/>
)}
<Modal
ref={addExpenseModal}
style={{height: 150, borderRadius: 8, backgroundColor: ColorPalette.DARK_GRAY}}
position={'bottom'}
onClosed={_.partial(setEditingEntry, null)}>
<ExpenseModal
entry={editingEntry}
deleteEntry={async entry => {
await si.removeEntry(entry);
resetViewingMonth();
setEditingEntry(null);
addExpenseModal.current?.close();
}}
onSubmit={data => {
submitEntry({type: TYPE_EXPENSE, ...data});
setEditingEntry(null);
addExpenseModal.current?.close();
}}
/>
</Modal>
<Modal
ref={addIncomeModal}
style={{height: 110, borderRadius: 8, backgroundColor: ColorPalette.DARK_GRAY}}
position={'bottom'}
onClosed={_.partial(setEditingEntry, null)}>
<IncomeModal
entry={editingEntry}
deleteEntry={async entry => {
await si.removeEntry(entry);
resetViewingMonth();
setEditingEntry(null);
addIncomeModal.current?.close();
}}
onSubmit={data => {
submitEntry({type: TYPE_INCOME, ...data});
setEditingEntry(null);
addIncomeModal.current?.close();
}}
/>
</Modal>
</View>
);
};
const styles = StyleSheet.create({
container: {
padding: 16,
backgroundColor: ColorPalette.DARKEST_GRAY,
height: '100%',
width: '100%',
display: 'flex',
flexDirection: 'column',
},
});
export default App;