@eohjsc/react-native-smart-city 0.7.3-rc17 → 0.7.3-rc18

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@eohjsc/react-native-smart-city",
3
3
  "title": "React Native Smart Home",
4
- "version": "0.7.3-rc17",
4
+ "version": "0.7.3-rc18",
5
5
  "description": "TODO",
6
6
  "main": "index.js",
7
7
  "files": [
@@ -48,18 +48,20 @@ const SliderRangeTemplate = memo(
48
48
  clearTimeout(timeout.current);
49
49
  if (value) {
50
50
  const data = parseInt(value, 10);
51
+ const numericValue = Math.min(max_value, Math.max(min_value, data));
52
+ setValue(numericValue);
51
53
  await doAction(
52
54
  action_data,
53
55
  JSON.stringify({
54
- value_brness: data,
55
- value: data,
56
+ value_brness: numericValue,
57
+ value: numericValue,
56
58
  })
57
59
  );
58
60
  }
59
61
  timeout.current = setTimeout(() => {
60
62
  setProcessing(false);
61
63
  }, 3000);
62
- }, [action_data, doAction, value]);
64
+ }, [action_data, doAction, max_value, min_value, value]);
63
65
 
64
66
  useEffect(() => {
65
67
  if (!processing) {
@@ -72,12 +74,10 @@ const SliderRangeTemplate = memo(
72
74
 
73
75
  const onInputChange = (text) => {
74
76
  if (text === '' || isNaN(text)) {
75
- setValue(min_value);
77
+ setValue(0);
76
78
  return;
77
79
  }
78
-
79
- const numericValue = Number(text);
80
- setValue(Math.min(max_value, Math.max(min_value, numericValue)));
80
+ setValue(Number(text));
81
81
  };
82
82
 
83
83
  return (
@@ -18,10 +18,11 @@ const ItemAutomate = ({
18
18
  enableScript = true,
19
19
  }) => {
20
20
  const t = useTranslations();
21
- const item = AUTOMATES[automate?.type];
22
- const isItemOneTap = AUTOMATES[automate?.type] === AUTOMATES.one_tap;
23
- const Icon = item?.icon;
24
- const valueEvaluations = useGetEvaluateValue(automate?.config, automate.unit);
21
+ const { type, config, unit } = automate;
22
+ const item = AUTOMATES[type];
23
+ const { icon: Icon, title, explanation } = item || {};
24
+ const isItemOneTap = AUTOMATES[type] === AUTOMATES.one_tap;
25
+ const valueEvaluations = useGetEvaluateValue(config, unit);
25
26
 
26
27
  const textCondition = useMemo(() => {
27
28
  return generateAutomationConditionText(automate, valueEvaluations, t);
@@ -33,9 +34,9 @@ const ItemAutomate = ({
33
34
  } else if (isSelected && isItemOneTap) {
34
35
  return t('quick_button_create_at_dashboard');
35
36
  } else {
36
- return t(item?.explanation);
37
+ return t(explanation);
37
38
  }
38
- }, [isItemOneTap, isSelected, item?.explanation, t, textCondition]);
39
+ }, [isItemOneTap, isSelected, explanation, t, textCondition]);
39
40
 
40
41
  const handleSelectIndex = useCallback(() => {
41
42
  if (onPress) {
@@ -63,7 +64,7 @@ const ItemAutomate = ({
63
64
  bold
64
65
  color={enableScript ? Colors.Black : Colors.Gray7}
65
66
  >
66
- {t(item?.title)}
67
+ {t(title)}
67
68
  </Text>
68
69
  <Text type="Label" color={Colors.Gray8} numberOfLines={1}>
69
70
  {renderText}
@@ -27,10 +27,9 @@ const ItemOneTap = memo(({ automate = {}, wrapSyles, onPressItem }) => {
27
27
 
28
28
  const goToDetail = useCallback(() => {
29
29
  navigate(Routes.ScriptDetail, {
30
- id,
31
30
  preAutomate: automate,
32
31
  });
33
- }, [automate, navigate, id]);
32
+ }, [automate, navigate]);
34
33
 
35
34
  const handleScriptAction = useCallback(async () => {
36
35
  const { success } = await axiosPost(API.AUTOMATE.ACTION_ONE_TAP(id));
@@ -107,7 +107,6 @@ describe('test Item', () => {
107
107
  await goDetail[0].props.onPress();
108
108
  });
109
109
  expect(global.mockedNavigate).toHaveBeenCalledWith(Routes.ScriptDetail, {
110
- id: 1,
111
110
  preAutomate: data.listAutomate[0].data[0],
112
111
  });
113
112
 
@@ -194,7 +193,6 @@ describe('test Item', () => {
194
193
  await goDetail[0].props.onPress();
195
194
  });
196
195
  expect(global.mockedNavigate).toHaveBeenCalledWith(Routes.ScriptDetail, {
197
- id: 1,
198
196
  preAutomate: data.listAutomate[0].data[0],
199
197
  });
200
198
  });
@@ -192,7 +192,7 @@ const SelectDeviceType = ({ route }) => {
192
192
  useEffect(() => {
193
193
  const fetchCountSummary = async () => {
194
194
  const { success, data } = await axiosGet(API.DEV_MODE.GATEWAY.COUNT(), {
195
- unit: unit?.id,
195
+ params: { unit: unit?.id },
196
196
  });
197
197
  if (success) {
198
198
  setUnitCountSummary(data);
@@ -31,7 +31,7 @@ const AddTypeSmart = ({ smartTypes, route }) => {
31
31
  closeScreen: closeScreen,
32
32
  };
33
33
 
34
- if (!unit && dataAutomate.type !== AUTOMATE_TYPE.SCHEDULE) {
34
+ if (!unit && dataAutomate.type === AUTOMATE_TYPE.VALUE_CHANGE) {
35
35
  navigate(Routes.SelectUnit, params);
36
36
  return;
37
37
  }
@@ -9,12 +9,14 @@ import styles from './InputNameStyles';
9
9
  import { useTranslations } from '../../../hooks/Common/useTranslations';
10
10
  import { axiosPost, axiosPut } from '../../../utils/Apis/axios';
11
11
  import NewActionWrapper from '../../Automate/AddNewAction/NewActionWrapper';
12
+ import Routes from '../../../utils/Route';
12
13
 
13
14
  const InputName = ({ title, placeholder }) => {
14
- const { automate, closeScreen } = useRoute().params;
15
+ const { automate = {}, closeScreen } = useRoute().params;
16
+ const { id: automateNameId, name: automateName } = automate;
15
17
  const t = useTranslations();
16
18
  const { navigate } = useNavigation();
17
- const [name, setName] = useState(automate?.name);
19
+ const [name, setName] = useState(automateName);
18
20
  const [processing, setProcessing] = useState(false);
19
21
  const handleContinue = useCallback(async () => {
20
22
  if (processing) {
@@ -29,21 +31,22 @@ const InputName = ({ title, placeholder }) => {
29
31
  name: name,
30
32
  };
31
33
 
32
- const { success, data } = automate?.id
33
- ? await axiosPut(API.AUTOMATE.UPDATE_AUTOMATE(automate?.id), params)
34
+ const { success, data } = automateNameId
35
+ ? await axiosPut(API.AUTOMATE.UPDATE_AUTOMATE(automateNameId), params)
34
36
  : await axiosPost(API.AUTOMATE.CREATE_AUTOMATE(), params);
35
37
 
36
38
  if (success) {
37
39
  navigate({
38
- name: closeScreen,
40
+ name: Routes.ScriptDetail,
39
41
  merge: true,
40
42
  params: {
41
- newAutomate: data,
43
+ preAutomate: data,
44
+ closeScreen: closeScreen,
42
45
  },
43
46
  });
44
47
  }
45
48
  setProcessing(false);
46
- }, [processing, automate, name, navigate, closeScreen]);
49
+ }, [processing, automate, name, automateNameId, navigate, closeScreen]);
47
50
 
48
51
  return (
49
52
  <NewActionWrapper
@@ -25,9 +25,12 @@ const MultiUnits = () => {
25
25
  const isFocused = useIsFocused();
26
26
  const { navigate } = useNavigation();
27
27
  const { params = {}, name: currentRouteName } = useRoute();
28
- const { unit, newAutomate } = params;
28
+ const { unit = {} } = params;
29
+ const { id: unitId, name } = unit;
29
30
  const [data, setData] = useState([]);
30
31
  const permissions = useBackendPermission();
32
+ const { smart_script_for_multi_unit, max_automations_per_unit } =
33
+ permissions || {};
31
34
 
32
35
  const [tabActive, setTabActive] = useState(AUTOMATE_TABS.SCENARIO);
33
36
 
@@ -37,8 +40,8 @@ const MultiUnits = () => {
37
40
 
38
41
  const getData = useCallback(
39
42
  async (fetchParams) => {
40
- if (unit?.id) {
41
- await fetchWithCache(API.UNIT.AUTOMATE(unit?.id), {}, (response) => {
43
+ if (unitId) {
44
+ await fetchWithCache(API.UNIT.AUTOMATE(unitId), {}, (response) => {
42
45
  const { success, data: automateData } = response;
43
46
  success && setData(automateData);
44
47
  });
@@ -50,7 +53,7 @@ const MultiUnits = () => {
50
53
  success && setData(automateData);
51
54
  }
52
55
  },
53
- [unit?.id]
56
+ [unitId]
54
57
  );
55
58
 
56
59
  const onPressTabName = (tab) => () => {
@@ -62,32 +65,26 @@ const MultiUnits = () => {
62
65
  navigate(Routes.UnitStack, {
63
66
  screen: Routes.ScriptDetail,
64
67
  params: {
65
- id: item?.id,
66
- unitId: unit?.id,
67
68
  preAutomate: item,
68
69
  },
69
70
  });
70
71
  },
71
- [navigate, unit?.id]
72
+ [navigate]
72
73
  );
73
74
 
74
- useEffect(() => {
75
- newAutomate && onPressItem(newAutomate);
76
- }, [newAutomate, onPressItem]);
77
-
78
75
  const handleOnAddNew = useCallback(() => {
79
- if (unit?.id) {
80
- if (permissions?.max_automations_per_unit <= data.length) {
76
+ if (unitId) {
77
+ if (max_automations_per_unit <= data.length) {
81
78
  ToastBottomHelper.error(
82
79
  t('reach_max_automations_per_unit', {
83
- length: permissions.max_automations_per_unit,
80
+ length: max_automations_per_unit,
84
81
  }),
85
82
  '',
86
83
  7000
87
84
  );
88
85
  return;
89
86
  }
90
- } else if (!permissions?.smart_script_for_multi_unit) {
87
+ } else if (!smart_script_for_multi_unit) {
91
88
  ToastBottomHelper.error(
92
89
  t('no_permission_smart_script_for_multi_unit'),
93
90
  '',
@@ -99,7 +96,7 @@ const MultiUnits = () => {
99
96
  navigate(Routes.UnitStack, {
100
97
  screen: Routes.ScenarioName,
101
98
  params: {
102
- automate: { type: AUTOMATE_TYPE.ONE_TAP, unit: unit?.id },
99
+ automate: { type: AUTOMATE_TYPE.ONE_TAP, unit: unitId },
103
100
  closeScreen: currentRouteName,
104
101
  },
105
102
  });
@@ -107,9 +104,9 @@ const MultiUnits = () => {
107
104
  }
108
105
 
109
106
  navigate(Routes.UnitStack, {
110
- screen: Routes.AddUnknownTypeSmart,
107
+ screen: Routes.AddAutomationTypeSmart,
111
108
  params: {
112
- automate: { unit: unit?.id },
109
+ automate: { unit: unitId },
113
110
  closeScreen: currentRouteName,
114
111
  },
115
112
  });
@@ -117,11 +114,11 @@ const MultiUnits = () => {
117
114
  currentRouteName,
118
115
  data.length,
119
116
  navigate,
120
- permissions?.max_automations_per_unit,
121
- permissions?.smart_script_for_multi_unit,
117
+ max_automations_per_unit,
118
+ smart_script_for_multi_unit,
122
119
  t,
123
120
  tabActive,
124
- unit?.id,
121
+ unitId,
125
122
  ]);
126
123
 
127
124
  const renderContent = useMemo(() => {
@@ -166,7 +163,7 @@ const MultiUnits = () => {
166
163
  return (
167
164
  <View style={styles.wrap}>
168
165
  <WrapHeaderScrollable
169
- title={unit?.id ? unit?.name : t('multi_units_automate')}
166
+ title={unitId ? name : t('multi_units_automate')}
170
167
  headerAniStyle={styles.headerAniStyle}
171
168
  >
172
169
  <View style={styles.wrapContent}>
@@ -81,7 +81,8 @@ describe('test OneTap', () => {
81
81
  merge: true,
82
82
  name: Routes.ScriptDetail,
83
83
  params: {
84
- newAutomate: {
84
+ closeScreen: Routes.ScriptDetail,
85
+ preAutomate: {
85
86
  id: 1,
86
87
  script: { id: 1, name: 'William Miller' },
87
88
  type: 'one_tap',
@@ -45,11 +45,10 @@ const ScriptDetail = ({ route }) => {
45
45
  usePopover();
46
46
  const t = useTranslations();
47
47
  const {
48
- id,
49
48
  saveAt,
50
49
  preAutomate = {}, // pre-loaded automate data
51
- newAutomate, // updated automate data
52
50
  newActionsList, // updated actions list
51
+ closeScreen,
53
52
  } = params;
54
53
  const [automate, setAutomate] = useState(preAutomate);
55
54
  const isFocused = useIsFocused();
@@ -63,7 +62,7 @@ const ScriptDetail = ({ route }) => {
63
62
  const [enableScript, setEnableScript] = useState(enable);
64
63
  const onShowActivityLog = useCallback(() => {
65
64
  navigate(Routes.ActivityLog, {
66
- id: id,
65
+ id: automateId,
67
66
  type:
68
67
  type === AUTOMATE_TYPE.ONE_TAP
69
68
  ? `automate.${AUTOMATE_TYPE.ONE_TAP}`
@@ -74,7 +73,7 @@ const ScriptDetail = ({ route }) => {
74
73
  user: Boolean(unit),
75
74
  },
76
75
  });
77
- }, [navigate, id, type, unit]);
76
+ }, [navigate, automateId, type, unit]);
78
77
 
79
78
  const listMenuItem = useMemo(
80
79
  () => [
@@ -107,39 +106,41 @@ const ScriptDetail = ({ route }) => {
107
106
 
108
107
  const fetchAutomateActions = useCallback(async () => {
109
108
  const { success, data: automateData } = await axiosGet(
110
- API.AUTOMATE.SCRIPT_ITEMS(id)
109
+ API.AUTOMATE.SCRIPT_ITEMS(automateId)
111
110
  );
112
111
  if (success) {
113
112
  setData(automateData.script_items || []);
114
113
  }
115
- }, [id]);
114
+ }, [automateId]);
116
115
 
117
116
  const fetchAutomate = useCallback(async () => {
118
117
  const { success, data: automateData } = await axiosGet(
119
- API.AUTOMATE.FETCH_AUTOMATE(id)
118
+ API.AUTOMATE.FETCH_AUTOMATE(automateId)
120
119
  );
121
120
  if (success) {
122
121
  setAutomate(automateData);
123
122
  setEnableScript(automateData.script.enable);
124
123
  }
125
- }, [id]);
124
+ }, [automateId]);
126
125
 
127
126
  const onPressEdit = useCallback(() => {
128
127
  navigate(Routes.EditActionsList, {
129
128
  data,
130
- id,
129
+ id: automateId,
131
130
  unitId: automate.unit,
132
131
  });
133
- }, [navigate, data, id, automate.unit]);
132
+ }, [navigate, data, automateId, automate.unit]);
134
133
 
135
134
  const handleScriptAction = useCallback(async () => {
136
- const { success } = await axiosPost(API.AUTOMATE.ACTION_ONE_TAP(id));
135
+ const { success } = await axiosPost(
136
+ API.AUTOMATE.ACTION_ONE_TAP(automateId)
137
+ );
137
138
  if (success) {
138
139
  ToastBottomHelper.success(t('activated_successfully'));
139
140
  } else {
140
141
  ToastBottomHelper.error(t('activation_failed'));
141
142
  }
142
- }, [id, t]);
143
+ }, [automateId, t]);
143
144
 
144
145
  const handleUpdateAutomate = useCallback(async () => {
145
146
  if (!can_edit) {
@@ -158,6 +159,19 @@ const ScriptDetail = ({ route }) => {
158
159
  });
159
160
  }, [automate, can_edit, enableScript, navigate, route.name, t]);
160
161
 
162
+ const handleGoBack = useCallback(async () => {
163
+ if (closeScreen === Routes.UnitDetail) {
164
+ navigate(closeScreen, { unitId: unit });
165
+ } else if (
166
+ closeScreen === Routes.MultiUnits ||
167
+ closeScreen === Routes.Automate
168
+ ) {
169
+ navigate(closeScreen, {});
170
+ } else {
171
+ goBack();
172
+ }
173
+ }, [closeScreen, goBack, navigate, unit]);
174
+
161
175
  const onChangeSwitch = useCallback(
162
176
  async (checked) => {
163
177
  setEnableScript(checked);
@@ -210,17 +224,13 @@ const ScriptDetail = ({ route }) => {
210
224
  saveAt && fetchAutomateActions();
211
225
  }, [saveAt, fetchAutomateActions]);
212
226
 
213
- useEffect(() => {
214
- newAutomate && setAutomate(newAutomate);
215
- }, [newAutomate]);
216
-
217
227
  return (
218
228
  <View style={styles.wrap}>
219
229
  <WrapHeaderScrollable
220
230
  title={name}
221
231
  headerAniStyle={styles.headerAniStyle}
222
232
  rightComponent={rightComponent}
223
- onGoBack={goBack}
233
+ onGoBack={handleGoBack}
224
234
  >
225
235
  <View style={styles.wrapContent}>
226
236
  {!!can_edit && (
@@ -76,7 +76,6 @@ describe('Test MultiUnits', () => {
76
76
  params: {
77
77
  isMultiUnits: true,
78
78
  unitName: null,
79
- unit: null,
80
79
  },
81
80
  });
82
81
  await act(async () => {
@@ -111,7 +110,6 @@ describe('Test MultiUnits', () => {
111
110
  expect(global.mockedNavigate).toBeCalledWith(Routes.UnitStack, {
112
111
  screen: Routes.ScriptDetail,
113
112
  params: {
114
- id: response[1].id,
115
113
  preAutomate: response[1],
116
114
  },
117
115
  });
@@ -120,7 +118,7 @@ describe('Test MultiUnits', () => {
120
118
  ItemAddNews[0].props.onAddNew();
121
119
  });
122
120
  expect(global.mockedNavigate).toBeCalledWith(Routes.UnitStack, {
123
- screen: Routes.AddUnknownTypeSmart,
121
+ screen: Routes.AddAutomationTypeSmart,
124
122
  params: {
125
123
  automate: { unit: undefined },
126
124
  closeScreen: undefined,
@@ -264,7 +262,6 @@ describe('Test MultiUnits', () => {
264
262
  params: {
265
263
  isMultiUnits: true,
266
264
  unitName: null,
267
- unit: null,
268
265
  },
269
266
  });
270
267
  await act(async () => {
@@ -173,7 +173,6 @@ describe('Test Automate', () => {
173
173
  params: {
174
174
  preAutomate: response.data[0].automates[0],
175
175
  closeScreen: undefined,
176
- id: 1,
177
176
  },
178
177
  });
179
178
  });
@@ -75,7 +75,6 @@ const Automate = () => {
75
75
  navigate(Routes.UnitStack, {
76
76
  screen: Routes.ScriptDetail,
77
77
  params: {
78
- id: item?.id,
79
78
  closeScreen: currentRouteName,
80
79
  preAutomate: item, // pre-loaded automate data
81
80
  },
@@ -129,7 +129,7 @@ export const useGetEvaluateValue = (configId, unitId) => {
129
129
  if (configId && valueEvaluations[configId] === undefined) {
130
130
  (async () => {
131
131
  const { success, data } = await axiosGet(API.VALUE_EVALUATIONS(), {
132
- configs__id: configId,
132
+ params: { configs__id: configId },
133
133
  });
134
134
  if (success) {
135
135
  setAction(Action.INIT_VALUE_EVALUATIONS, {
@@ -438,7 +438,7 @@ const NotificationItem = memo(({ item }) => {
438
438
  navigation.navigate(Routes.UnitStack, {
439
439
  screen: Routes.ScriptDetail,
440
440
  params: {
441
- id: automate_id,
441
+ preAutomate: { id: automate_id },
442
442
  },
443
443
  });
444
444
  },
@@ -158,7 +158,7 @@ describe('Test Select unit screen', () => {
158
158
  await iconClose[0].props.onPress();
159
159
  });
160
160
  expect(global.mockedNavigate).toHaveBeenCalledWith(Routes.ScriptDetail, {
161
- id: 1,
161
+ automate: { id: 1 },
162
162
  havePermission: true,
163
163
  isMultiUnits: undefined,
164
164
  name: '1',
@@ -50,7 +50,7 @@ const SelectUnit = () => {
50
50
  const handleOnGoBackAndClose = useCallback(() => {
51
51
  if (automateId) {
52
52
  navigate(Routes.ScriptDetail, {
53
- id: automateId,
53
+ automate: { id: automateId },
54
54
  name: scriptName,
55
55
  type: oldType,
56
56
  havePermission: true,