@eohjsc/react-native-smart-city 0.2.59 → 0.2.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/README.md +115 -68
  2. package/assets/images/Map/MarkerGeolocation.svg +4 -0
  3. package/package.json +3 -3
  4. package/src/commons/ActionGroup/CurtainButtonTemplate.js +10 -2
  5. package/src/commons/ActionGroup/__test__/CurtainButtonTemplate.test.js +1 -1
  6. package/src/commons/ActionGroup/__test__/MenuActionAddSchedule.test.js +71 -0
  7. package/src/commons/ActionGroup/hooks/AccessScheduleDetailStyles.js +41 -0
  8. package/src/commons/ActionGroup/hooks/MenuActionAddSchedule.js +110 -0
  9. package/src/commons/ActionGroup/hooks/MenuActionAddScheduleStyle.js +69 -0
  10. package/src/commons/ActionGroup/hooks/RecurringDetail.js +97 -0
  11. package/src/commons/DateTimeRangeChange/DateTimeButton.js +7 -2
  12. package/src/commons/Device/HistoryChart.js +80 -81
  13. package/src/commons/Device/HorizontalBarChart.js +48 -31
  14. package/src/commons/Device/LinearChart.js +28 -1
  15. package/src/commons/Form/CurrencyInput.js +1 -0
  16. package/src/commons/FourButtonFilterHistory/__test__/FourButtonFilterHistory.test.js +48 -0
  17. package/src/commons/FourButtonFilterHistory/index.js +72 -0
  18. package/src/commons/FourButtonFilterHistory/styles.js +22 -0
  19. package/src/commons/ImagePicker/index.js +27 -33
  20. package/src/commons/MediaPlayerDetail/Styles/MediaPlayerDetailStyles.js +11 -1
  21. package/src/commons/MediaPlayerDetail/index.js +14 -5
  22. package/src/commons/SubUnit/OneTap/OneTapStyles.js +20 -1
  23. package/src/commons/SubUnit/OneTap/__test__/SubUnitAutomate.test.js +151 -40
  24. package/src/commons/SubUnit/OneTap/index.js +64 -12
  25. package/src/commons/UnitSummary/AirQuality/index.js +9 -7
  26. package/src/commons/UnitSummary/ConfigHistoryChart.js +2 -1
  27. package/src/configs/API.js +3 -0
  28. package/src/configs/Constants.js +15 -0
  29. package/src/iot/RemoteControl/Bluetooth.js +6 -3
  30. package/src/iot/RemoteControl/GoogleHome.js +6 -3
  31. package/src/iot/RemoteControl/Internet.js +1 -0
  32. package/src/iot/RemoteControl/LG.js +2 -1
  33. package/src/iot/RemoteControl/index.js +13 -6
  34. package/src/navigations/SharedStack.js +11 -9
  35. package/src/navigations/UnitStack.js +26 -2
  36. package/src/screens/ActivityLog/ItemLog.js +3 -3
  37. package/src/screens/ActivityLog/__test__/ItemLog.test.js +5 -2
  38. package/src/screens/ActivityLog/hooks/index.js +2 -1
  39. package/src/screens/ActivityLog/index.js +0 -1
  40. package/src/screens/AddLocationMaps/index.js +4 -2
  41. package/src/screens/AddNewAction/SelectSensorDevices.js +18 -11
  42. package/src/screens/AddNewAction/Styles/SelectSensorDevicesStyles.js +5 -1
  43. package/src/screens/AddNewAction/__test__/SelectSensorDevices.test.js +6 -1
  44. package/src/screens/Automate/MultiUnits.js +7 -4
  45. package/src/screens/Automate/__test__/MultiUnits.test.js +1 -1
  46. package/src/screens/Automate/__test__/index.test.js +12 -0
  47. package/src/screens/ConfirmUnitDeletion/__test__/ConfirmUnitDeletion.test.js +61 -0
  48. package/src/screens/ConfirmUnitDeletion/index.js +64 -0
  49. package/src/screens/ConfirmUnitDeletion/styles.js +37 -0
  50. package/src/screens/Device/__test__/detail.test.js +3 -2
  51. package/src/screens/Device/detail.js +48 -15
  52. package/src/screens/Device/hooks/useDisconnectedDevice.js +2 -1
  53. package/src/screens/Device/styles.js +3 -3
  54. package/src/screens/EmergencySetting/__test__/DropDownItem.test.js +59 -0
  55. package/src/screens/EmergencySetting/__test__/index.test.js +27 -0
  56. package/src/screens/EmergencySetting/components/DropDownItem.js +54 -0
  57. package/src/screens/EmergencySetting/index.js +92 -0
  58. package/src/screens/EmergencySetting/styles/DropDownItem.js +38 -0
  59. package/src/screens/EmergencySetting/styles.js +25 -0
  60. package/src/screens/MoveToAnotherSubUnit/__test__/index.test.js +126 -0
  61. package/src/screens/MoveToAnotherSubUnit/index.js +88 -0
  62. package/src/screens/MoveToAnotherSubUnit/styles/MoveToAnotherSubUnitStyles.js +50 -0
  63. package/src/screens/ScriptDetail/Styles/indexStyles.js +0 -1
  64. package/src/screens/ScriptDetail/index.js +1 -0
  65. package/src/screens/SubUnit/AddSubUnit.js +3 -3
  66. package/src/screens/SubUnit/AddSubUnitStyles.js +0 -2
  67. package/src/screens/SubUnit/EditSubUnit.js +16 -7
  68. package/src/screens/SubUnit/EditSubUnitStyles.js +2 -3
  69. package/src/screens/SubUnit/__test__/EditSubUnit.test.js +2 -2
  70. package/src/screens/TDSGuide/index.js +1 -1
  71. package/src/screens/Unit/ChooseLocation.js +3 -7
  72. package/src/screens/Unit/ChooseLocationStyles.js +5 -8
  73. package/src/screens/Unit/Detail.js +16 -6
  74. package/src/screens/Unit/ManageUnit.js +20 -26
  75. package/src/screens/Unit/SmartAccount.js +25 -41
  76. package/src/screens/Unit/SmartAccountItem.js +2 -1
  77. package/src/screens/Unit/SmartAccountStyles.js +0 -1
  78. package/src/screens/Unit/__test__/ManageUnit.test.js +0 -6
  79. package/src/screens/Unit/__test__/SmartAccount.test.js +24 -0
  80. package/src/screens/Unit/__test__/SmartAccountItem.test.js +72 -0
  81. package/src/screens/UnitSummary/components/3PPowerConsumption/index.js +58 -59
  82. package/src/screens/UnitSummary/components/PowerConsumption/index.js +26 -22
  83. package/src/screens/UnitSummary/components/Temperature/ItemTemperature/index.js +2 -2
  84. package/src/screens/UnitSummary/components/Temperature/index.js +15 -14
  85. package/src/screens/UnitSummary/components/UvIndex/index.js +6 -5
  86. package/src/screens/UnitSummary/components/WaterQuality/index.js +9 -7
  87. package/src/screens/UnitSummary/index.js +11 -7
  88. package/src/screens/WaterQualityGuide/index.js +1 -0
  89. package/src/utils/Apis/axios.js +4 -4
  90. package/src/utils/I18n/translations/en.json +20 -2
  91. package/src/utils/I18n/translations/vi.json +21 -2
  92. package/src/utils/Route/index.js +3 -0
  93. package/src/utils/Utils.js +4 -0
  94. package/src/commons/ThreeButtonHistory/CalendarHeader.js +0 -35
  95. package/src/commons/ThreeButtonHistory/CalendarHeaderStyles.js +0 -17
  96. package/src/commons/ThreeButtonHistory/SelectMonth.js +0 -53
  97. package/src/commons/ThreeButtonHistory/SelectMonthStyles.js +0 -29
  98. package/src/commons/ThreeButtonHistory/__test__/SelectMonth.test.js +0 -37
  99. package/src/commons/ThreeButtonHistory/__test__/ThreeButtonHistory.test.js +0 -240
  100. package/src/commons/ThreeButtonHistory/index.js +0 -310
  101. package/src/commons/ThreeButtonHistory/styles.js +0 -65
@@ -1,17 +0,0 @@
1
- import { StyleSheet } from 'react-native';
2
-
3
- export default StyleSheet.create({
4
- wrap: {
5
- flexDirection: 'row',
6
- alignItems: 'center',
7
- justifyContent: 'space-between',
8
- paddingVertical: 16,
9
- paddingHorizontal: 24,
10
- },
11
- arrow: {
12
- transform: [{ rotate: '180deg' }],
13
- },
14
- button: {
15
- padding: 8,
16
- },
17
- });
@@ -1,53 +0,0 @@
1
- import React, { memo, useMemo, useCallback } from 'react';
2
- import { View, TouchableOpacity } from 'react-native';
3
- import moment from 'moment';
4
-
5
- import Text from '../Text';
6
- import styles from './SelectMonthStyles';
7
- import { Colors } from '../../configs';
8
-
9
- const SelectMonth = memo(({ selectedMonth, setSelectedMonth }) => {
10
- const months = useMemo(() => {
11
- return Array.from({ length: 12 }, (_, i) => moment(selectedMonth).month(i));
12
- }, [selectedMonth]);
13
-
14
- const MonthItem = useCallback(
15
- ({ month }) => {
16
- const isSelected = month.isSame(selectedMonth, 'month');
17
- const isDisabled = month.isAfter(moment(), 'month');
18
- const textColor = isSelected
19
- ? Colors.White
20
- : isDisabled
21
- ? Colors.Gray6
22
- : Colors.Gray9;
23
- const backgroundColor = isSelected ? Colors.Primary : Colors.Transparent;
24
-
25
- const onPress = () => setSelectedMonth(month);
26
-
27
- return (
28
- <View style={styles.wrapItem}>
29
- <TouchableOpacity
30
- onPress={onPress}
31
- disabled={isDisabled}
32
- style={[styles.item, { backgroundColor }]}
33
- >
34
- <Text color={textColor} semibold>
35
- {month.format('MMM')}
36
- </Text>
37
- </TouchableOpacity>
38
- </View>
39
- );
40
- },
41
- [selectedMonth, setSelectedMonth]
42
- );
43
-
44
- return (
45
- <View style={styles.wrap}>
46
- {months.map((month, index) => (
47
- <MonthItem key={index} month={month} />
48
- ))}
49
- </View>
50
- );
51
- });
52
-
53
- export default SelectMonth;
@@ -1,29 +0,0 @@
1
- import { StyleSheet } from 'react-native';
2
- import { Constants } from '../../configs';
3
-
4
- const marginItem = 40;
5
- const marginHorizontal = 24;
6
- const widthItem = (Constants.width - marginHorizontal * 2 - marginItem * 2) / 3;
7
- const heightItem = (widthItem / 36) * 16;
8
-
9
- export default StyleSheet.create({
10
- wrap: {
11
- marginHorizontal: 24,
12
- flexDirection: 'row',
13
- justifyContent: 'space-between',
14
- flexWrap: 'wrap',
15
- },
16
- wrapItem: {
17
- width: widthItem,
18
- height: heightItem,
19
- marginBottom: 4,
20
- paddingVertical: 4,
21
- paddingHorizontal: 8,
22
- },
23
- item: {
24
- flex: 1,
25
- justifyContent: 'center',
26
- alignItems: 'center',
27
- borderRadius: 4,
28
- },
29
- });
@@ -1,37 +0,0 @@
1
- import React from 'react';
2
- import { act, create } from 'react-test-renderer';
3
- import { TouchableOpacity } from 'react-native';
4
- import SelectMonth from '../SelectMonth';
5
- import moment from 'moment';
6
-
7
- jest.mock('react', () => {
8
- return {
9
- ...jest.requireActual('react'),
10
- memo: (x) => x,
11
- };
12
- });
13
-
14
- test('test SelectMonth', () => {
15
- let tree;
16
- const mockSetSelectedMonth = jest.fn();
17
- const currentMonth = moment();
18
-
19
- act(() => {
20
- tree = create(
21
- <SelectMonth
22
- selectedMonth={currentMonth}
23
- setSelectedMonth={mockSetSelectedMonth}
24
- />
25
- );
26
- });
27
- const instance = tree.root;
28
- const monthItems = instance.findAllByType(TouchableOpacity);
29
- expect(monthItems).toHaveLength(12);
30
-
31
- for (let i = 0; i < 12; i++) {
32
- act(() => {
33
- monthItems[i].props.onPress();
34
- });
35
- expect(mockSetSelectedMonth).toBeCalledWith(moment(currentMonth).month(i));
36
- }
37
- });
@@ -1,240 +0,0 @@
1
- import React from 'react';
2
- import { act, create } from 'react-test-renderer';
3
- import { SCProvider } from '../../../context';
4
- import { mockSCStore } from '../../../context/mockStore';
5
- import ThreeButtonHistory from '../index';
6
- import moment from 'moment';
7
- import SelectMonth from '../SelectMonth';
8
- import { TESTID } from '../../../configs/Constants';
9
- import { TouchableOpacity } from 'react-native';
10
- import CalendarHeader from '../CalendarHeader';
11
- import ViewButtonBottom from '../../ViewButtonBottom';
12
- import BottomSheet from '../../BottomSheet';
13
- import styles from '../styles';
14
-
15
- const wrapComponent = (props) => (
16
- <SCProvider initState={mockSCStore({})}>
17
- <ThreeButtonHistory {...props} />
18
- </SCProvider>
19
- );
20
-
21
- jest.mock('react', () => {
22
- return {
23
- ...jest.requireActual('react'),
24
- memo: (x) => x,
25
- };
26
- });
27
-
28
- describe('test ThreeButtonHistory', () => {
29
- Date.now = jest.fn(() => new Date('2021-09-09T10:00:00.000Z'));
30
- let tree;
31
- let props;
32
- const startDate = moment().add(-7, 'days').valueOf();
33
- const endDate = moment().valueOf();
34
- const mockSetStartDate = jest.fn();
35
- const mockSetEndDate = jest.fn();
36
- const mockSetGroupBy = jest.fn();
37
-
38
- beforeEach(() => {
39
- mockSetStartDate.mockClear();
40
- mockSetEndDate.mockClear();
41
- mockSetGroupBy.mockClear();
42
- props = {
43
- startDate,
44
- endDate,
45
- setStartDate: mockSetStartDate,
46
- setEndDate: mockSetEndDate,
47
- setGroupBy: mockSetGroupBy,
48
- };
49
- });
50
-
51
- const openCalendar = async (instance) => {
52
- const historyButtons = instance.findAll(
53
- (el) =>
54
- el.props.testID === TESTID.HISTORY_BUTTON &&
55
- el.type === TouchableOpacity
56
- );
57
- expect(historyButtons).toHaveLength(3);
58
- await act(async () => {
59
- await historyButtons[2].props.onPress();
60
- });
61
- };
62
-
63
- const testSelectGroupBy = async (index, groupBy) => {
64
- await act(async () => {
65
- tree = await create(wrapComponent(props));
66
- });
67
- const instance = tree.root;
68
- const historyButtons = instance.findAll(
69
- (el) =>
70
- el.props.testID === TESTID.HISTORY_BUTTON &&
71
- el.type === TouchableOpacity
72
- );
73
- await act(async () => {
74
- await historyButtons[index].props.onPress();
75
- });
76
- expect(mockSetGroupBy).toBeCalledWith(groupBy);
77
- };
78
-
79
- test('test group by date', async () => {
80
- await testSelectGroupBy(2, 'date');
81
- });
82
-
83
- test('test group by week', async () => {
84
- await testSelectGroupBy(0, 'week');
85
- });
86
-
87
- test('test group by month', async () => {
88
- await testSelectGroupBy(1, 'month');
89
- });
90
-
91
- test('test calendar change month', async () => {
92
- await act(async () => {
93
- tree = await create(wrapComponent(props));
94
- });
95
- const instance = tree.root;
96
-
97
- await openCalendar(instance);
98
- const calendar = instance.find(
99
- (el) => el.props.testID === TESTID.HISTORY_CALENDAR
100
- );
101
- const calendarHeader = instance.findByType(CalendarHeader);
102
- expect(calendarHeader.props.currentMonth.toString()).toBe(
103
- moment().toString()
104
- );
105
- // Click calendar title
106
- await act(async () => {
107
- await calendarHeader.props.onPressTitle();
108
- });
109
- const selectMonth = instance.findByType(SelectMonth);
110
- expect(selectMonth).toBeDefined();
111
- expect(calendar.props.style).toBe(styles.displayNone);
112
- // Click arrow left
113
- await act(async () => {
114
- await calendarHeader.props.onSubtractMonth();
115
- });
116
- expect(calendarHeader.props.currentMonth.toString()).toBe(
117
- moment().add(-1, 'month').toString()
118
- );
119
- // Click arrow right
120
- await act(async () => {
121
- await calendarHeader.props.onAddMonth();
122
- });
123
- expect(calendarHeader.props.currentMonth.toString()).toBe(
124
- moment().toString()
125
- );
126
- // Click arrow right
127
- await act(async () => {
128
- await calendarHeader.props.onAddMonth();
129
- });
130
- expect(calendarHeader.props.currentMonth.toString()).toBe(
131
- moment().toString()
132
- ); // exceeded limit
133
- });
134
-
135
- test('test press Done', async () => {
136
- await act(async () => {
137
- tree = await create(wrapComponent(props));
138
- });
139
- const instance = tree.root;
140
- const modal = instance.findByType(BottomSheet);
141
-
142
- await openCalendar(instance);
143
- expect(modal.props.isVisible).toBeTruthy();
144
-
145
- const calendar = instance.find(
146
- (el) => el.props.testID === TESTID.HISTORY_CALENDAR
147
- );
148
- const calendarHeader = instance.findByType(CalendarHeader);
149
- const viewButtonBottom = instance.findByType(ViewButtonBottom);
150
- // Click calendar title
151
- await act(async () => {
152
- await calendarHeader.props.onPressTitle();
153
- });
154
- expect(calendar.props.style).toBe(styles.displayNone);
155
- // Click Done
156
- await act(async () => {
157
- await viewButtonBottom.props.onRightClick();
158
- });
159
- expect(calendar.props.style).not.toBe(styles.displayNone);
160
- // Click Done
161
- await act(async () => {
162
- await viewButtonBottom.props.onRightClick();
163
- });
164
- expect(mockSetStartDate).toBeCalled();
165
- expect(mockSetEndDate).toBeCalled();
166
- expect(modal.props.isVisible).toBeFalsy();
167
- });
168
-
169
- test('test press Cancel', async () => {
170
- await act(async () => {
171
- tree = await create(wrapComponent(props));
172
- });
173
- const instance = tree.root;
174
- const modal = instance.findByType(BottomSheet);
175
-
176
- await openCalendar(instance);
177
- expect(modal.props.isVisible).toBeTruthy();
178
-
179
- const calendar = instance.find(
180
- (el) => el.props.testID === TESTID.HISTORY_CALENDAR
181
- );
182
- const calendarHeader = instance.findByType(CalendarHeader);
183
- const viewButtonBottom = instance.findByType(ViewButtonBottom);
184
- // Click calendar title
185
- await act(async () => {
186
- await calendarHeader.props.onPressTitle();
187
- });
188
- expect(calendar.props.style).toBe(styles.displayNone);
189
- // Click Cancel
190
- await act(async () => {
191
- await viewButtonBottom.props.onLeftClick();
192
- });
193
- expect(calendar.props.style).not.toBe(styles.displayNone);
194
- // Click Cancel
195
- await act(async () => {
196
- await viewButtonBottom.props.onLeftClick();
197
- });
198
- expect(modal.props.isVisible).toBeFalsy();
199
- });
200
-
201
- test('test calendar select date range', async () => {
202
- await act(async () => {
203
- tree = await create(wrapComponent(props));
204
- });
205
- const instance = tree.root;
206
-
207
- await openCalendar(instance);
208
- const calendar = instance.find(
209
- (el) => el.props.testID === TESTID.HISTORY_CALENDAR
210
- );
211
-
212
- const selectDate = async (date) => {
213
- await act(async () => {
214
- await calendar.props.onDayPress({ dateString: date });
215
- });
216
- };
217
- // 2 -> 9
218
- await selectDate('2021-09-20'); // 2 -> 20
219
- await selectDate('2021-09-01'); // 1 -> 20
220
- await selectDate('2021-09-15'); // 1 -> 15
221
- await selectDate('2021-09-05'); // 5 -> 15
222
- await selectDate('2021-09-15'); // 5 -> null
223
- await selectDate('2021-09-05'); // null -> null
224
- await selectDate('2021-09-10'); // 10 -> null
225
- await selectDate('2021-09-05'); // 5 -> 10
226
- await selectDate('2021-09-10'); // 5 -> null
227
- await selectDate('2021-09-20'); // 5 -> 20
228
- await selectDate('2021-09-05'); // null -> 20
229
- await selectDate('2021-09-25'); // 20 -> 25
230
- await selectDate('2021-09-20'); // null -> 25
231
- await selectDate('2021-09-10'); // 10 -> 25
232
-
233
- const viewButtonBottom = instance.findByType(ViewButtonBottom);
234
- await act(async () => {
235
- await viewButtonBottom.props.onRightClick();
236
- });
237
- expect(mockSetStartDate).toBeCalledWith(moment('2021-09-10').valueOf());
238
- expect(mockSetEndDate).toBeCalledWith(moment('2021-09-25').valueOf());
239
- });
240
- });
@@ -1,310 +0,0 @@
1
- import React, { memo, useCallback, useState, useMemo, useRef } from 'react';
2
- import { View, TouchableOpacity } from 'react-native';
3
- import { IconOutline } from '@ant-design/icons-react-native';
4
- import { Calendar } from 'react-native-calendars';
5
- import moment from 'moment';
6
-
7
- import Text from '../../commons/Text';
8
- import BottomSheet from '../../commons/BottomSheet';
9
- import SelectMonth from './SelectMonth';
10
- import CalendarHeader from './CalendarHeader';
11
- import ViewButtonBottom from '../ViewButtonBottom';
12
- import { useTranslations } from '../../hooks/Common/useTranslations';
13
-
14
- import { Colors } from '../../configs';
15
- import { TESTID } from '../../configs/Constants';
16
- import styles from './styles';
17
-
18
- const ThreeButtonHistory = memo(
19
- ({ startDate, endDate, setEndDate, setStartDate, setGroupBy }) => {
20
- const t = useTranslations();
21
- const calendarRef = useRef();
22
- const [selectedIndex, setSelectedIndex] = useState(2);
23
- const [showCalendar, setShowCalendar] = useState(false);
24
- const [selectedStart, setSelectedStart] = useState(
25
- moment(startDate).format('YYYY-MM-DD')
26
- );
27
- const [selectedEnd, setSelectedEnd] = useState(
28
- moment(endDate).format('YYYY-MM-DD')
29
- );
30
-
31
- const [showSelectMonth, setShowSelectMonth] = useState(false);
32
- const [currentMonth, setCurrentMonth] = useState(moment());
33
-
34
- const listItem = useMemo(
35
- () => [
36
- {
37
- id: 1,
38
- dateTitle: '7D',
39
- },
40
- {
41
- id: 2,
42
- dateTitle: '1M',
43
- },
44
- {
45
- id: 3,
46
- dateTitle: (
47
- <View style={styles.paddingTop2}>
48
- <IconOutline
49
- name="calendar"
50
- color={selectedIndex === 2 ? Colors.Primary : Colors.Gray8}
51
- size={21}
52
- />
53
- </View>
54
- ),
55
- },
56
- ],
57
- [selectedIndex]
58
- );
59
-
60
- const onCalendarCancel = useCallback(() => {
61
- if (showSelectMonth) {
62
- setShowSelectMonth(false);
63
- return;
64
- }
65
- setShowCalendar(false);
66
- }, [showSelectMonth, setShowSelectMonth]);
67
-
68
- const onCalendarDone = useCallback(() => {
69
- if (showSelectMonth) {
70
- setShowSelectMonth(false);
71
- return;
72
- }
73
- if (selectedStart === null || selectedEnd === null) {
74
- return;
75
- }
76
- setShowCalendar(false);
77
- setStartDate(moment(selectedStart).valueOf());
78
- setEndDate(moment(selectedEnd).valueOf());
79
- }, [
80
- setStartDate,
81
- selectedStart,
82
- setEndDate,
83
- selectedEnd,
84
- showSelectMonth,
85
- setShowSelectMonth,
86
- ]);
87
-
88
- const onDateSelected = useCallback(
89
- (date) => {
90
- const selectedDate = date.dateString;
91
- const mmSelectedDate = moment(selectedDate);
92
- onMonthSelected(mmSelectedDate);
93
-
94
- if (selectedDate === selectedStart) {
95
- setSelectedStart(null);
96
- return;
97
- }
98
- if (selectedDate === selectedEnd) {
99
- setSelectedEnd(null);
100
- return;
101
- }
102
-
103
- if (!!selectedStart && !!selectedEnd) {
104
- if (mmSelectedDate.isAfter(selectedEnd, 'date')) {
105
- setSelectedEnd(selectedDate);
106
- return;
107
- }
108
- if (mmSelectedDate.isBefore(selectedStart, 'date')) {
109
- setSelectedStart(selectedDate);
110
- return;
111
- }
112
- if (
113
- Math.abs(mmSelectedDate.diff(selectedStart, 'days')) <=
114
- Math.abs(mmSelectedDate.diff(selectedEnd, 'days'))
115
- ) {
116
- setSelectedStart(selectedDate);
117
- } else {
118
- setSelectedEnd(selectedDate);
119
- }
120
- return;
121
- }
122
-
123
- if (selectedStart) {
124
- if (mmSelectedDate.isBefore(selectedStart, 'date')) {
125
- setSelectedStart(selectedDate);
126
- setSelectedEnd(selectedStart);
127
- } else {
128
- setSelectedEnd(selectedDate);
129
- }
130
- return;
131
- }
132
-
133
- if (selectedEnd) {
134
- if (mmSelectedDate.isAfter(selectedEnd, 'date')) {
135
- setSelectedStart(selectedEnd);
136
- setSelectedEnd(selectedDate);
137
- } else {
138
- setSelectedStart(selectedDate);
139
- }
140
- return;
141
- }
142
-
143
- setSelectedStart(selectedDate);
144
- },
145
- [selectedStart, selectedEnd, onMonthSelected]
146
- );
147
-
148
- const onPressItemButton = useCallback(
149
- (index) => () => {
150
- switch (index) {
151
- case 0:
152
- setGroupBy('week');
153
- break;
154
- case 1:
155
- setGroupBy('month');
156
- break;
157
- case 2:
158
- setGroupBy('date');
159
- selectedIndex === 2 && setShowCalendar(true);
160
- }
161
- setSelectedIndex(index);
162
- },
163
- [selectedIndex, setGroupBy]
164
- );
165
-
166
- const ItemButton = memo(
167
- ({ dateTitle, onPress, isSelected }) => {
168
- return (
169
- <TouchableOpacity
170
- onPress={onPress}
171
- style={[styles.buttonDate, isSelected && styles.buttonDateActive]}
172
- testID={TESTID.HISTORY_BUTTON}
173
- >
174
- <View>
175
- <Text semibold color={isSelected && Colors.Primary}>
176
- {dateTitle}
177
- </Text>
178
- </View>
179
- </TouchableOpacity>
180
- );
181
- },
182
- [selectedIndex]
183
- );
184
-
185
- const onPressCalendarTitle = useCallback(() => {
186
- setShowSelectMonth((state) => !state);
187
- }, [setShowSelectMonth]);
188
-
189
- const onAddMonth = useCallback(() => {
190
- setCurrentMonth((oldMonth) => {
191
- const newMonth = moment(oldMonth).add(1, 'months');
192
- if (newMonth.isAfter(moment(), 'month')) {
193
- return oldMonth;
194
- }
195
- calendarRef?.current?.addMonth(1);
196
- return newMonth;
197
- });
198
- }, []);
199
-
200
- const onSubtractMonth = useCallback(() => {
201
- setCurrentMonth((oldMonth) => moment(oldMonth).add(-1, 'months'));
202
- calendarRef?.current?.addMonth(-1);
203
- }, []);
204
-
205
- const onMonthSelected = useCallback(
206
- (month) => {
207
- setCurrentMonth((oldMonth) => {
208
- const monthDiff = month.month() - oldMonth.month();
209
- calendarRef?.current?.addMonth(monthDiff);
210
- return month;
211
- });
212
- },
213
- [setCurrentMonth]
214
- );
215
-
216
- const markedDates = useMemo(() => {
217
- const dates = {
218
- [selectedStart]: {
219
- selected: true,
220
- selectedColor: Colors.Primary,
221
- selectedTextColor: Colors.White,
222
- },
223
- [selectedEnd]: {
224
- selected: true,
225
- selectedColor: Colors.Primary,
226
- selectedTextColor: Colors.White,
227
- },
228
- };
229
- const currDate = moment(selectedStart).startOf('day');
230
- const lastDate = moment(selectedEnd).startOf('day');
231
-
232
- while (currDate.add(1, 'days').diff(lastDate) < 0) {
233
- dates[[moment(currDate).format('YYYY-MM-DD')]] = {
234
- customStyles: {
235
- container: {
236
- borderRadius: 0,
237
- width: 60,
238
- backgroundColor: Colors.Gray4,
239
- },
240
- text: {
241
- color: Colors.Black,
242
- },
243
- },
244
- selected: true,
245
- color: Colors.Gray4,
246
- };
247
- }
248
- return dates;
249
- }, [selectedStart, selectedEnd]);
250
-
251
- return (
252
- <>
253
- <View style={styles.historyTitleRight}>
254
- {listItem &&
255
- listItem.map((item, index) => {
256
- return (
257
- <ItemButton
258
- key={index}
259
- dateTitle={item.dateTitle}
260
- onPress={onPressItemButton(index)}
261
- isSelected={selectedIndex === index}
262
- />
263
- );
264
- })}
265
- </View>
266
- <BottomSheet
267
- isVisible={showCalendar}
268
- onBackdropPress={onCalendarCancel}
269
- style={styles.modal}
270
- >
271
- <View style={styles.calendar}>
272
- <CalendarHeader
273
- currentMonth={currentMonth}
274
- onAddMonth={onAddMonth}
275
- onSubtractMonth={onSubtractMonth}
276
- onPressTitle={onPressCalendarTitle}
277
- />
278
- {showSelectMonth && (
279
- <SelectMonth
280
- selectedMonth={currentMonth}
281
- setSelectedMonth={onMonthSelected}
282
- />
283
- )}
284
- <Calendar
285
- ref={calendarRef}
286
- current={currentMonth.format('YYYY-MM-DD')}
287
- style={showSelectMonth && styles.displayNone}
288
- markingType={'custom'}
289
- onDayPress={onDateSelected}
290
- maxDate={moment().format('YYYY-MM-DD')}
291
- markedDates={markedDates}
292
- hideArrows={true}
293
- renderHeader={() => <></>}
294
- testID={TESTID.HISTORY_CALENDAR}
295
- />
296
- <View style={styles.separator} />
297
- <ViewButtonBottom
298
- leftTitle={t('cancel')}
299
- onLeftClick={onCalendarCancel}
300
- rightTitle={t('done')}
301
- onRightClick={onCalendarDone}
302
- />
303
- </View>
304
- </BottomSheet>
305
- </>
306
- );
307
- }
308
- );
309
-
310
- export default ThreeButtonHistory;