MainScreen.js 8.54 KB
Newer Older
Soo Hyun Kim's avatar
Soo Hyun Kim committed
1
2
3
import React, { useEffect, useState } from 'react';
import { View, Text, TextInput, StyleSheet, Button, Keyboard, TouchableWithoutFeedback, Modal, Pressable, FlatList, ScrollView } from 'react-native';
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
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import weekApi from './db/mainScreen.api';
import { getDateStr } from './utils/dateFunction';

const DetailItem = ({item}) => {
    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>
        </>
    );
};

const AssetItem = ({item}) => {
    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
    const [modalOpen, setModalOpen] = useState(false);
Soo Hyun Kim's avatar
Soo Hyun Kim committed
43

44
45
    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
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 [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()
        }, [])
      );

    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 })
        console.log('=============week changed')
    }

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

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

Soo Hyun Kim's avatar
Soo Hyun Kim committed
84
85
    return (
        <>
86
87
88
89
            <TouchableWithoutFeedback onPress={() => {
                Keyboard.dismiss();
            }}>
                <View style={{ flex: 1 }}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
90
91
92
93
94
95
                    <WeeklyCalendar
                        selectedDate={selectedDate}
                        startingDate={startingDate}
                        onWeekChanged={(start, end) => getWeeklyData(start, end)}
                        onDateSelected={(date) => setSelectedDate(getDateStr(date))}
                        weeklyData={weeklyData}
96
                    />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
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
                    <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>
                                {
                                    singleMoney.length != 0 ?
                                        <View>
                                            {singleMoney.length !== 0 && singleMoney.map((item, index) => <DetailItem item={item} key={index}/>)}
                                        </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 }}>
                                    {totalAssetsMoney.length !== 0 && totalAssetsMoney.map((item, index) => <AssetItem item={item} key={index}/>)}
                                </View>
                            </View>
                            <Pressable
                                onPress={() => navigation.navigate('PostMoney')}
                                style={style.submitButton}
                            >
                                <Text style={[style.Font, { color: 'white' }]}>수입/지출 기록</Text>
                            </Pressable>
                        </>
                    </ScrollView>
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
                    <SpeedDial
                        isOpen={open}
                        icon={{ name: 'edit', color: '#fff' }} //연필모양
                        openIcon={{ name: 'close', color: '#fff' }} //x모양
                        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}
                                style={{ ...style.modalToggle, ...style.modalClose }} //...은 중괄호를 풀어서 합치려고 이용함
                                onPress={() => setModalOpen(false)}
                            />
                            <DeptPage />
                        </View>
                    </Modal>

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

167

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

        padding: 10,
        borderRadius: 10,
        alignSelf: 'flex-start',
        marginTop: -40, //위치를 center로
    },
    modalClose: {
        alignSelf: 'center',
        alignItems: 'flex-start',
        marginTop: 150,
        marginBottom: 50,
    },
    modalContent: {
        flex: 1, //이후 유용한 키보드를 추가하려고 ex)dismissing keyboard
    },
    Contents: {
        alignItems: "center",
Soo Hyun Kim's avatar
Soo Hyun Kim committed
225
        margin: 20
Soo Hyun Kim's avatar
Soo Hyun Kim committed
226
227
228
229
    }
});

export default MainScreen;