Calendar.js 5.89 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
2
import React, { useState, useEffect } from 'react';
import { SafeAreaView, StyleSheet, View, Text, FlatList, TouchableOpacity } from 'react-native';
Choi Ga Young's avatar
Choi Ga Young committed
3
import { Button } from 'react-native-elements';
Choi Ga Young's avatar
Choi Ga Young committed
4
import Ionicons from 'react-native-vector-icons/Ionicons';
Choi Ga Young's avatar
Choi Ga Young committed
5
6

const DateItem = ({ dateitem, textColor, onPress, flatListHeight }) => {
Choi Ga Young's avatar
Choi Ga Young committed
7
  return (
Choi Ga Young's avatar
Choi Ga Young committed
8
    <TouchableOpacity onPress={onPress}
Choi Ga Young's avatar
Choi Ga Young committed
9
      style={[style.dateContainer, { height: flatListHeight }]}>
Choi Ga Young's avatar
Choi Ga Young committed
10
      <Text style={textColor}>{dateitem.date.getDate()}</Text>
Choi Ga Young's avatar
Choi Ga Young committed
11
12
13
14
15
16
17
      {
        dateitem.type &&
        <View>
          <Text style={{ color: '#1E90FF' }}>{dateitem.type.input ? dateitem.type.input : null}</Text>
          <Text style={{ color: '#DC143C' }}>{dateitem.type.output ? dateitem.type.output : null}</Text>
        </View>
      }
Choi Ga Young's avatar
Choi Ga Young committed
18
19
20
    </TouchableOpacity>
  );
};
Choi Ga Young's avatar
Choi Ga Young committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const INIT_Data = [
  {
    date: "2021-06-28",
    type: { 'input': 10000, 'output': 1000 },
  },
  {
    date: "2021-07-01",
    type: { 'input': 50000, 'output': 3500 },
  },
  {
    date: "2021-07-11",
    type: { 'input': 100000, 'output': 2000 },
  },
  {
    date: "2021-08-14",
    type: { 'input': 15000, 'output': 2010 },
  },
];
Choi Ga Young's avatar
Choi Ga Young committed
39

40
41
42
43
44
45
46
47
48
49
50
function Calendar({
  navigation,
  MData,
  year,
  setYear,
  month,
  setMonth,
  todayM,
  todayY,
}) {
  //console.log('MData', MData)
Choi Ga Young's avatar
Choi Ga Young committed
51
  const date = new Date();
Choi Ga Young's avatar
Choi Ga Young committed
52
  const [flatListHeight, setFlatListHeight] = useState(400)
Choi Ga Young's avatar
Choi Ga Young committed
53
54
55
56
57
58
59
60
61
62
63

  const prevLast = new Date(year, month, 0); //이전 달의 마지막 날
  const thisFirst = new Date(year, month, 1); //이번 달의 첫째 날
  const thisLast = new Date(year, month + 1, 0); //이번 달의 마지막 날

  const thisFirstDay = thisFirst.getDay(); //이번 달 첫번 째 요일
  const prevLastDate = prevLast.getDate(); //이전 달 마지막 날짜
  const thisLastDate = thisLast.getDate(); //이번 달 마지막 날짜
  const thisLastDay = thisLast.getDay(); //이번 달 마지막 요일

  // 이번 달 달력에 쓰일 날짜들. 이전달 다음달 전부 포함
Choi Ga Young's avatar
Choi Ga Young committed
64
65
  const Dates = [];
  const DBDates = [];
Choi Ga Young's avatar
Choi Ga Young committed
66
  if (thisFirstDay !== 0) { // 첫째 날이 일요일이 아니라면
Choi Ga Young's avatar
Choi Ga Young committed
67
    for (let i = 0; i < thisFirstDay; i++) {
Choi Ga Young's avatar
Choi Ga Young committed
68
      Dates.unshift({ date: new Date(year, month - 1, prevLastDate - i) })
Choi Ga Young's avatar
Choi Ga Young committed
69
    }
Choi Ga Young's avatar
Choi Ga Young committed
70
71
  }
  for (let i = 1; i <= thisLastDate; i++) { //이번 달 날짜 
Choi Ga Young's avatar
Choi Ga Young committed
72
    Dates.push({ date: new Date(year, month, i) })
Choi Ga Young's avatar
Choi Ga Young committed
73
74
  }
  for (let i = 1; i <= 6 - thisLastDay; i++) {
Choi Ga Young's avatar
Choi Ga Young committed
75
    Dates.push({ date: new Date(year, month + 1, i) })
Choi Ga Young's avatar
Choi Ga Young committed
76
  }
Choi Ga Young's avatar
Choi Ga Young committed
77

Choi Ga Young's avatar
Choi Ga Young committed
78
  //DB 데이터와 날짜 비교
Choi Ga Young's avatar
Choi Ga Young committed
79
80
81
  for (let i = 0; i < MData.length; i++) {
    const str = MData[i].date
    DBDates.push({ date: new Date(Number(str.slice(0, 4)), Number(str.slice(5, 7)) - 1, Number(str.slice(8, 10))), type: MData[i].type })
Choi Ga Young's avatar
Choi Ga Young committed
82
83
  }

Choi Ga Young's avatar
Choi Ga Young committed
84
  for (let i = 0; i < Dates.length; i++) {
Choi Ga Young's avatar
Choi Ga Young committed
85
    const idx = DBDates.findIndex(obj => obj.date.getTime() === Dates[i].date.getTime())
Choi Ga Young's avatar
Choi Ga Young committed
86
87
88
89
    if (idx !== -1) {
      Dates[i] = DBDates[idx]
    }
  }
Choi Ga Young's avatar
Choi Ga Young committed
90
91
92
93
94

  let istoday = false;

  const renderDate = ({ item }) => {
    let color = '#000000';
Choi Ga Young's avatar
Choi Ga Young committed
95
    if (item.date.getDate() === date.getDate() && month === todayM && year === todayY) {
Choi Ga Young's avatar
Choi Ga Young committed
96
97
98
99
100
101
      istoday = true;
    }
    if (istoday) {
      color = '#6495ed'
      istoday = false;
    }
Choi Ga Young's avatar
Choi Ga Young committed
102
    if (!(item.date.getMonth() === month)) {
Choi Ga Young's avatar
Choi Ga Young committed
103
104
      color = '#a9a9a9'
    }
Choi Ga Young's avatar
Choi Ga Young committed
105

Choi Ga Young's avatar
Choi Ga Young committed
106
    return (
Choi Ga Young's avatar
Choi Ga Young committed
107
      <DateItem dateitem={item} textColor={{ color }} onPress={() => navigation.navigate('CalDetail', String(item.date.toJSON()).split(/T/)[0])} flatListHeight={flatListHeight / (Dates.length / 7)} />
Choi Ga Young's avatar
Choi Ga Young committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    )
  }
  const prevBtn = () => {
    if (month < 1) {
      setYear(year - 1)
      setMonth(11)
    } else {
      setMonth(month - 1)
    }
  };
  const nextBtn = () => {
    if (month > 10) {
      setYear(year + 1)
      setMonth(0)
    } else {
      setMonth(month + 1)
    }
  }
  useEffect(() => {
    setMonth(todayM)
    setYear(todayY)
  }, [])

Choi Ga Young's avatar
Choi Ga Young committed
131
132
  const onLayout = (event) => {
    const { x, y, height, width } = event.nativeEvent.layout;
Choi Ga Young's avatar
Choi Ga Young committed
133
134
    setFlatListHeight(height)
  }
Choi Ga Young's avatar
Choi Ga Young committed
135
136
  return (
    <>
Choi Ga Young's avatar
Choi Ga Young committed
137
      <SafeAreaView style={style.EntContainer}>
Choi Ga Young's avatar
Choi Ga Young committed
138
139
140
141
142
143
144
145
146
147
148
149
        <View style={style.Container}>
          <Text style={style.Head}>{year}  {month + 1} </Text>
          <View style={style.Buttons}>
            <Button icon={
              <Ionicons name='chevron-back-sharp' onPress={prevBtn} size={15} color='black' />
            } type='clear' />
            <Button title='Today' onPress={() => { setMonth(todayM); setYear(todayY) }} type='clear' />
            <Button icon={
              <Ionicons name='chevron-forward-sharp' onPress={nextBtn} size={15} color='black' />
            } type='clear' />
          </View>
        </View>
Choi Ga Young's avatar
Choi Ga Young committed
150
151
152
153
154
155
156
157
158
        <View style={style.Days}>
          <Text style={style.DayText}></Text>
          <Text style={style.DayText}></Text>
          <Text style={style.DayText}></Text>
          <Text style={style.DayText}></Text>
          <Text style={style.DayText}></Text>
          <Text style={style.DayText}></Text>
          <Text style={style.DayText}></Text>
        </View>
Choi Ga Young's avatar
Choi Ga Young committed
159
        <FlatList
Choi Ga Young's avatar
Choi Ga Young committed
160
          style={{ backgroundColor: "#FFFFFF" }}
Choi Ga Young's avatar
Choi Ga Young committed
161
          onLayout={onLayout}
Choi Ga Young's avatar
Choi Ga Young committed
162
          data={Dates}
Choi Ga Young's avatar
Choi Ga Young committed
163
164
          numColumns={7}
          renderItem={renderDate}
Choi Ga Young's avatar
Choi Ga Young committed
165
          keyExtractor={item => item.date}
Choi Ga Young's avatar
Choi Ga Young committed
166
        />
Choi Ga Young's avatar
Choi Ga Young committed
167
168
169
      </SafeAreaView>
    </>
  )
Choi Ga Young's avatar
Choi Ga Young committed
170
};
Choi Ga Young's avatar
Choi Ga Young committed
171
172

const style = StyleSheet.create({
Choi Ga Young's avatar
Choi Ga Young committed
173
174
175
176
177
  EntContainer: { //전체 (SafeAreaView)
    flex: 1,
    padding: 20
  },
  Head: { //00년00월
Choi Ga Young's avatar
Choi Ga Young committed
178
    fontSize: 24,
Choi Ga Young's avatar
Choi Ga Young committed
179
    fontFamily: 'GowunDodum-Regular'
Choi Ga Young's avatar
Choi Ga Young committed
180
  },
Choi Ga Young's avatar
Choi Ga Young committed
181
  Container: { //년월, 버튼 뷰에 적용
Choi Ga Young's avatar
Choi Ga Young committed
182
183
184
    justifyContent: "center",
    alignItems: "center",
  },
Choi Ga Young's avatar
Choi Ga Young committed
185
186
187
188
  dateContainer: { //각 date에 적용 (Text 태그), DateItem 컴포넌트에 적용
    flex: 1,
    borderWidth: 0.8,
    borderColor: '#DCDCDC'
Choi Ga Young's avatar
Choi Ga Young committed
189
190
191
192
193
194
  },
  Buttons: {
    flexDirection: 'row',
    margin: 10
  },
  Days: {
Choi Ga Young's avatar
Choi Ga Young committed
195
196
    flexDirection: 'row',
    backgroundColor: '#6495ED'
Choi Ga Young's avatar
Choi Ga Young committed
197

Choi Ga Young's avatar
Choi Ga Young committed
198
  },
Choi Ga Young's avatar
Choi Ga Young committed
199
200
  DayText: {
    flex: 1,
Choi Ga Young's avatar
Choi Ga Young committed
201
    textAlign: 'center',
Choi Ga Young's avatar
Choi Ga Young committed
202
203
204
    color: '#FFFFFF',
    fontFamily: 'GowunDodum-Regular',
    fontWeight: "bold"
Choi Ga Young's avatar
Choi Ga Young committed
205
  },
Choi Ga Young's avatar
Choi Ga Young committed
206
207
208
});

export default Calendar;