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

12
const DetailItem = ({ item }) => {
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
        <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>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
32
                <Text style={[style.itemTextNum, style.Font]}>{item.contents}</Text>
Choi Ga Young's avatar
Choi Ga Young committed
33
                <Text style={[style.itemTextNum, style.Font]}>{(item.price).toLocaleString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}</Text>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
34
            </View>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
35
36
37
38
39
40
            {item.asset ?
                <Text style={[style.itemTextNum, style.Font]}>
                    {'(' + item.asset + '>' + item.deposit_asset + ')'}
                </Text>
                : null}
        </View>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
41
42
43
    );
};

44
const AssetItem = ({ item }) => {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
45
46
47
    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
48
            <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
49
50
51
        </View>
    );
};
Soo Hyun Kim's avatar
Soo Hyun Kim committed
52

Soo Hyun Kim's avatar
Soo Hyun Kim committed
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
const getMarkedDates = (weeklyData) => {
    if (weeklyData.length === 0) {
        return null;
    }

    const markedData = weeklyData.map(data => {
        return (
            {
                date: data.date,
                dots: getDots(data.type_id)
            }
        );
    })

    return markedData
}

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
84
function MainScreen({ navigation }) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
85
86
    const day = new Date(getDateStr())
    const daysToSubtract = day.getDay()
Soo Hyun Kim's avatar
Soo Hyun Kim committed
87
88
    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
89

90
91
92
    const [modalOpen, setModalOpen] = useState(false);
    const [open, setOpen] = useState(false)

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

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

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

    useFocusEffect(
        React.useCallback(() => {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
106
            getWeeklyData(initialStartingDate, initialEndDate)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
107
108
            getTotalMoney()
        }, [])
109
    );
Soo Hyun Kim's avatar
Soo Hyun Kim committed
110

Soo Hyun Kim's avatar
Soo Hyun Kim committed
111
112
113
114
115
116
117
118
119
120
    const getWeeklyData = async (start, end) => {
        if (getDateStr(start) !== getDateStr(startingDate)) {
            console.log('weekchanged~~~~~~~~~~~~', start, end)
            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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    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
136
137
    return (
        <>
138
139
140
141
            <TouchableWithoutFeedback onPress={() => {
                Keyboard.dismiss();
            }}>
                <View style={{ flex: 1 }}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
142
143
144
145
146
147
148
149
150
                    {/* <Pressable onPress={() => {
                        let startD = new Date(startingDate.getFullYear(), startingDate.getMonth(), startingDate.getDate() - 7)
                        let endD = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate() - 7)
                        getWeeklyData(startD, endD);
                        setStartingDate(startD)
                        setEndDate(endD)
                    }}>
                        <Text>테스트</Text>
                    </Pressable> */}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
151
152
153
154
                    <WeeklyCalendar
                        selectedDate={selectedDate}
                        startingDate={startingDate}
                        onWeekChanged={(start, end) => getWeeklyData(start, end)}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
155
156
                        onDateSelected={(date) => setSelectedDate(date)}
                        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>
168
                                            {singleMoney.length !== 0 && singleMoney.map((item, index) => <DetailItem item={item} key={index} />)}
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
                        onOpen={() => setOpen(!open)}
                        onClose={() => setOpen(!open)}
                    >
                        <SpeedDial.Action
                            icon={{ name: 'add', color: '#fff' }}
                            title="부채"
                            onPress={() => navigation.navigate('DeptPage')}
                        />
                        <SpeedDial.Action
                            icon={{ name: 'add', color: '#fff' }}
                            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",
Soo Hyun Kim's avatar
Soo Hyun Kim 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
265
266
267
268
269
    itemTextNum: {
        flex: 1,
        textAlign: "center",
    },
    itemText: {
        flex: 1,
    },
    submitButton: {
        backgroundColor: "#6b768a",
        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;