MainScreen.js 8.36 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
25
26
    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>
                <Text style={[style.itemTextNum, style.Font]}>{item.price}</Text>
            </View>
        </>
    );
};

27
const AssetItem = ({ item }) => {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
28
29
30
31
32
33
34
    return (
        <View style={{ flexDirection: "row", width: 180 }}>
            <Text style={[style.Font, { width: "40%", textAlign: 'left' }]}>{item.name}</Text>
            <Text style={[style.Font, { width: "60%", textAlign: 'right' }]}>{item.price} </Text>
        </View>
    );
};
Soo Hyun Kim's avatar
Soo Hyun Kim committed
35
36

function MainScreen({ navigation }) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
37
38
39
40
41
    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))

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

Soo Hyun Kim's avatar
Soo Hyun Kim committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    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()
        }, [])
59
    );
Soo Hyun Kim's avatar
Soo Hyun Kim committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

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

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

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

export default MainScreen;