MainScreen.js 10.5 KB
Newer Older
Soo Hyun Kim's avatar
Soo Hyun Kim committed
1
import React, { useEffect, useState } from 'react';
Soo Hyun Kim's avatar
Soo Hyun Kim committed
2
import { View, Text, StyleSheet, Keyboard, TouchableWithoutFeedback, TouchableOpacity, Modal, Pressable, ScrollView } from 'react-native';
3
import { SpeedDial } from 'react-native-elements';
Soo Hyun Kim's avatar
Soo Hyun Kim committed
4
import WeeklyCalendar from './components/WeeklyCalendar';
Choi Ga Young's avatar
Choi Ga Young committed
5
import Ionicons from 'react-native-vector-icons/Ionicons';
Soo Hyun Kim's avatar
Soo Hyun Kim committed
6
import FontAwesome from 'react-native-vector-icons/FontAwesome';
Choi Ga Young's avatar
Choi Ga Young committed
7
import DeptPage from './DeptPage';
Soo Hyun Kim's avatar
Soo Hyun Kim committed
8
9
import weekApi from './db/mainScreen.api';
import { getDateStr } from './utils/dateFunction';
Soo Hyun Kim's avatar
Soo Hyun Kim committed
10
import { useIsFocused } from '@react-navigation/native';
Soo Hyun Kim's avatar
Soo Hyun Kim committed
11

Soo Hyun Kim's avatar
Soo Hyun Kim committed
12
const DetailItem = ({ item, onPress }) => {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
13
14
15
16
17
18
19
20
21
22
23
24
    let item_color
    switch (item.type) {
        case 1:
            item_color = style.inputColor;
            break;
        case 2:
            item_color = style.outputColor;
            break;
        case 3:
            item_color = style.moveColor;
            break;
    }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
25
    return (
Soo Hyun Kim's avatar
Soo Hyun Kim committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
        <TouchableOpacity onPress={onPress}>
            <View style={{
                padding: "5%", borderColor: '#d3d3d3', //light grey
                borderWidth: 1, borderTopWidth: 0,
            }}>
                <View style={{ flexDirection: "row" }}>
                    <Text style={[style.itemText, item_color]}>{item.category}</Text>
                    <Text style={[style.itemTextNum, style.Font]}>{item.contents}</Text>
                    <Text style={[style.itemTextNum, style.Font]}>{(item.price).toLocaleString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}</Text>
                </View>
                {item.asset ?
                    <Text style={[style.itemTextNum, style.Font]}>
                        {'(' + item.asset + '>' + item.deposit_asset + ')'}
                    </Text>
                    : null}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
41
            </View>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
42
        </TouchableOpacity>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
43
44
45
    );
};

46
const AssetItem = ({ item }) => {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
47
48
49
    return (
        <View style={{ flexDirection: "row", width: 180 }}>
            <Text style={[style.Font, { width: "40%", textAlign: 'left' }]}>{item.name}</Text>
Choi Ga Young's avatar
Choi Ga Young committed
50
            <Text style={[style.Font, { width: "60%", textAlign: 'right' }]}>{(item.price).toLocaleString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")} </Text>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
51
52
53
        </View>
    );
};
Soo Hyun Kim's avatar
Soo Hyun Kim committed
54

Soo Hyun Kim's avatar
Soo Hyun Kim committed
55
56
const getMarkedDates = (weeklyData) => {
    if (weeklyData.length === 0) {
Choi Ga Young's avatar
Choi Ga Young committed
57
        return ([]);
Soo Hyun Kim's avatar
Soo Hyun Kim committed
58
59
60
61
62
63
64
65
66
67
    }

    const markedData = weeklyData.map(data => {
        return (
            {
                date: data.date,
                dots: getDots(data.type_id)
            }
        );
    })
Choi Ga Young's avatar
Choi Ga Young committed
68
    return (markedData)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
}

const getDots = (typeArray) => {
    const tempDots = []
    for (let i = 0; i < typeArray.length; i++) {
        if (typeArray[i] === "1") {
            tempDots.push({ color: "#7192d1" })
        } else if (typeArray[i] === '2') {
            tempDots.push({ color: "#d98b79" })
        } else {
            tempDots.push({ color: "white" })
        }
    }
    return tempDots;
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
85
function MainScreen({ navigation }) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
86
87
    const isFocused = useIsFocused();

Soo Hyun Kim's avatar
Soo Hyun Kim committed
88
89
    const day = new Date(getDateStr())
    const daysToSubtract = day.getDay()
Soo Hyun Kim's avatar
Soo Hyun Kim committed
90
91
    const initialStartingDate = new Date(day.getFullYear(), day.getMonth(), day.getDate() - daysToSubtract)
    const initialEndDate = new Date(day.getFullYear(), day.getMonth(), day.getDate() + (6 - daysToSubtract))
Soo Hyun Kim's avatar
Soo Hyun Kim committed
92

93
94
95
    const [modalOpen, setModalOpen] = useState(false);
    const [open, setOpen] = useState(false)

Soo Hyun Kim's avatar
Soo Hyun Kim committed
96
    const [selectedDate, setSelectedDate] = useState(getDateStr())
Soo Hyun Kim's avatar
Soo Hyun Kim committed
97
    const [startingDate, setStartingDate] = useState(initialStartingDate)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
98
    const [weekMoney, setWeekMoney] = useState({ input: 0, output: 0 })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
99
    const [markedDates, setMarkedDates] = useState([])
Soo Hyun Kim's avatar
Soo Hyun Kim committed
100
101
102
103
104
105

    const [singleMoney, setSingleMoney] = useState([])

    const [totalMoney, setTotalMoney] = useState(0)
    const [totalAssetsMoney, setTotalAssetsMoney] = useState([])

Soo Hyun Kim's avatar
Soo Hyun Kim committed
106
    useEffect(() => {
Choi Ga Young's avatar
Choi Ga Young committed
107
108
109
        getthisWeekData(initialStartingDate, initialEndDate);
        getTotalMoney();
        getSingleData();
Soo Hyun Kim's avatar
Soo Hyun Kim committed
110
111
112
113
114
115
    }, [isFocused])

    const getthisWeekData = async (start, end) => {
        const { data, input, output } = await weekApi.getWeeklyData(start, end)
        setStartingDate(start)
        setWeekMoney({ input: input, output, output })
Choi Ga Young's avatar
Choi Ga Young committed
116
117
118
        setTimeout(() => {
            setMarkedDates(getMarkedDates(data))
        }, 300)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
119
    }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
120

Soo Hyun Kim's avatar
Soo Hyun Kim committed
121
122
123
124
125
126
127
128
129
    const getWeeklyData = async (start, end) => {
        if (getDateStr(start) !== getDateStr(startingDate)) {
            const { data, input, output } = await weekApi.getWeeklyData(start, end)
            setStartingDate(start)
            setWeekMoney({ input: input, output, output })
            setMarkedDates(getMarkedDates(data))
        }
    }

Soo Hyun Kim's avatar
Soo Hyun Kim committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    const getTotalMoney = async () => {
        const { total, assets_total } = await weekApi.getTotalData()
        setTotalMoney(total)
        setTotalAssetsMoney(assets_total)
    }

    useEffect(() => {
        getSingleData()
    }, [selectedDate])

    const getSingleData = async () => {
        const tempData = await weekApi.getData(selectedDate)
        setSingleMoney(tempData)
    }

Soo Hyun Kim's avatar
Soo Hyun Kim committed
145
146
    return (
        <>
147
148
149
150
            <TouchableWithoutFeedback onPress={() => {
                Keyboard.dismiss();
            }}>
                <View style={{ flex: 1 }}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
151
152
153
154
                    <WeeklyCalendar
                        selectedDate={selectedDate}
                        startingDate={startingDate}
                        onWeekChanged={(start, end) => getWeeklyData(start, end)}
Choi Ga Young's avatar
Choi Ga Young committed
155
                        onDateSelected={(date) => setSelectedDate(getDateStr(date))}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
156
                        markedDates={markedDates}
157
                    />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
158
159
160
161
162
163
164
165
                    <View style={style.weekData}>
                        <Text style={[style.Font, { color: 'white' }]}>수입 {weekMoney.input}</Text>
                        <Text style={[style.Font, { color: 'white' }]}>지출 {weekMoney.output}</Text>
                    </View>
                    <ScrollView nestedScrollEnabled={true} horizontal={false} >
                        <>
                            <View>
                                {
166
                                    singleMoney.length !== 0 ?
Soo Hyun Kim's avatar
Soo Hyun Kim committed
167
                                        <View>
Choi Ga Young's avatar
Choi Ga Young committed
168
                                            {singleMoney.length !== 0 && singleMoney.map((item, index) => <DetailItem item={item} key={index} onPress={() => navigation.navigate('UpdatePage', [selectedDate, item])} />)}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
169
170
171
172
173
174
175
176
                                        </View>
                                        : <View style={{ marginTop: 10, marginBottom: 50 }}>
                                            <Text style={{ textAlign: "center", fontSize: 20, fontFamily: 'GowunDodum-Regular' }}>내역이 없습니다.</Text>
                                        </View>
                                }
                            </View>
                            <View style={style.Contents}>
                                <Text style={[style.Font, { fontWeight: 'bold' }]}> 자산</Text>
Choi Ga Young's avatar
Choi Ga Young committed
177
                                <Text style={style.totalMoney}><FontAwesome name="won" style={style.totalMoney} /> {(totalMoney).toLocaleString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}</Text>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
178
                                <View style={{ marginTop: 5 }}>
179
                                    {totalAssetsMoney.length !== 0 && totalAssetsMoney.map((item, index) => <AssetItem item={item} key={index} />)}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
180
181
182
183
184
185
186
187
188
189
                                </View>
                            </View>
                            <Pressable
                                onPress={() => navigation.navigate('PostMoney')}
                                style={style.submitButton}
                            >
                                <Text style={[style.Font, { color: 'white' }]}>수입/지출 기록</Text>
                            </Pressable>
                        </>
                    </ScrollView>
190
191
                    <SpeedDial
                        isOpen={open}
Choi Ga Young's avatar
Choi Ga Young committed
192
                        icon={{ name: 'edit', color: '#fff' }}
193
                        openIcon={{ name: 'close', color: '#fff' }}
194
195
196
197
                        onOpen={() => setOpen(!open)}
                        onClose={() => setOpen(!open)}
                    >
                        <SpeedDial.Action
Choi Ga Young's avatar
Choi Ga Young committed
198
                            icon={<FontAwesome name={'money'} size={20} color={'#fff'} />}
199
200
201
202
                            title="부채"
                            onPress={() => navigation.navigate('DeptPage')}
                        />
                        <SpeedDial.Action
YoonDongMin's avatar
0810DM    
YoonDongMin committed
203
                            icon={{ name: 'note', color: '#fff' }}
204
205
206
207
208
209
210
211
212
213
214
                            title="메모"
                            onPress={() => navigation.navigate('MemoPage')}
                        />

                    </SpeedDial>
                    <Modal visible={modalOpen} animationType='slide'>
                        <View style={style.modalContent}>
                            <Ionicons
                                name='close'
                                color='red'
                                size={24}
Choi Ga Young's avatar
Choi Ga Young committed
215
                                style={{ ...style.modalToggle, ...style.modalClose }}
216
217
218
219
220
221
222
223
                                onPress={() => setModalOpen(false)}
                            />
                            <DeptPage />
                        </View>
                    </Modal>

                </View>
            </TouchableWithoutFeedback>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
224
225
226
227
228
        </>
    )
}

const style = StyleSheet.create({
Soo Hyun Kim's avatar
Soo Hyun Kim committed
229
230
231
    weekData: {
        flexDirection: "row",
        justifyContent: "space-around",
Choi Ga Young's avatar
Choi Ga Young committed
232
        backgroundColor: "#adbfdb",
Soo Hyun Kim's avatar
Soo Hyun Kim committed
233
234
        paddingBottom: 10,
    },
Soo Hyun Kim's avatar
Soo Hyun Kim committed
235
236
237
238
239
240
    TextInput: {
        borderColor: 'skyblue',
        height: 40,
        margin: 12,
        borderWidth: 1
    },
Soo Hyun Kim's avatar
Soo Hyun Kim committed
241
242
243
    totalMoney: {
        fontSize: 35,
    },
Soo Hyun Kim's avatar
Soo Hyun Kim committed
244
    Font: {
Choi Ga Young's avatar
Choi Ga Young committed
245
        fontFamily: 'GowunDodum-Regular',
Soo Hyun Kim's avatar
Soo Hyun Kim committed
246
247
248
249
250
251
252
253
        fontSize: 18
    },
    inputColor: {
        color: '#1E90FF'
    },
    outputColor: {
        color: '#dc143c'
    },
Soo Hyun Kim's avatar
Soo Hyun Kim committed
254
255
256
    moveColor: {
        color: '#646d75'
    },
Soo Hyun Kim's avatar
Soo Hyun Kim committed
257
258
259
260
261
262
263
264
    itemTextNum: {
        flex: 1,
        textAlign: "center",
    },
    itemText: {
        flex: 1,
    },
    submitButton: {
Choi Ga Young's avatar
Choi Ga Young committed
265
        backgroundColor: '#bfd3f2',
Soo Hyun Kim's avatar
Soo Hyun Kim committed
266
267
268
269
        margin: 10,
        height: 50,
        alignItems: 'center',
        justifyContent: 'center',
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
    },
    modalToggle: {

        padding: 10,
        borderRadius: 10,
        alignSelf: 'flex-start',
        marginTop: -40, //위치를 center로
    },
    modalClose: {
        alignSelf: 'center',
        alignItems: 'flex-start',
        marginTop: 150,
        marginBottom: 50,
    },
    modalContent: {
Choi Ga Young's avatar
Choi Ga Young committed
285
        flex: 1,
286
287
288
    },
    Contents: {
        alignItems: "center",
Soo Hyun Kim's avatar
Soo Hyun Kim committed
289
        margin: 20
Soo Hyun Kim's avatar
Soo Hyun Kim committed
290
291
292
293
    }
});

export default MainScreen;