@eohjsc/react-native-smart-city 0.3.89 → 0.3.91

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 (166) hide show
  1. package/package.json +1 -1
  2. package/src/commons/ActionGroup/__test__/NumberUpDownTemplate.test.js +1 -1
  3. package/src/commons/ActionTemplate/CurtainAction.js +2 -2
  4. package/src/commons/ActionTemplate/OnOffButtonAction.js +2 -2
  5. package/src/commons/ActionTemplate/OnOffSimpleAction.js +2 -2
  6. package/src/commons/ActionTemplate/OnOffSmartLockAction.js +2 -2
  7. package/src/commons/ActionTemplate/OneButtonAction.js +2 -2
  8. package/src/commons/ActionTemplate/ThreeButtonAction.js +2 -2
  9. package/src/commons/ActionTemplate/__test__/CurtainAction.test.js +1 -1
  10. package/src/commons/ActionTemplate/__test__/OnOffButtonAction.test.js +1 -1
  11. package/src/commons/ActionTemplate/__test__/OnOffSimpleAction.test.js +1 -1
  12. package/src/commons/ActionTemplate/__test__/OnOffSmartLockAction.test.js +1 -1
  13. package/src/commons/ActionTemplate/__test__/OneButtonAction.test.js +1 -1
  14. package/src/commons/ActionTemplate/__test__/ThreeButtonAction.test.js +1 -1
  15. package/src/commons/ActionTemplate/__test__/index.test.js +21 -70
  16. package/src/commons/ActionTemplate/index.js +19 -55
  17. package/src/commons/Alert/__test__/Alert.test.js +1 -1
  18. package/src/commons/Auth/__test__/OtpInputList.test.js +4 -2
  19. package/src/commons/Auth/__test__/SocialButton.test.js +4 -2
  20. package/src/commons/BackDefault/__test__/BackDefault.test.js +1 -1
  21. package/src/commons/Button/__test__/Button.test.js +1 -1
  22. package/src/commons/ButtonPopup/__test__/ButtonPopup.test.js +1 -1
  23. package/src/commons/Calendar/__test__/Calendar.test.js +1 -1
  24. package/src/commons/ChartLoading/__test__/ChartLoading.test.js +1 -1
  25. package/src/commons/CircleButton/__test__/CircleButton.test.js +1 -1
  26. package/src/commons/CircleView/__test__/CircleView.test.js +1 -1
  27. package/src/commons/DateTimeRangeChange/__test__/DateTimeButton.test.js +4 -2
  28. package/src/commons/DateTimeRangeChange/index.js +31 -48
  29. package/src/commons/Device/Emergency/__test__/EmergencyButton.test.js +2 -2
  30. package/src/commons/Device/Emergency/__test__/EmergencyDetail.test.js +1 -1
  31. package/src/commons/Device/FlatListItems.js +1 -1
  32. package/src/commons/Device/Hanet/ItemHanetDevice.test.js +2 -2
  33. package/src/commons/Device/HistoryChart.js +6 -49
  34. package/src/commons/Device/LinearChart/LinearChart.test.js +1 -1
  35. package/src/commons/Device/ProgressBar/__test__/ProgressBar.test.js +3 -4
  36. package/src/commons/Device/ProgressBar/index.js +6 -4
  37. package/src/commons/Device/ProgressBar/styles.js +9 -6
  38. package/src/commons/Device/RainningSensor/__test__/CurrentRainSensor.test.js +5 -5
  39. package/src/commons/Device/SonosSpeaker/__test__/SonosSpeaker.test.js +3 -3
  40. package/src/commons/Device/WindDirection/Compass/Compass.test.js +2 -2
  41. package/src/commons/Device/WindSpeed/LinearChart/index.js +1 -1
  42. package/src/commons/Device/__test__/DeviceAlertStatus.test.js +3 -3
  43. package/src/commons/Device/__test__/DisconnectedView.test.js +2 -2
  44. package/src/commons/Device/__test__/FlatListItems.test.js +1 -1
  45. package/src/commons/Device/__test__/SensorConnectedStatus.test.js +1 -1
  46. package/src/commons/DisplayChecking/__test__/DisplayChecking.test.js +1 -1
  47. package/src/commons/Form/__test__/CurrencyInput.test.js +2 -2
  48. package/src/commons/Form/__test__/TextInput.test.js +2 -2
  49. package/src/commons/Form/__test__/TextInputPassword.test.js +2 -2
  50. package/src/commons/FullLoading/__test__/index.test.js +2 -2
  51. package/src/commons/ImageButton/__test__/ImageButton.test.js +1 -1
  52. package/src/commons/OneTapTemplate/NumberUpDownActionTemplate.js +2 -2
  53. package/src/commons/OneTapTemplate/OptionsDropdownActionTemplate.js +4 -4
  54. package/src/commons/OneTapTemplate/StatesGridActionTemplate.js +4 -4
  55. package/src/commons/OneTapTemplate/__test__/NumberUpDownActionTemplate.test.js +2 -2
  56. package/src/commons/OneTapTemplate/__test__/OptionsDropdownActionTemplate.test.js +1 -1
  57. package/src/commons/OneTapTemplate/__test__/StatesGridActionTemplate.test.js +2 -2
  58. package/src/commons/RowItem/__test__/RowItem.test.js +1 -1
  59. package/src/commons/RowUser/__test__/RowUser.test.js +1 -1
  60. package/src/commons/Section/Section.test.js +2 -2
  61. package/src/commons/SelectSubUnit/__test__/SelectSubUnit.test.js +1 -1
  62. package/src/commons/SelectUnit/__test__/SelectUnit.test.js +3 -3
  63. package/src/commons/Sharing/__test__/ButtonRemoveMember.test.js +1 -1
  64. package/src/commons/Sharing/__test__/DevicePermissionsCheckbox.test.js +1 -1
  65. package/src/commons/Sharing/__test__/MemberList.test.js +6 -4
  66. package/src/commons/Sharing/__test__/RowMember.test.js +2 -2
  67. package/src/commons/Sharing/__test__/StationDevicePermission.test.js +3 -3
  68. package/src/commons/Sharing/__test__/WrapHeaderScrollable.test.js +4 -2
  69. package/src/commons/SubUnit/OneTap/__test__/SubUnitAutomate.test.js +2 -10
  70. package/src/commons/SubUnit/OneTap/index.js +3 -1
  71. package/src/commons/TextButton/__test__/TextButton.test.js +2 -2
  72. package/src/commons/Today/__test__/Today.test.js +1 -1
  73. package/src/commons/TouchableScale/__test__/TouchableScale.test.js +1 -1
  74. package/src/commons/UnitSummary/AirQuality/index.js +2 -2
  75. package/src/commons/UnitSummary/ConfigHistoryChart/index.js +0 -6
  76. package/src/commons/UnitSummary/__test__/TotalPowerBox.test.js +1 -1
  77. package/src/commons/UnitSummary/__test__/TotalPowerConsumption.test.js +1 -1
  78. package/src/commons/ViewButtonBottom/__test__/ViewButtonBottom.test.js +1 -1
  79. package/src/configs/API.js +9 -32
  80. package/src/configs/Constants.js +2 -30
  81. package/src/context/reducer.ts +3 -1
  82. package/src/hoc/__test__/withRemoteControl.test.js +1 -1
  83. package/src/navigations/UnitStack.js +23 -13
  84. package/src/screens/ActivityLog/FilterPopup.js +4 -4
  85. package/src/screens/ActivityLog/__test__/FilterPopup.test.js +2 -2
  86. package/src/screens/ActivityLog/__test__/index.test.js +2 -2
  87. package/src/screens/AddCommon/__test__/SelectSubUnit.test.js +3 -3
  88. package/src/screens/AddCommon/__test__/SelectUnit.test.js +5 -5
  89. package/src/screens/AddNewAction/ChooseAction.js +180 -0
  90. package/src/screens/AddNewAction/ChooseConfig.js +207 -0
  91. package/src/screens/AddNewAction/Components/SelectDevices.js +75 -0
  92. package/src/screens/AddNewAction/Device/__test__/index.test.js +1 -1
  93. package/src/screens/AddNewAction/NewActionWrapper.js +55 -0
  94. package/src/screens/AddNewAction/SelectControlDevices.js +107 -0
  95. package/src/screens/AddNewAction/SelectMonitorDevices.js +142 -0
  96. package/src/screens/AddNewAction/SetupConfigCondition.js +245 -0
  97. package/src/screens/AddNewAction/Styles/SetupSensorStyles.js +1 -1
  98. package/src/screens/AddNewAction/__test__/{SelectAction.test.js → ChooseAction.test.js} +94 -114
  99. package/src/screens/AddNewAction/__test__/ChooseConfig.test.js +211 -0
  100. package/src/screens/AddNewAction/__test__/{SelectSensorDevices.test.js → SelectControlDevices.test.js} +32 -83
  101. package/src/screens/AddNewAction/__test__/SelectMonitorDevices.test.js +92 -0
  102. package/src/screens/AddNewAction/__test__/SetupSensor.test.js +9 -22
  103. package/src/screens/AddNewAutoSmart/__test__/AddNewAutoSmart.test.js +2 -3
  104. package/src/screens/AddNewAutoSmart/index.js +13 -33
  105. package/src/screens/AddNewGateway/PlugAndPlay/__test__/ConnectRouterGuide.test.js +1 -1
  106. package/src/screens/AddNewGateway/PlugAndPlay/__test__/FirstWarning.test.js +1 -1
  107. package/src/screens/AddNewGateway/PlugAndPlay/__test__/ZigbeeDeviceConnectGuide.test.js +1 -1
  108. package/src/screens/AddNewGateway/__test__/ConnectingWifiGuide.test.js +1 -1
  109. package/src/screens/AddNewGateway/__test__/ScanGatewayQR.test.js +2 -2
  110. package/src/screens/AddNewGateway/__test__/ScanModbusQR.test.js +3 -3
  111. package/src/screens/AddNewGateway/__test__/ScanWifiDeviceQR.test.js +2 -2
  112. package/src/screens/AddNewGateway/__test__/SelectDeviceSubUnit.test.js +3 -3
  113. package/src/screens/AddNewGateway/__test__/SelectDeviceUnit.test.js +1 -1
  114. package/src/screens/AddNewGateway/__test__/ShareWifiPassword.test.js +1 -1
  115. package/src/screens/AddNewOneTap/index.js +16 -7
  116. package/src/screens/AllCamera/__test__/index.test.js +18 -57
  117. package/src/screens/AllCamera/index.js +98 -30
  118. package/src/screens/Automate/MultiUnits.js +2 -2
  119. package/src/screens/Automate/__test__/MultiUnits.test.js +2 -2
  120. package/src/screens/Automate/__test__/index.test.js +19 -38
  121. package/src/screens/Automate/index.js +8 -3
  122. package/src/screens/Device/__test__/DetailHistoryChart.test.js +2 -2
  123. package/src/screens/Device/__test__/EmergencyCountdown.test.js +4 -2
  124. package/src/screens/Device/__test__/sensorDisplayItem.test.js +10 -10
  125. package/src/screens/Device/components/ChartWrapper.js +3 -3
  126. package/src/screens/Device/components/VisualChart.js +1 -1
  127. package/src/screens/Device/components/__test__/VisualChart.test.js +1 -1
  128. package/src/screens/Device/hooks/useEvaluateValue.js +8 -0
  129. package/src/screens/DeviceInfo/__test__/index.test.js +16 -18
  130. package/src/screens/DeviceInfo/index.js +6 -5
  131. package/src/screens/EditActionsList/__tests__/index.test.js +14 -6
  132. package/src/screens/Notification/__test__/Notification.test.js +65 -74
  133. package/src/screens/Notification/index.js +14 -14
  134. package/src/screens/ScriptDetail/__test__/index.test.js +2 -8
  135. package/src/screens/ScriptDetail/index.js +9 -88
  136. package/src/screens/ScriptDetail/utils.js +112 -0
  137. package/src/screens/SelectUnit/__test__/index.test.js +0 -5
  138. package/src/screens/SelectUnit/index.js +1 -2
  139. package/src/screens/Sharing/Components/TitleCheckBox.js +2 -2
  140. package/src/screens/SmartAccount/SuccessfullyConnected/__test__/SuccessfullyConnected.test.js +3 -3
  141. package/src/screens/SmartAccount/__test__/Connecting.test.js +1 -1
  142. package/src/screens/SmartAccount/__test__/SmartAccount.test.js +3 -3
  143. package/src/screens/SmartIr/__test__/ButtonsBottom.test.js +1 -1
  144. package/src/screens/SmartIr/__test__/GroupButtonByType.test.js +5 -5
  145. package/src/screens/SmartIr/__test__/SelectBrand.test.js +2 -2
  146. package/src/screens/SmartIr/__test__/SelectDeviceType.test.js +2 -2
  147. package/src/screens/SmartIr/__test__/SmartIr.test.js +2 -2
  148. package/src/screens/Template/__test__/detail.test.js +10 -2
  149. package/src/screens/Unit/__test__/Detail.test.js +7 -13
  150. package/src/screens/Unit/components/__test__/Header.test.js +4 -4
  151. package/src/screens/UnitSummary/components/3PPowerConsumption/__test__/3PPowerConsumption.test.js +1 -1
  152. package/src/screens/UnitSummary/components/3PPowerConsumption/index.js +14 -6
  153. package/src/screens/UnitSummary/components/PowerConsumption/__test__/PowerConsumption.test.js +1 -1
  154. package/src/screens/UnitSummary/components/Temperature/index.js +2 -2
  155. package/src/screens/UnitSummary/components/UvIndex/index.js +2 -2
  156. package/src/screens/UnitSummary/components/WaterQuality/index.js +2 -2
  157. package/src/screens/UnitSummary/components/__test__/TotalPowerBox.test.js +1 -1
  158. package/src/screens/UnitSummary/components/__test__/TotalPowerConsumption.test.js +1 -1
  159. package/src/screens/UnitSummary/components/__test__/index.test.js +2 -2
  160. package/src/utils/Apis/axios.js +3 -0
  161. package/src/utils/I18n/translations/en.json +1 -0
  162. package/src/utils/I18n/translations/vi.json +1 -0
  163. package/src/utils/Route/index.js +6 -3
  164. package/src/screens/AddNewAction/SelectAction.js +0 -570
  165. package/src/screens/AddNewAction/SelectSensorDevices.js +0 -236
  166. package/src/screens/AddNewAction/SetupSensor.js +0 -263
@@ -0,0 +1,55 @@
1
+ import React, { useCallback, useMemo } from 'react';
2
+ import { TouchableOpacity, View } from 'react-native';
3
+
4
+ import styles from './Styles/SelectActionStyles';
5
+ import { useTranslations } from '../../hooks/Common/useTranslations';
6
+ import WrapHeaderScrollable from '../../commons/Sharing/WrapHeaderScrollable';
7
+ import { Icon } from '@ant-design/react-native';
8
+ import { Colors } from '../../configs';
9
+ import { useNavigation, useRoute } from '@react-navigation/native';
10
+ import BottomButtonView from '../../commons/BottomButtonView';
11
+ import { AccessibilityLabel } from '../../configs/Constants';
12
+
13
+ const NewActionWrapper = ({ name, children, canNext, onNext, nextTitle }) => {
14
+ const t = useTranslations();
15
+ const { navigate } = useNavigation();
16
+ const { params = {} } = useRoute();
17
+ const { closeScreen } = params;
18
+
19
+ const handleClose = useCallback(() => {
20
+ navigate(closeScreen);
21
+ }, [closeScreen, navigate]);
22
+
23
+ const rightComponent = useMemo(
24
+ () => (
25
+ <TouchableOpacity
26
+ style={styles.closeButton}
27
+ onPress={handleClose}
28
+ accessibilityLabel={AccessibilityLabel.ICON_CLOSE}
29
+ >
30
+ <Icon name={'close'} size={24} color={Colors.Black} />
31
+ </TouchableOpacity>
32
+ ),
33
+ [handleClose]
34
+ );
35
+
36
+ return (
37
+ <View style={styles.wrap}>
38
+ <WrapHeaderScrollable
39
+ title={t('set_up {name}', { name })}
40
+ headerAniStyle={styles.headerAniStyle}
41
+ rightComponent={rightComponent}
42
+ >
43
+ {children}
44
+ </WrapHeaderScrollable>
45
+ <BottomButtonView
46
+ style={styles.bottomButtonView}
47
+ mainTitle={nextTitle}
48
+ onPressMain={onNext}
49
+ typeMain={canNext ? 'primary' : 'disabled'}
50
+ />
51
+ </View>
52
+ );
53
+ };
54
+
55
+ export default NewActionWrapper;
@@ -0,0 +1,107 @@
1
+ import React, { useCallback, useEffect, useState } from 'react';
2
+ import { useNavigation } from '@react-navigation/native';
3
+ import { fetchWithCache } from '../../utils/Apis/axios';
4
+ import { API } from '../../configs';
5
+ import Routes from '../../utils/Route';
6
+ import SelectDevices from './Components/SelectDevices';
7
+ import { useTranslations } from '../../hooks/Common/useTranslations';
8
+
9
+ const SelectControlDevices = ({ route }) => {
10
+ const t = useTranslations();
11
+ const {
12
+ unit,
13
+ automateId,
14
+ automate = {},
15
+ type,
16
+ scriptName,
17
+ isAutomateTab,
18
+ isCreateNewAction,
19
+ isMultiUnits,
20
+ oldType,
21
+ closeScreen,
22
+ } = route?.params || {};
23
+
24
+ const [stations, setStations] = useState([]);
25
+ const [listStation, setListStation] = useState([]);
26
+ const [listMenuItem, setListMenuItem] = useState([]);
27
+
28
+ const { navigate } = useNavigation();
29
+ const [loading, setLoading] = useState(true);
30
+
31
+ const fetchDetails = useCallback(async () => {
32
+ await fetchWithCache(API.UNIT.DEVICE_CONTROL(unit.id), {}, (response) => {
33
+ const { success, data } = response;
34
+ if (success) {
35
+ const newData = data.filter((item) => item.sensors.length > 0);
36
+ const listMenu = newData.map((item, index) => ({
37
+ text: item.name,
38
+ station: item,
39
+ index: index,
40
+ }));
41
+ setStations(newData);
42
+ setListMenuItem(listMenu);
43
+ setListStation(listMenu);
44
+ }
45
+ });
46
+ setLoading(false);
47
+ }, [unit]);
48
+
49
+ useEffect(() => {
50
+ fetchDetails();
51
+ }, [fetchDetails]);
52
+
53
+ const onPressContinue = useCallback(
54
+ (selectedDevice, indexStation) => {
55
+ navigate(Routes.ChooseAction, {
56
+ unit,
57
+ device: selectedDevice,
58
+ automateId: automateId,
59
+ automate: {
60
+ ...automate,
61
+ sensor_id: selectedDevice?.id
62
+ ? selectedDevice.id
63
+ : automate.sensor_id,
64
+ },
65
+ stationName: stations[indexStation]?.name,
66
+ type,
67
+ scriptName,
68
+ isAutomateTab,
69
+ isCreateNewAction,
70
+ isMultiUnits,
71
+ oldType,
72
+ closeScreen,
73
+ });
74
+ },
75
+ [
76
+ navigate,
77
+ unit,
78
+ automateId,
79
+ automate,
80
+ stations,
81
+ type,
82
+ scriptName,
83
+ isAutomateTab,
84
+ isCreateNewAction,
85
+ isMultiUnits,
86
+ oldType,
87
+ closeScreen,
88
+ ]
89
+ );
90
+
91
+ if (loading) {
92
+ return null;
93
+ }
94
+
95
+ return (
96
+ <SelectDevices
97
+ onPressContinue={onPressContinue}
98
+ automateId={automateId}
99
+ listStation={listStation}
100
+ listMenuItem={listMenuItem}
101
+ stations={stations}
102
+ title={t('select_device')}
103
+ />
104
+ );
105
+ };
106
+
107
+ export default SelectControlDevices;
@@ -0,0 +1,142 @@
1
+ import React, { useCallback, useEffect, useState } from 'react';
2
+ import { useNavigation } from '@react-navigation/native';
3
+
4
+ import { useTranslations } from '../../hooks/Common/useTranslations';
5
+ import { fetchWithCache } from '../../utils/Apis/axios';
6
+ import { API } from '../../configs';
7
+ import Routes from '../../utils/Route';
8
+ import SelectDevices from './Components/SelectDevices';
9
+ import { AUTOMATE_TYPE } from '../../configs/Constants';
10
+
11
+ const SelectMonitorDevices = ({ route }) => {
12
+ const t = useTranslations();
13
+ const {
14
+ unit,
15
+ automateId,
16
+ automate = {},
17
+ type,
18
+ scriptName,
19
+ isAutomateTab,
20
+ isCreateNewAction,
21
+ isMultiUnits,
22
+ oldType,
23
+ closeScreen,
24
+ } = route?.params || {};
25
+
26
+ const [stations, setStations] = useState([]);
27
+ const [listStation, setListStation] = useState([]);
28
+ const [listMenuItem, setListMenuItem] = useState([]);
29
+ const [defaultSelectedDevice, setDefaultSelectedDevice] = useState({});
30
+
31
+ const { navigate } = useNavigation();
32
+ const [loading, setLoading] = useState(true);
33
+
34
+ const fetchDetails = useCallback(async () => {
35
+ const configs = { params: {} };
36
+ if (type === AUTOMATE_TYPE.EVENT) {
37
+ configs.params.type = 'event';
38
+ }
39
+ await fetchWithCache(
40
+ API.UNIT.DEVICE_SENSOR(unit.id),
41
+ configs,
42
+ (response) => {
43
+ const { success, data } = response;
44
+ if (success) {
45
+ const newData = data.filter((item) => item.sensors.length > 0);
46
+ const listMenu = newData.map((item, index) => ({
47
+ text: item.name,
48
+ station: item,
49
+ index: index,
50
+ }));
51
+
52
+ setStations(newData);
53
+ setListMenuItem(listMenu);
54
+ setListStation(listMenu);
55
+ }
56
+ }
57
+ );
58
+ setLoading(false);
59
+ }, [type, unit.id]);
60
+
61
+ useEffect(() => {
62
+ if (!automate?.sensor_id) {
63
+ return;
64
+ }
65
+ let selectSensor = {};
66
+ let found = false;
67
+ for (let i = 0; i < stations.length; i++) {
68
+ for (let j = 0; j < stations[i].sensors.length; j++) {
69
+ if (stations[i].sensors[j].id === automate.sensor_id) {
70
+ selectSensor = stations[i].sensors[j];
71
+ found = true;
72
+ break;
73
+ }
74
+ }
75
+ if (found) {
76
+ break;
77
+ }
78
+ }
79
+ setDefaultSelectedDevice(selectSensor);
80
+ }, [automate?.sensor_id, stations]);
81
+
82
+ useEffect(() => {
83
+ fetchDetails();
84
+ }, [fetchDetails]);
85
+
86
+ const onPressContinue = useCallback(
87
+ (selectedDevice, indexStation) => {
88
+ navigate(Routes.ChooseConfig, {
89
+ unit,
90
+ device: selectedDevice,
91
+ automateId: automateId,
92
+ automate: {
93
+ ...automate,
94
+ sensor_id: selectedDevice?.id
95
+ ? selectedDevice.id
96
+ : automate.sensor_id,
97
+ },
98
+ stationName: stations[indexStation]?.name,
99
+ type,
100
+ scriptName,
101
+ isAutomateTab,
102
+ isCreateNewAction,
103
+ isMultiUnits,
104
+ oldType,
105
+ closeScreen,
106
+ });
107
+ },
108
+ [
109
+ navigate,
110
+ unit,
111
+ automateId,
112
+ automate,
113
+ stations,
114
+ type,
115
+ scriptName,
116
+ isAutomateTab,
117
+ isCreateNewAction,
118
+ isMultiUnits,
119
+ oldType,
120
+ closeScreen,
121
+ ]
122
+ );
123
+
124
+ if (loading) {
125
+ return null;
126
+ }
127
+
128
+ return (
129
+ <SelectDevices
130
+ onPressContinue={onPressContinue}
131
+ automateId={automateId}
132
+ listStation={listStation}
133
+ listMenuItem={listMenuItem}
134
+ stations={stations}
135
+ title={t('select_sensor')}
136
+ defaultSelectedDevice={defaultSelectedDevice}
137
+ closeScreen={closeScreen}
138
+ />
139
+ );
140
+ };
141
+
142
+ export default SelectMonitorDevices;
@@ -0,0 +1,245 @@
1
+ import React, { useCallback, useEffect, useMemo, useState } from 'react';
2
+ import { Image, TouchableOpacity, View } from 'react-native';
3
+ import { useNavigation, useRoute } from '@react-navigation/native';
4
+
5
+ import styles from './Styles/SetupSensorStyles';
6
+ import { Colors, Images } from '../../configs';
7
+ import { useTranslations } from '../../hooks/Common/useTranslations';
8
+ import { ModalCustom } from '../../commons/Modal';
9
+ import Text from '../../commons/Text';
10
+ import { HorizontalPicker } from '../../commons';
11
+ import { useStatusBarPreview } from '../../hooks/Common/useStatusBar';
12
+ import Routes from '../../utils/Route';
13
+ import { getAutomationData } from '../ScriptDetail/utils';
14
+ import NewActionWrapper from './NewActionWrapper';
15
+
16
+ const SetupConfigCondition = () => {
17
+ const t = useTranslations();
18
+
19
+ const { navigate } = useNavigation();
20
+ const { params = {} } = useRoute();
21
+ const { item, sensorData, type, automate } = params;
22
+
23
+ const hasLimit = useMemo(() => !!item?.range_max, [item]);
24
+ const [isShowModal, setIsShowModal] = useState(false);
25
+ const [itemActiveModal, setItemActiveModal] = useState(null);
26
+ const [value, setValue] = useState(null);
27
+ const [minimum] = useState(hasLimit ? parseInt(item?.range_min, 10) : 0);
28
+ const [maximum, setMaximum] = useState(
29
+ value === 0
30
+ ? 100
31
+ : hasLimit
32
+ ? (parseInt(item?.range_max, 10) - minimum) * 10 + minimum
33
+ : (parseInt(item?.value, 10) + 20 - minimum) * 10 + minimum
34
+ );
35
+
36
+ const hasNoValueEvaluation = !item?.evaluate_configuration;
37
+
38
+ const conditionOptions = useMemo(() => {
39
+ if (!item?.evaluate_configuration) {
40
+ return [
41
+ {
42
+ title: `${t('is_below')} (<)`,
43
+ condition: '<',
44
+ },
45
+ {
46
+ title: `${t('is')} (=)`,
47
+ condition: '=',
48
+ },
49
+ {
50
+ title: `${t('is_above')} (>)`,
51
+ condition: '>',
52
+ },
53
+ ];
54
+ }
55
+ if (item.evaluate_configuration.ranges) {
56
+ return item.evaluate_configuration.ranges.map((range, index) => ({
57
+ title: range.evaluate?.text,
58
+ condition: 'value_evaluation',
59
+ config: item.id,
60
+ value: index,
61
+ }));
62
+ }
63
+ return [];
64
+ }, [item.evaluate_configuration, item.id, t]);
65
+
66
+ useEffect(() => {
67
+ let defaultCondition = null;
68
+ if (sensorData) {
69
+ const index = sensorData.findIndex((i) => i?.id === item?.id);
70
+ if (index !== -1 && sensorData[index].condition) {
71
+ defaultCondition = sensorData[index];
72
+ }
73
+ }
74
+
75
+ if (automate && !defaultCondition) {
76
+ const automationData = getAutomationData(type, automate);
77
+
78
+ const index = conditionOptions.findIndex(
79
+ (i) =>
80
+ i.condition === automationData?.condition &&
81
+ i.value === automationData?.value
82
+ );
83
+ if (index !== -1) {
84
+ defaultCondition = conditionOptions[index];
85
+ }
86
+ }
87
+
88
+ if (!defaultCondition) {
89
+ defaultCondition = conditionOptions[0];
90
+ }
91
+
92
+ setItemActiveModal(defaultCondition);
93
+ }, [conditionOptions, item, sensorData, automate, type]);
94
+
95
+ const onOpenModal = useCallback(() => {
96
+ setIsShowModal(true);
97
+ }, []);
98
+
99
+ const onCloseModal = useCallback(() => {
100
+ setIsShowModal(false);
101
+ }, []);
102
+
103
+ const onChooseCondition = (selectedItem) => () => {
104
+ setItemActiveModal(selectedItem);
105
+ onCloseModal();
106
+ };
107
+
108
+ const onChangeValue = useCallback(
109
+ (newValue) => {
110
+ setValue(
111
+ (parseFloat(newValue / 128) + parseFloat(minimum)).toFixed(
112
+ item?.decimal_behind || 1
113
+ )
114
+ );
115
+ },
116
+ // eslint-disable-next-line react-hooks/exhaustive-deps
117
+ [value]
118
+ );
119
+
120
+ const onSave = useCallback(() => {
121
+ const dataTemp = [...sensorData];
122
+ const index = dataTemp.findIndex((i) => i?.id === item?.id);
123
+ dataTemp.splice(index, 1, { ...item, ...itemActiveModal });
124
+ navigate({
125
+ name: Routes.ChooseConfig,
126
+ merge: true,
127
+ params: {
128
+ newSensorData: dataTemp,
129
+ },
130
+ });
131
+ }, [sensorData, item, itemActiveModal, navigate]);
132
+
133
+ const renderScroll = useMemo(() => {
134
+ return (
135
+ <HorizontalPicker
136
+ minimum={minimum}
137
+ maximum={maximum}
138
+ segmentSpacing={8}
139
+ segmentWidth={8}
140
+ step={10}
141
+ normalHeight={4}
142
+ normalWidth={4}
143
+ stepHeight={12}
144
+ stepWidth={12}
145
+ stepColor={Colors.Gray6}
146
+ normalColor={Colors.Gray6}
147
+ onChangeValue={onChangeValue}
148
+ value={value - minimum}
149
+ />
150
+ );
151
+ // eslint-disable-next-line react-hooks/exhaustive-deps
152
+ }, [minimum, maximum, value]);
153
+
154
+ useEffect(() => {
155
+ !hasNoValueEvaluation && setValue(1);
156
+ }, [hasNoValueEvaluation]);
157
+
158
+ useEffect(() => {
159
+ if (!hasLimit && maximum - 150 <= value * 10) {
160
+ setMaximum(maximum + 200);
161
+ }
162
+ // eslint-disable-next-line react-hooks/exhaustive-deps
163
+ }, [value]);
164
+
165
+ useEffect(() => {
166
+ if (item?.title) {
167
+ const itemTemp = conditionOptions.find((i) => i.title === item.title);
168
+ itemTemp && setItemActiveModal(itemTemp);
169
+ }
170
+ // eslint-disable-next-line react-hooks/exhaustive-deps
171
+ }, [item]);
172
+
173
+ useEffect(() => {
174
+ // eslint-disable-next-line react-hooks/rules-of-hooks
175
+ useStatusBarPreview(
176
+ isShowModal ? Colors.BlackTransparent4 : Colors.TextTransparent
177
+ );
178
+ }, [isShowModal]);
179
+
180
+ return (
181
+ <>
182
+ <NewActionWrapper
183
+ name={t('set_up {name}', { name: item?.name })}
184
+ nextTitle={t('save')}
185
+ onNext={onSave}
186
+ canNext={true}
187
+ >
188
+ <TouchableOpacity
189
+ onPress={onOpenModal}
190
+ style={[styles.itemCondition, styles.shadowView]}
191
+ >
192
+ <Text type="Body" color={Colors.Gray7}>
193
+ {t('condition')}
194
+ </Text>
195
+ <Text type="H4" semibold style={styles.description}>
196
+ {itemActiveModal?.title}
197
+ </Text>
198
+ <Image source={Images.arrowBack} style={styles.arrowRight} />
199
+ </TouchableOpacity>
200
+ {hasNoValueEvaluation && (
201
+ <>
202
+ <Text type="Body" color={Colors.Gray7} style={styles.setANumber}>
203
+ {t('set_a_number')}
204
+ </Text>
205
+ <View style={[styles.flexRow, styles.center]}>
206
+ <View>
207
+ <Text style={styles.value}>{value}</Text>
208
+ <View style={styles.underline} />
209
+ </View>
210
+ <Text type="H2" style={styles.unit}>
211
+ {item?.unit}
212
+ </Text>
213
+ </View>
214
+ {renderScroll}
215
+ </>
216
+ )}
217
+ </NewActionWrapper>
218
+ <ModalCustom
219
+ onBackdropPress={onCloseModal}
220
+ isVisible={isShowModal}
221
+ style={styles.modal}
222
+ >
223
+ <View style={styles.modalContent}>
224
+ <Text type="H3">{t('select_condition')}</Text>
225
+ {conditionOptions.map((option, index) => (
226
+ <React.Fragment key={index}>
227
+ <TouchableOpacity
228
+ onPress={onChooseCondition(option)}
229
+ key={index}
230
+ style={styles.itemModal}
231
+ >
232
+ <Text type="H4">{option.title}</Text>
233
+ </TouchableOpacity>
234
+ {index !== conditionOptions.length - 1 && (
235
+ <View style={styles.separated} />
236
+ )}
237
+ </React.Fragment>
238
+ ))}
239
+ </View>
240
+ </ModalCustom>
241
+ </>
242
+ );
243
+ };
244
+
245
+ export default SetupConfigCondition;
@@ -35,7 +35,7 @@ export default StyleSheet.create({
35
35
  marginHorizontal: 16,
36
36
  borderRadius: 8,
37
37
  },
38
- desciption: {
38
+ description: {
39
39
  marginTop: 4,
40
40
  },
41
41
  setANumber: {