MainScreen.js 8.55 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';

YoonDongMin's avatar
0810DM    
YoonDongMin committed
12

13
const DetailItem = ({ item }) => {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
14
15
16
17
18
19
20
21
    return (
        <>
            <View style={{
                flexDirection: "row", padding: "5%", borderColor: '#d3d3d3', //light grey
                borderWidth: 1, borderTopWidth: 0,
            }}>
                <Text style={[style.itemText, item.type === 1 ? style.inputColor : style.outputColor]}>{item.category}</Text>
                <Text style={[style.itemTextNum, style.Font]}>{item.contents}</Text>
Choi Ga Young's avatar
Choi Ga Young committed
22
                <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
23
24
25
26
27
            </View>
        </>
    );
};

28
const AssetItem = ({ item }) => {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
29
30
31
    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
32
            <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
33
34
35
        </View>
    );
};
Soo Hyun Kim's avatar
Soo Hyun Kim committed
36
37

function MainScreen({ navigation }) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
38
39
40
41
42
    const day = new Date(getDateStr())
    const daysToSubtract = day.getDay()
    const startingDate = new Date(day.getFullYear(), day.getMonth(), day.getDate() - daysToSubtract)
    const endDate = new Date(day.getFullYear(), day.getMonth(), day.getDate() + (6 - daysToSubtract))

43
44
45
    const [modalOpen, setModalOpen] = useState(false);
    const [open, setOpen] = useState(false)

Soo Hyun Kim's avatar
Soo Hyun Kim committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    const [selectedDate, setSelectedDate] = useState(getDateStr())
    const [weeklyData, setWeeklyData] = useState([])
    const [weekMoney, setWeekMoney] = useState({ input: 0, output: 0 })

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

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

    useFocusEffect(
        React.useCallback(() => {
            getWeeklyData(startingDate, endDate)
            getTotalMoney()
        }, [])
60
    );
Soo Hyun Kim's avatar
Soo Hyun Kim committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

    const getTotalMoney = async () => {
        const { total, assets_total } = await weekApi.getTotalData()
        setTotalMoney(total)
        setTotalAssetsMoney(assets_total)
    }

    const getWeeklyData = async (start, end) => {
        const { data, input, output } = await weekApi.getWeeklyData(start, end)
        setWeeklyData(data)
        setWeekMoney({ input: input, output, output })
    }

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

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

Soo Hyun Kim's avatar
Soo Hyun Kim committed
83
84
    return (
        <>
85
86
87
88
            <TouchableWithoutFeedback onPress={() => {
                Keyboard.dismiss();
            }}>
                <View style={{ flex: 1 }}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
89
90
91
92
93
94
                    <WeeklyCalendar
                        selectedDate={selectedDate}
                        startingDate={startingDate}
                        onWeekChanged={(start, end) => getWeeklyData(start, end)}
                        onDateSelected={(date) => setSelectedDate(getDateStr(date))}
                        weeklyData={weeklyData}
95
                    />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
96
97
98
99
100
101
102
103
                    <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>
                                {
104
                                    singleMoney.length !== 0 ?
Soo Hyun Kim's avatar
Soo Hyun Kim committed
105
                                        <View>
106
                                            {singleMoney.length !== 0 && singleMoney.map((item, index) => <DetailItem item={item} key={index} />)}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
107
108
109
110
111
112
113
114
                                        </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
115
                                <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
116
                                <View style={{ marginTop: 5 }}>
117
                                    {totalAssetsMoney.length !== 0 && totalAssetsMoney.map((item, index) => <AssetItem item={item} key={index} />)}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
118
119
120
121
122
123
124
125
126
127
                                </View>
                            </View>
                            <Pressable
                                onPress={() => navigation.navigate('PostMoney')}
                                style={style.submitButton}
                            >
                                <Text style={[style.Font, { color: 'white' }]}>수입/지출 기록</Text>
                            </Pressable>
                        </>
                    </ScrollView>
128
129
                    <SpeedDial
                        isOpen={open}
Choi Ga Young's avatar
Choi Ga Young committed
130
                        icon={{ name: 'edit', color: '#fff' }}
131
                        openIcon={{ name: 'close', color: '#fff' }}
132
133
134
135
                        onOpen={() => setOpen(!open)}
                        onClose={() => setOpen(!open)}
                    >
                        <SpeedDial.Action
YoonDongMin's avatar
0810DM    
YoonDongMin committed
136
                            icon={<FontAwesome name={'money'} size={20} color={'#fff'}/>}
137
138
139
140
                            title="부채"
                            onPress={() => navigation.navigate('DeptPage')}
                        />
                        <SpeedDial.Action
YoonDongMin's avatar
0810DM    
YoonDongMin committed
141
                            icon={{ name: 'note', color: '#fff' }}
142
143
144
145
146
147
148
149
150
151
152
                            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
153
                                style={{ ...style.modalToggle, ...style.modalClose }}
154
155
156
157
158
159
160
161
                                onPress={() => setModalOpen(false)}
                            />
                            <DeptPage />
                        </View>
                    </Modal>

                </View>
            </TouchableWithoutFeedback>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
162
163
164
165
166
        </>
    )
}

const style = StyleSheet.create({
Soo Hyun Kim's avatar
Soo Hyun Kim committed
167
168
169
170
171
172
    weekData: {
        flexDirection: "row",
        justifyContent: "space-around",
        backgroundColor: "#6b768a",
        paddingBottom: 10,
    },
Soo Hyun Kim's avatar
Soo Hyun Kim committed
173
174
175
176
177
178
    TextInput: {
        borderColor: 'skyblue',
        height: 40,
        margin: 12,
        borderWidth: 1
    },
Soo Hyun Kim's avatar
Soo Hyun Kim committed
179
180
181
    totalMoney: {
        fontSize: 35,
    },
Soo Hyun Kim's avatar
Soo Hyun Kim committed
182
    Font: {
Choi Ga Young's avatar
Choi Ga Young committed
183
        fontFamily: 'GowunDodum-Regular',
Soo Hyun Kim's avatar
Soo Hyun Kim committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
        fontSize: 18
    },
    inputColor: {
        color: '#1E90FF'
    },
    outputColor: {
        color: '#dc143c'
    },
    itemTextNum: {
        flex: 1,
        textAlign: "center",
    },
    itemText: {
        flex: 1,
    },
    submitButton: {
        backgroundColor: "#6b768a",
        margin: 10,
        height: 50,
        alignItems: 'center',
        justifyContent: 'center',
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
    },
    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
220
        flex: 1,
221
222
223
    },
    Contents: {
        alignItems: "center",
Soo Hyun Kim's avatar
Soo Hyun Kim committed
224
        margin: 20
Soo Hyun Kim's avatar
Soo Hyun Kim committed
225
226
227
228
    }
});

export default MainScreen;