Calendar.js 7.3 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
4
5
import { Button, normalize } from 'react-native-elements';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
Choi Ga Young's avatar
Choi Ga Young committed
6
import Ionicons from 'react-native-vector-icons/Ionicons';
Choi Ga Young's avatar
Choi Ga Young committed
7
import DetailInfo from './DetailInfo';
Choi Ga Young's avatar
Choi Ga Young committed
8

Choi Ga Young's avatar
Choi Ga Young committed
9
10
11
const Stack = createStackNavigator();

const DateItem = ({ dateitem, textColor, onPress, flatListHeight }) => {
Choi Ga Young's avatar
Choi Ga Young committed
12
  return (
Choi Ga Young's avatar
Choi Ga Young committed
13
    <TouchableOpacity onPress={onPress}
Choi Ga Young's avatar
Choi Ga Young committed
14
15
16
17
18
19
20
21
22
      style={[style.dateContainer, { height: flatListHeight }]}>
      <Text style={textColor}>{dateitem.idates}</Text>
      {
        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
23
24
25
    </TouchableOpacity>
  );
};
Choi Ga Young's avatar
Choi Ga Young committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
44

Choi Ga Young's avatar
Choi Ga Young committed
45
46
47
48
49
50
51
52
53
54
55
const dateTostr = (date) => {
  //console.log('didididid', date)
  const dsy = date.getFullYear();
  const dsm = ('0' + (date.getMonth() + 1)).slice(-2);
  const dsd = ('0' + date.getDate()).slice(-2);
  //console.log('return', dsy + '-' + dsm + '-' + dsd)
  return dsy + '-' + dsm + '-' + dsd;

}

function Calendar({ navigation }) {
Choi Ga Young's avatar
Choi Ga Young committed
56
57
58
59
60
  const date = new Date();
  const [year, setYear] = useState(date.getFullYear());
  const [month, setMonth] = useState(date.getMonth());
  const todayM = date.getMonth();
  const todayY = date.getFullYear();
Choi Ga Young's avatar
Choi Ga Young committed
61
  const [flatListHeight, setFlatListHeight] = useState(400)
Choi Ga Young's avatar
Choi Ga Young committed
62
63
64
65
66
67
68
69
70
71
72
73

  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(); //이번 달 마지막 요일

  // 이번 달 달력에 쓰일 날짜들. 이전달 다음달 전부 포함
  const tempDates = [];
Choi Ga Young's avatar
Choi Ga Young committed
74
  const testArr = [];
Choi Ga Young's avatar
Choi Ga Young committed
75
76
77
  if (thisFirstDay != 0) { // 첫째 날이 일요일이 아니라면
    for (let i = 0; i < thisFirstDay; i++) {
      tempDates.unshift(prevLastDate - i);
Choi Ga Young's avatar
Choi Ga Young committed
78
79
80
81
      //testArr.unshift({ ay: year, am: month, ad: prevLastDate - i });
      const re1 = dateTostr(new Date(year, month - 1, prevLastDate - i))
      testArr.unshift({ fd: re1, sd: prevLastDate - i })
    }
Choi Ga Young's avatar
Choi Ga Young committed
82
83
84
  }
  for (let i = 1; i <= thisLastDate; i++) { //이번 달 날짜 
    tempDates.push(i);
Choi Ga Young's avatar
Choi Ga Young committed
85
86
87
    //testArr.push({ ay: year, am: month + 1, ad: i });
    const re2 = dateTostr(new Date(year, month, i))
    testArr.push({ fd: re2, sd: i })
Choi Ga Young's avatar
Choi Ga Young committed
88
89
90
  }
  for (let i = 1; i <= 6 - thisLastDay; i++) {
    tempDates.push(i);
Choi Ga Young's avatar
Choi Ga Young committed
91
92
93
    //testArr.push({ ay: year, am: month + 2, ad: i });
    const re3 = dateTostr(new Date(year, month + 1, i))
    testArr.push({ fd: re3, sd: i})
Choi Ga Young's avatar
Choi Ga Young committed
94
  }
Choi Ga Young's avatar
Choi Ga Young committed
95
  console.log('testArr', testArr);
Choi Ga Young's avatar
Choi Ga Young committed
96
97
  const dates = tempDates.map((element, index) => ({ key: `${index}`, idates: element }));

Choi Ga Young's avatar
Choi Ga Young committed
98
99
100
101
102
103
104
105
106
107
108
109
  const testDates = testArr.map((obj, index) => ({ key: `${index}`, tdates: obj.sd }));
  console.log('testDates', testDates)

  const testID = dates.findIndex(obj => obj.idates == 28);
  if (testID !== -1) {
    //dates[testID] = { key: `${dates[testID].key}`, idates: dates[testID].idates, type: 'input', price: 10000 };
    dates[testID] = { key: `${dates[testID].key}`, idates: dates[testID].idates, type: { "input": 10000, "output": 1000 } };
    //console.log('타입확인', dates[testID]); //타입확인 {"idates": 28, "key": "1", "type": {"input": 10000, "output": 1000}}
    //console.log('type', Object.keys(dates[testID].type)); //type ["input", "output"]
    console.log('a', dates[testID].type.input)
  }

Choi Ga Young's avatar
Choi Ga Young committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  const thisfirstID = tempDates.indexOf(1); // 이번 달 1일
  const thislastID = tempDates.lastIndexOf(thisLastDate); // 이번 달 마지막 날짜 인덱스

  let istoday = false;

  const renderDate = ({ item }) => {
    let color = '#000000';
    if (item.idates === date.getDate() && month === todayM && year === todayY) {
      istoday = true;
    }
    if (istoday) {
      color = '#6495ed'
      istoday = false;
    }
    if (!(item.key >= thisfirstID && item.key < thislastID + 1)) {
      color = '#a9a9a9'
    }
    return (
Choi Ga Young's avatar
Choi Ga Young committed
128
      <DateItem dateitem={item} textColor={{ color }} onPress={() => navigation.navigate('DetailInfo')} flatListHeight={flatListHeight / 5} />
Choi Ga Young's avatar
Choi Ga Young committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    )
  }
  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
152
153
  const onLayout = (event) => {
    const { x, y, height, width } = event.nativeEvent.layout;
Choi Ga Young's avatar
Choi Ga Young committed
154
155
    setFlatListHeight(height)
  }
Choi Ga Young's avatar
Choi Ga Young committed
156
157
  return (
    <>
Choi Ga Young's avatar
Choi Ga Young committed
158
      <SafeAreaView style={style.EntContainer}>
Choi Ga Young's avatar
Choi Ga Young committed
159
160
161
162
163
164
165
166
167
168
169
170
        <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
171
172
173
174
175
176
177
178
179
        <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
180
181
        <FlatList
          onLayout={onLayout}
Choi Ga Young's avatar
Choi Ga Young committed
182
183
184
          data={dates}
          numColumns={7}
          renderItem={renderDate}
Choi Ga Young's avatar
Choi Ga Young committed
185
186
187
188
        />
        <Stack.Navigator>
          <Stack.Screen name="DetailInfo" component={DetailInfo} />
        </Stack.Navigator>
Choi Ga Young's avatar
Choi Ga Young committed
189
190
191
      </SafeAreaView>
    </>
  )
Choi Ga Young's avatar
Choi Ga Young committed
192
};
Choi Ga Young's avatar
Choi Ga Young committed
193
194

const style = StyleSheet.create({
Choi Ga Young's avatar
Choi Ga Young committed
195
196
197
198
199
  EntContainer: { //전체 (SafeAreaView)
    flex: 1,
    padding: 20
  },
  Head: { //00년00월
Choi Ga Young's avatar
Choi Ga Young committed
200
201
    fontSize: 24,
  },
Choi Ga Young's avatar
Choi Ga Young committed
202
  Container: { //년월, 버튼 뷰에 적용
Choi Ga Young's avatar
Choi Ga Young committed
203
204
205
    justifyContent: "center",
    alignItems: "center",
  },
Choi Ga Young's avatar
Choi Ga Young committed
206
207
208
209
  dateContainer: { //각 date에 적용 (Text 태그), DateItem 컴포넌트에 적용
    flex: 1,
    borderWidth: 0.8,
    borderColor: '#DCDCDC'
Choi Ga Young's avatar
Choi Ga Young committed
210
211
212
213
214
215
  },
  Buttons: {
    flexDirection: 'row',
    margin: 10
  },
  Days: {
Choi Ga Young's avatar
Choi Ga Young committed
216
217
    flexDirection: 'row',
    backgroundColor: '#6495ED'
Choi Ga Young's avatar
Choi Ga Young committed
218
  },
Choi Ga Young's avatar
Choi Ga Young committed
219
220
  DayText: {
    flex: 1,
Choi Ga Young's avatar
Choi Ga Young committed
221
222
    textAlign: 'center',
    color: '#FFFFFF'
Choi Ga Young's avatar
Choi Ga Young committed
223
  },
Choi Ga Young's avatar
Choi Ga Young committed
224
225
226
});

export default Calendar;