@eohjsc/react-native-smart-city 0.7.20 → 0.7.21

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.
@@ -7,18 +7,15 @@ import { showAllGridItems } from '../../../../commons/ActionTemplate/__test__/ut
7
7
  import TextInput from '../../../../commons/Form/TextInput';
8
8
  import GridItem from '../../../../commons/Grid/GridItem';
9
9
  import { ModalCustom } from '../../../../commons/Modal';
10
- import { SCProvider } from '../../../../context';
10
+ import { SCProvider, useSCContextSelector } from '../../../../context';
11
11
  import { mockSCStore } from '../../../../context/mockStore';
12
12
  import Routes from '../../../../utils/Route';
13
13
  import { ToastBottomHelper } from '../../../../utils/Utils';
14
14
  import SetupConfigCondition from '../SetupConfigCondition';
15
-
16
- jest.mock('react', () => {
17
- return {
18
- ...jest.requireActual('react'),
19
- memo: (x) => x,
20
- };
21
- });
15
+ import AccessibilityLabel from '../../../../configs/AccessibilityLabel';
16
+ import { API } from '../../../../configs';
17
+ import api from '../../../../utils/Apis/axios';
18
+ import MockAdapter from 'axios-mock-adapter';
22
19
 
23
20
  const BOOLEAN_VALUE_EVALUATION_CONFIGURATION = {
24
21
  id: 1,
@@ -39,10 +36,25 @@ const wrapComponent = (configuration, onPress) => (
39
36
  </SCProvider>
40
37
  );
41
38
 
39
+ jest.mock('../../../../context', () => ({
40
+ ...jest.requireActual('../../../../context'),
41
+ useSCContextSelector: jest.fn(),
42
+ }));
43
+
44
+ const mock = new MockAdapter(api.axiosInstance);
45
+
42
46
  describe('Test SetupConfigCondition', () => {
43
47
  const spyToastError = jest.spyOn(ToastBottomHelper, 'error');
44
48
 
45
49
  let tree;
50
+ useSCContextSelector.mockImplementation((selector) =>
51
+ selector({
52
+ automate: {
53
+ isCreateCondition: false,
54
+ isUpdateCondition: false,
55
+ },
56
+ })
57
+ );
46
58
  beforeEach(() => {
47
59
  tree = null;
48
60
  spyToastError.mockClear();
@@ -150,6 +162,14 @@ describe('Test SetupConfigCondition', () => {
150
162
  });
151
163
 
152
164
  it('choose config value evaluation', async () => {
165
+ useSCContextSelector.mockImplementation((selector) =>
166
+ selector({
167
+ automate: {
168
+ isCreateCondition: false,
169
+ isUpdateCondition: false,
170
+ },
171
+ })
172
+ );
153
173
  useRoute.mockReturnValue({
154
174
  params: {
155
175
  automate: {},
@@ -183,6 +203,14 @@ describe('Test SetupConfigCondition', () => {
183
203
  });
184
204
 
185
205
  const testCustomCondition = async (value) => {
206
+ useSCContextSelector.mockImplementation((selector) =>
207
+ selector({
208
+ automate: {
209
+ isCreateCondition: false,
210
+ isUpdateCondition: false,
211
+ },
212
+ })
213
+ );
186
214
  useRoute.mockReturnValue({
187
215
  params: {
188
216
  automate: {},
@@ -225,7 +253,7 @@ describe('Test SetupConfigCondition', () => {
225
253
  });
226
254
 
227
255
  const bottomButtonView = instance.findByProps({
228
- testID: 'continue_button',
256
+ accessibilityLabel: AccessibilityLabel.BUTTON_CONTINUE,
229
257
  });
230
258
 
231
259
  await act(async () => {
@@ -273,7 +301,7 @@ describe('Test SetupConfigCondition', () => {
273
301
  });
274
302
 
275
303
  const bottomButtonView = instance.findByProps({
276
- testID: 'continue_button',
304
+ accessibilityLabel: AccessibilityLabel.BUTTON_CONTINUE,
277
305
  });
278
306
 
279
307
  await act(async () => {
@@ -333,4 +361,81 @@ describe('Test SetupConfigCondition', () => {
333
361
  'Customize conditions'
334
362
  );
335
363
  });
364
+
365
+ it('test isCreateCondition success', async () => {
366
+ useSCContextSelector.mockImplementation((selector) =>
367
+ selector({
368
+ automate: {
369
+ isCreateCondition: true,
370
+ isUpdateCondition: false,
371
+ },
372
+ })
373
+ );
374
+ useRoute.mockReturnValue({
375
+ params: {
376
+ config: {
377
+ id: 1,
378
+ name: 'config name',
379
+ title: 'is below (<)',
380
+ value_evaluations: [BOOLEAN_VALUE_EVALUATION_CONFIGURATION],
381
+ },
382
+ automate: { id: 1 },
383
+ sensorData: [],
384
+ },
385
+ });
386
+ mock.onPost(API.AUTOMATE.ADD_CONDITION(1)).reply(200);
387
+ await act(async () => {
388
+ tree = await create(wrapComponent());
389
+ });
390
+ const instance = tree.root;
391
+
392
+ const gridItems = instance.findAllByType(GridItem);
393
+ expect(gridItems).toHaveLength(3);
394
+ await act(async () => {
395
+ gridItems[0].props.onPress();
396
+ });
397
+
398
+ expect(global.mockedNavigate).toBeCalledWith({
399
+ name: 'ScriptDetail',
400
+ params: {
401
+ preAutomate: { condition: 'value_evaluation', id: 1, value: [1, 0] },
402
+ },
403
+ });
404
+ });
405
+
406
+ it('test isUpdateCondition fail', async () => {
407
+ useSCContextSelector.mockImplementation((selector) =>
408
+ selector({
409
+ automate: {
410
+ isCreateCondition: false,
411
+ isUpdateCondition: 1,
412
+ },
413
+ })
414
+ );
415
+ useRoute.mockReturnValue({
416
+ params: {
417
+ config: {
418
+ id: 1,
419
+ name: 'config name',
420
+ title: 'is below (<)',
421
+ value_evaluations: [BOOLEAN_VALUE_EVALUATION_CONFIGURATION],
422
+ },
423
+ automate: { id: 1 },
424
+ sensorData: [],
425
+ },
426
+ });
427
+ mock.onPut(API.AUTOMATE.UPDATE_CONDITION(1, 1)).reply(400);
428
+ await act(async () => {
429
+ tree = await create(wrapComponent());
430
+ });
431
+ const instance = tree.root;
432
+
433
+ const gridItems = instance.findAllByType(GridItem);
434
+ expect(gridItems).toHaveLength(3);
435
+ await act(async () => {
436
+ gridItems[0].props.onPress();
437
+ });
438
+
439
+ expect(global.mockedNavigate).not.toBeCalled();
440
+ });
336
441
  });
@@ -1,5 +1,5 @@
1
1
  import { useNavigation } from '@react-navigation/native';
2
- import React, { useCallback, useState } from 'react';
2
+ import React, { useCallback, useContext, useState } from 'react';
3
3
  import { View } from 'react-native';
4
4
  import ItemAutomate from '../../../commons/Automate/ItemAutomate';
5
5
  import { HeaderCustom } from '../../../commons/Header';
@@ -8,9 +8,12 @@ import { useTranslations } from '../../../hooks/Common/useTranslations';
8
8
  import Routes from '../../../utils/Route';
9
9
  import styles from './styles/AddNewAutoSmartStyles';
10
10
  import { AUTOMATE_TYPE } from '../../../configs/Constants';
11
+ import { SCContext } from '../../../context';
12
+ import { Action } from '../../../context/actionType';
11
13
 
12
14
  const AddTypeSmart = ({ smartTypes, route }) => {
13
15
  const t = useTranslations();
16
+ const { setAction } = useContext(SCContext);
14
17
  const { navigate, goBack } = useNavigation();
15
18
  const { automate = {}, closeScreen } = route?.params || {};
16
19
  const { id, unit, type } = automate;
@@ -30,7 +33,8 @@ const AddTypeSmart = ({ smartTypes, route }) => {
30
33
  unitId: unit,
31
34
  closeScreen: closeScreen,
32
35
  };
33
-
36
+ setAction(Action.IS_CREATE_CONDITION, false);
37
+ setAction(Action.IS_UPDATE_CONDITION, false);
34
38
  if (!unit && dataAutomate.type === AUTOMATE_TYPE.VALUE_CHANGE) {
35
39
  navigate(Routes.SelectUnit, params);
36
40
  return;
@@ -38,7 +42,7 @@ const AddTypeSmart = ({ smartTypes, route }) => {
38
42
 
39
43
  navigate(dataAutomate.route, params);
40
44
  },
41
- [smartTypes, automate, unit, closeScreen, navigate]
45
+ [smartTypes, automate, unit, closeScreen, setAction, navigate]
42
46
  );
43
47
 
44
48
  const onSelectType = (index) => {
@@ -0,0 +1,51 @@
1
+ import React, { useCallback } from 'react';
2
+ import { API, Colors } from '../../../../configs';
3
+ import AlertAction from '../../../../commons/AlertAction';
4
+ import { axiosDelete } from '../../../../utils/Apis/axios';
5
+ import { ToastBottomHelper } from '../../../../utils/Utils';
6
+ import { useTranslations } from '../../../../hooks/Common/useTranslations';
7
+ import AccessibilityLabel from '../../../../configs/AccessibilityLabel';
8
+
9
+ const DeleteCondition = ({
10
+ automate,
11
+ setAutomate,
12
+ condition,
13
+ isVisible,
14
+ setIsVisible,
15
+ }) => {
16
+ const t = useTranslations();
17
+ const deleteScript = useCallback(async () => {
18
+ const { success } = await axiosDelete(
19
+ API.AUTOMATE.DELETE_CONDITION(automate.id, condition.id)
20
+ );
21
+ if (success) {
22
+ ToastBottomHelper.success(t('removed_successfully'));
23
+ const conditions = automate.conditions.filter(
24
+ (el) => el.id !== condition.id
25
+ );
26
+ setAutomate((prev) => ({ ...prev, conditions }));
27
+ } else {
28
+ ToastBottomHelper.error(t('remove_failed'));
29
+ }
30
+ setIsVisible({});
31
+ }, [automate, condition.id, setAutomate, setIsVisible, t]);
32
+
33
+ const hidePopup = () => setIsVisible({});
34
+
35
+ return (
36
+ <AlertAction
37
+ visible={isVisible}
38
+ hideModal={hidePopup}
39
+ title={t('title_delete_condition')}
40
+ message={t('message_delete_condition')}
41
+ leftButtonTitle={t('cancel')}
42
+ leftButtonClick={hidePopup}
43
+ rightButtonTitle={t('remove')}
44
+ rightButtonClick={deleteScript}
45
+ rightButtonStyle={{ color: Colors.Red6 }}
46
+ accessibilityLabel={AccessibilityLabel.AUTOMATE_DELETE_CONDITION}
47
+ />
48
+ );
49
+ };
50
+
51
+ export default DeleteCondition;
@@ -0,0 +1,196 @@
1
+ import React, { memo, useContext, useMemo } from 'react';
2
+
3
+ import { useRoute } from '@react-navigation/native';
4
+
5
+ import { ModalCustom } from '../../../../commons/Modal';
6
+ import { StyleSheet, View } from 'react-native';
7
+ import { Colors } from '../../../../configs';
8
+ import { useTranslations } from '../../../../hooks/Common/useTranslations';
9
+ import { useBackendPermission } from '../../../../utils/Permission/backend';
10
+ import ValueChange from '../../../../../assets/images/Automation-fill.svg';
11
+ import Schedule from '../../../../../assets/images/Schedule.svg';
12
+ import { TouchableOpacity } from 'react-native';
13
+ import { Text } from '../../../../commons';
14
+ import Routes from '../../../../utils/Route';
15
+ import AccessibilityLabel from '../../../../configs/AccessibilityLabel';
16
+ import { SCContext } from '../../../../context';
17
+ import { Action } from '../../../../context/actionType';
18
+
19
+ const ModalAddCondition = memo(
20
+ ({ automate, isVisible, setIsVisible, numberActionAdded, navigate }) => {
21
+ const t = useTranslations();
22
+ const { setAction } = useContext(SCContext);
23
+ const { unit } = automate;
24
+ const { name: currentScreenName } = useRoute();
25
+ const permissions = useBackendPermission();
26
+ const { max_actions_per_automation } = permissions || {};
27
+
28
+ const listItem = useMemo(() => {
29
+ return [
30
+ {
31
+ id: 'time_frame',
32
+ text: t('time_frame'),
33
+ example: t('ex_time_frame'),
34
+ image: <Schedule />,
35
+ onClick: () => {
36
+ setIsVisible(false);
37
+ navigate(Routes.AddEditConditionSchedule, {
38
+ automate: automate,
39
+ closeScreen: Routes.ScriptDetail,
40
+ });
41
+ },
42
+ },
43
+ {
44
+ id: 'when_value_change',
45
+ text: t('when_value_change'),
46
+ example: t('ex_when_value_change'),
47
+ image: <ValueChange />,
48
+ onClick: () => {
49
+ setIsVisible(false);
50
+ const navParams = {
51
+ automate: automate,
52
+ closeScreen: currentScreenName,
53
+ numberActionCanAdd:
54
+ max_actions_per_automation - numberActionAdded,
55
+ routeName: unit ? null : Routes.SelectControlDevices,
56
+ };
57
+ setAction(Action.IS_UPDATE_CONDITION, false);
58
+ setAction(Action.IS_CREATE_CONDITION, true);
59
+ navigate(Routes.SelectMonitorDevices, navParams);
60
+ if (unit) {
61
+ navigate(Routes.SelectMonitorDevices, navParams);
62
+ } else {
63
+ navigate(Routes.SelectUnit, {
64
+ automate,
65
+ routeName: Routes.SelectMonitorDevices,
66
+ });
67
+ }
68
+ },
69
+ },
70
+ ];
71
+ }, [
72
+ automate,
73
+ currentScreenName,
74
+ max_actions_per_automation,
75
+ navigate,
76
+ numberActionAdded,
77
+ setAction,
78
+ setIsVisible,
79
+ t,
80
+ unit,
81
+ ]);
82
+
83
+ const hidePopup = () => setIsVisible(false);
84
+
85
+ return (
86
+ <ModalCustom
87
+ isVisible={isVisible}
88
+ onBackButtonPress={hidePopup}
89
+ onBackdropPress={hidePopup}
90
+ style={styles.container}
91
+ >
92
+ <View style={styles.popoverStyle}>
93
+ <View style={styles.modalWrapper}>
94
+ <View style={styles.modalHeader}>
95
+ <Text style={styles.modalHeaderText} semibold>
96
+ {t('add_condition')}
97
+ </Text>
98
+ </View>
99
+ {listItem.map((item, index) => {
100
+ return (
101
+ <View style={styles.rowItem} key={`rowItem-${index}`}>
102
+ <TouchableOpacity
103
+ onPress={item.onClick}
104
+ accessibilityLabel={
105
+ AccessibilityLabel.AUTOMATE_LIST_SCRIPT_ACTION
106
+ }
107
+ >
108
+ <View style={styles.wapItem}>
109
+ <View style={styles.imageItem}>{item.image}</View>
110
+ <View style={styles.colItem}>
111
+ <Text type="Body" semibold>
112
+ {item.text}
113
+ </Text>
114
+ <Text style={styles.textItem} numberOfLines={1}>
115
+ {item.example}
116
+ </Text>
117
+ </View>
118
+ </View>
119
+ </TouchableOpacity>
120
+ </View>
121
+ );
122
+ })}
123
+ </View>
124
+ </View>
125
+ </ModalCustom>
126
+ );
127
+ }
128
+ );
129
+
130
+ export default ModalAddCondition;
131
+
132
+ const styles = StyleSheet.create({
133
+ container: {
134
+ flex: 1,
135
+ margin: 0,
136
+ },
137
+ popoverStyle: {
138
+ width: '100%',
139
+ backgroundColor: Colors.White,
140
+ bottom: 0,
141
+ left: 0,
142
+ position: 'absolute',
143
+ borderRadius: 10,
144
+ paddingBottom: 50,
145
+ },
146
+ modalWrapper: {
147
+ flex: 1,
148
+ flexDirection: 'column',
149
+ backgroundColor: Colors.White,
150
+ borderRadius: 10,
151
+ },
152
+ modalHeader: {
153
+ padding: 16,
154
+ marginBottom: 10,
155
+ backgroundColor: Colors.White,
156
+ alignItems: 'center',
157
+ justifyContent: 'center',
158
+ borderBottomWidth: 1,
159
+ borderTopLeftRadius: 20,
160
+ borderTopRightRadius: 20,
161
+ borderColor: Colors.Gray4,
162
+ },
163
+ modalHeaderText: {
164
+ fontSize: 16,
165
+ lineHeight: 24,
166
+ color: Colors.Gray9,
167
+ },
168
+ rowItem: {
169
+ padding: 16,
170
+ marginBottom: 10,
171
+ marginHorizontal: 10,
172
+ backgroundColor: Colors.White,
173
+ borderRadius: 16,
174
+ borderWidth: 1,
175
+ borderColor: Colors.Gray4,
176
+ },
177
+ wapItem: {
178
+ flexDirection: 'row',
179
+ alignItems: 'center',
180
+ justifyContent: 'center',
181
+ },
182
+ imageItem: {
183
+ width: '10%',
184
+ alignItems: 'center',
185
+ justifyContent: 'center',
186
+ marginRight: 20,
187
+ },
188
+ colItem: {
189
+ flex: 1,
190
+ flexDirection: 'column',
191
+ },
192
+ textItem: {
193
+ color: Colors.Gray8,
194
+ fontSize: 12,
195
+ },
196
+ });
@@ -57,9 +57,15 @@ export default StyleSheet.create({
57
57
  },
58
58
  editButton: {
59
59
  height: 40,
60
+ paddingLeft: 40,
60
61
  justifyContent: 'center',
61
62
  alignItems: 'center',
62
63
  },
64
+ rowActionList: {
65
+ flexDirection: 'row',
66
+ alignItems: 'center',
67
+ justifyContent: 'space-between',
68
+ },
63
69
  moreButton: {
64
70
  marginLeft: -5,
65
71
  },
@@ -183,6 +189,12 @@ export default StyleSheet.create({
183
189
  justifyContent: 'center',
184
190
  alignItems: 'center',
185
191
  },
192
+ width30: {
193
+ width: '30%',
194
+ },
195
+ width40: {
196
+ width: '40%',
197
+ },
186
198
  width50: {
187
199
  width: '50%',
188
200
  },
@@ -198,6 +210,12 @@ export default StyleSheet.create({
198
210
  maxWidth: 190,
199
211
  marginRight: 5,
200
212
  },
213
+ boxCondition: {
214
+ flexDirection: 'row',
215
+ alignItems: 'center',
216
+ maxWidth: 210,
217
+ marginRight: 5,
218
+ },
201
219
  card: {
202
220
  marginRight: 0,
203
221
  padding: 10,