@eohjsc/react-native-smart-city 0.7.22 → 0.7.24

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 (61) hide show
  1. package/package.json +1 -1
  2. package/src/commons/ActionGroup/TerminalBoxTemplate.js +3 -0
  3. package/src/commons/ActionTemplate/OnOffButtonAction.js +38 -4
  4. package/src/commons/ActionTemplate/OnOffSimpleAction.js +55 -15
  5. package/src/commons/ActionTemplate/OnOffSmartLockAction.js +46 -8
  6. package/src/commons/ActionTemplate/SwitchButtonAction.js +35 -4
  7. package/src/commons/ActionTemplate/ThreeButtonAction.js +13 -3
  8. package/src/commons/ActionTemplate/__test__/OnOffButtonAction.test.js +46 -7
  9. package/src/commons/ActionTemplate/__test__/OnOffSimpleAction.test.js +66 -6
  10. package/src/commons/ActionTemplate/__test__/OnOffSmartLockAction.test.js +53 -13
  11. package/src/commons/ActionTemplate/__test__/SwitchButtonAction.test.js +46 -7
  12. package/src/commons/ActionTemplate/__test__/index.test.js +6 -2
  13. package/src/commons/ActionTemplate/index.js +65 -10
  14. package/src/commons/MediaPlayerDetail/MediaPlayerFull.js +26 -32
  15. package/src/commons/OneTapTemplate/StatesGridActionTemplate.js +8 -6
  16. package/src/commons/SubUnit/OneTap/__test__/SubUnitAutomate.test.js +6 -0
  17. package/src/commons/SubUnit/OneTap/index.js +5 -0
  18. package/src/commons/UnitSummary/ConfigHistoryChart/index.js +16 -12
  19. package/src/commons/Widgets/IFrameWithConfig/IFrameWithConfig.js +3 -1
  20. package/src/commons/Widgets/IFrameWithConfig/__tests__/IFrameWithConfig.test.js +1 -1
  21. package/src/configs/API.js +6 -0
  22. package/src/configs/AccessibilityLabel.js +2 -0
  23. package/src/screens/ActivityLog/__test__/index.test.js +10 -0
  24. package/src/screens/ActivityLog/hooks/index.js +1 -1
  25. package/src/screens/Automate/AddNewAction/ChooseAction.js +15 -51
  26. package/src/screens/Automate/AddNewAction/SelectControlDevices.js +13 -3
  27. package/src/screens/Automate/AddNewAction/SetupConfigCondition.js +74 -54
  28. package/src/screens/Automate/AddNewAction/__test__/ChooseAction.test.js +114 -4
  29. package/src/screens/Automate/AddNewAction/__test__/SetupConfigCondition.test.js +37 -8
  30. package/src/screens/Automate/AddNewAutoSmart/AddTypeSmart.js +5 -0
  31. package/src/screens/Automate/AddNewAutoSmart/__test__/AddNewAutoSmart.test.js +18 -2
  32. package/src/screens/Automate/Components/InputName.js +7 -6
  33. package/src/screens/Automate/Constants.js +12 -0
  34. package/src/screens/Automate/EditActionsList/UpdateActionScript.js +24 -55
  35. package/src/screens/Automate/EditActionsList/__tests__/UpdateActionScript.test.js +298 -41
  36. package/src/screens/Automate/EditActionsList/__tests__/index.test.js +2 -2
  37. package/src/screens/Automate/EditActionsList/index.js +26 -14
  38. package/src/screens/Automate/MultiUnits.js +9 -1
  39. package/src/screens/Automate/OneTap/__test__/AddNewOneTap.test.js +3 -3
  40. package/src/screens/Automate/ScriptDetail/Components/AddActionScript.js +4 -10
  41. package/src/screens/Automate/ScriptDetail/Components/DeleteScript.js +2 -4
  42. package/src/screens/Automate/ScriptDetail/__test__/index.test.js +78 -0
  43. package/src/screens/Automate/ScriptDetail/index.js +16 -10
  44. package/src/screens/Automate/ScriptDetail/utils.js +39 -15
  45. package/src/screens/Automate/SetSchedule/AddEditConditionSchedule.js +27 -160
  46. package/src/screens/Automate/SetSchedule/EditSchedule.js +269 -0
  47. package/src/screens/Automate/SetSchedule/__test__/AddEditConditionSchedule.test.js +327 -22
  48. package/src/screens/Automate/SetSchedule/__test__/index.test.js +35 -22
  49. package/src/screens/Automate/SetSchedule/components/RepeatOptionsPopup.js +2 -8
  50. package/src/screens/Automate/SetSchedule/index.js +15 -129
  51. package/src/screens/Automate/SetSchedule/styles/indexStyles.js +9 -0
  52. package/src/screens/Automate/__test__/MultiUnits.test.js +6 -1
  53. package/src/screens/Automate/hooks/useAction.js +222 -0
  54. package/src/screens/Device/__test__/detail.test.js +48 -1
  55. package/src/screens/Device/detail.js +46 -3
  56. package/src/screens/PlayBackCamera/__test__/index.test.js +48 -13
  57. package/src/screens/PlayBackCamera/index.js +1 -1
  58. package/src/utils/Apis/axios.js +6 -0
  59. package/src/utils/I18n/translations/en.js +8 -0
  60. package/src/utils/I18n/translations/vi.js +12 -4
  61. package/src/screens/Automate/constants.js +0 -0
@@ -144,19 +144,20 @@ const EditActionsList = () => {
144
144
  const paddedIndex = (index + 1).toString().padStart(2, '0');
145
145
  const {
146
146
  action_script,
147
+ toggle_script,
147
148
  notify_script,
148
149
  delay_script,
149
150
  email_script,
150
151
  sms_script,
151
152
  } = item;
152
- if (action_script) {
153
+ if (action_script || toggle_script) {
153
154
  const {
154
155
  sensor_icon_kit,
155
156
  station_name,
156
157
  sensor_name,
157
158
  action_name,
158
159
  data: data_action,
159
- } = action_script;
160
+ } = action_script || toggle_script;
160
161
 
161
162
  return (
162
163
  <CommonItem
@@ -178,7 +179,9 @@ const EditActionsList = () => {
178
179
  {sensor_name}
179
180
  </Text>
180
181
  <Text numberOfLines={1} type="H4" color={Colors.Gray9}>
181
- {action_name} {data_action}
182
+ {toggle_script
183
+ ? t('toggle')
184
+ : `${action_name} ${data_action || ''}`}
182
185
  </Text>
183
186
  </>
184
187
  }
@@ -316,6 +319,7 @@ const EditActionsList = () => {
316
319
  const renderAction = useMemo(() => {
317
320
  const {
318
321
  action_script,
322
+ toggle_script,
319
323
  notify_script,
320
324
  delay_script,
321
325
  email_script,
@@ -323,17 +327,17 @@ const EditActionsList = () => {
323
327
  id: scriptItemId,
324
328
  } = scriptItem;
325
329
 
326
- if (action_script) {
330
+ if (action_script || toggle_script) {
327
331
  onClosePopup();
328
332
  setScriptItem({});
329
333
  navigate(Routes.UpdateActionScript, {
330
334
  unitId,
331
335
  automateId: id,
336
+ activatedScript: scriptItem,
332
337
  device: {
333
- id: action_script.end_device_id,
338
+ id: action_script?.end_device_id || toggle_script?.end_device_id,
334
339
  },
335
- numberActionAdded: actionsList.length,
336
- scriptItemId: scriptItemId,
340
+ scriptItems: actionsList,
337
341
  closeScreen: Routes.EditActionsList,
338
342
  });
339
343
  }
@@ -407,27 +411,35 @@ const EditActionsList = () => {
407
411
  );
408
412
 
409
413
  const renderMessageRemove = useMemo(() => {
410
- const { action_script, notify_script, delay_script, email_script } =
411
- itemRemove;
412
- if (action_script) {
414
+ const {
415
+ action_script,
416
+ toggle_script,
417
+ notify_script,
418
+ delay_script,
419
+ email_script,
420
+ } = itemRemove;
421
+ if (action_script || toggle_script) {
422
+ const actionName = action_script?.action_name || t('toggle');
423
+ const stationName =
424
+ action_script?.station_name || toggle_script?.station_name;
413
425
  return (
414
426
  <ParsedText
415
427
  style={styles.messageDelete}
416
428
  parse={[
417
429
  {
418
- pattern: new RegExp(itemRemove.action_script.action_name),
430
+ pattern: new RegExp(actionName),
419
431
  style: styles.textHighlight,
420
432
  },
421
433
  {
422
- pattern: new RegExp(itemRemove.action_script.station_name),
434
+ pattern: new RegExp(stationName),
423
435
  style: styles.textHighlight,
424
436
  },
425
437
  ]}
426
438
  childrenProps={{ allowFontScaling: false }}
427
439
  >
428
440
  {t('message_delete_action', {
429
- actionName: itemRemove.action_script.action_name,
430
- stationName: itemRemove.action_script.station_name,
441
+ actionName: actionName,
442
+ stationName: stationName,
431
443
  })}
432
444
  </ParsedText>
433
445
  );
@@ -96,7 +96,15 @@ const MultiUnits = () => {
96
96
  navigate(Routes.UnitStack, {
97
97
  screen: Routes.ScenarioName,
98
98
  params: {
99
- automate: { type: AUTOMATE_TYPE.ONE_TAP, unit: unitId },
99
+ automate: {
100
+ type: AUTOMATE_TYPE.ONE_TAP,
101
+ conditions: [
102
+ {
103
+ type: AUTOMATE_TYPE.ONE_TAP,
104
+ },
105
+ ],
106
+ unit: unitId,
107
+ },
100
108
  closeScreen: currentRouteName,
101
109
  },
102
110
  });
@@ -50,7 +50,7 @@ describe('test OneTap', () => {
50
50
  script: { id: 1, name: 'William Miller' },
51
51
  };
52
52
 
53
- mock.onPost(API.AUTOMATE.CREATE_AUTOMATE()).reply(200, response);
53
+ mock.onPost(API.AUTOMATE.CREATE_AUTOMATE_V2()).reply(200, response);
54
54
 
55
55
  await act(async () => {
56
56
  tree = await create(wrapComponent(route));
@@ -99,7 +99,7 @@ describe('test OneTap', () => {
99
99
  params: { type: 'one_tap' },
100
100
  };
101
101
 
102
- mock.onPost(API.AUTOMATE.CREATE_AUTOMATE()).reply(400);
102
+ mock.onPost(API.AUTOMATE.CREATE_AUTOMATE_V2()).reply(400);
103
103
 
104
104
  await act(async () => {
105
105
  tree = await create(wrapComponent(route));
@@ -130,7 +130,7 @@ describe('test OneTap', () => {
130
130
  },
131
131
  };
132
132
 
133
- mock.onPost(API.AUTOMATE.CREATE_AUTOMATE()).reply(400);
133
+ mock.onPost(API.AUTOMATE.CREATE_AUTOMATE_V2()).reply(400);
134
134
  await act(async () => {
135
135
  tree = await create(wrapComponent(route));
136
136
  });
@@ -20,14 +20,7 @@ import AccessibilityLabel from '../../../../configs/AccessibilityLabel';
20
20
  import { AUTOMATE_TYPE } from '../../../../configs/Constants';
21
21
 
22
22
  const AddActionScript = memo(
23
- ({
24
- automate,
25
- isVisible,
26
- setIsVisible,
27
- numberActionAdded,
28
- type,
29
- navigate,
30
- }) => {
23
+ ({ automate, isVisible, setIsVisible, scriptItems, type, navigate }) => {
31
24
  const t = useTranslations();
32
25
  const { id, unit } = automate;
33
26
 
@@ -46,9 +39,10 @@ const AddActionScript = memo(
46
39
  const navParams = {
47
40
  unitId: unit,
48
41
  automateId: id,
42
+ scriptItems: scriptItems,
49
43
  closeScreen: currentScreenName,
50
44
  numberActionCanAdd:
51
- max_actions_per_automation - numberActionAdded,
45
+ max_actions_per_automation - scriptItems.length,
52
46
  routeName: unit ? null : Routes.SelectControlDevices,
53
47
  };
54
48
 
@@ -134,7 +128,7 @@ const AddActionScript = memo(
134
128
  id,
135
129
  max_actions_per_automation,
136
130
  navigate,
137
- numberActionAdded,
131
+ scriptItems,
138
132
  setIsVisible,
139
133
  t,
140
134
  unit,
@@ -4,17 +4,15 @@ import AlertAction from '../../../../commons/AlertAction';
4
4
  import { axiosDelete } from '../../../../utils/Apis/axios';
5
5
  import { ToastBottomHelper } from '../../../../utils/Utils';
6
6
  import { useTranslations } from '../../../../hooks/Common/useTranslations';
7
- import { useNavigation } from '@react-navigation/native';
8
7
 
9
- const RenameScript = ({ automate, isVisible, setIsVisible }) => {
8
+ const RenameScript = ({ automate, isVisible, setIsVisible, goBack }) => {
10
9
  const t = useTranslations();
11
- const { goBack } = useNavigation();
12
10
 
13
11
  const deleteScript = useCallback(async () => {
14
12
  const { success } = await axiosDelete(API.AUTOMATE.SCRIPT(automate.id));
15
13
  if (success) {
16
14
  setIsVisible(false);
17
- goBack();
15
+ goBack && goBack();
18
16
  ToastBottomHelper.success(t('removed_successfully'));
19
17
  } else {
20
18
  ToastBottomHelper.error(t('remove_failed'));
@@ -742,6 +742,7 @@ describe('Test ScriptDetail', () => {
742
742
  unitId: route.params.preAutomate.unit,
743
743
  automateId: route.params.preAutomate.id,
744
744
  numberActionCanAdd: 2,
745
+ scriptItems: [],
745
746
  closeScreen: undefined,
746
747
  routeName: null,
747
748
  });
@@ -901,6 +902,7 @@ describe('Test ScriptDetail', () => {
901
902
  unitId: route.params.preAutomate.unit,
902
903
  automateId: route.params.preAutomate.id,
903
904
  numberActionCanAdd: 2,
905
+ scriptItems: [],
904
906
  closeScreen: undefined,
905
907
  routeName: null,
906
908
  },
@@ -1211,6 +1213,82 @@ describe('Test ScriptDetail', () => {
1211
1213
  );
1212
1214
  });
1213
1215
 
1216
+ it('Test render textCondition schedule time range', async () => {
1217
+ route.params = {
1218
+ ...route.params,
1219
+ preAutomate: {
1220
+ type: AUTOMATE_TYPE.SCHEDULE,
1221
+ conditions: [
1222
+ {
1223
+ type: AUTOMATE_TYPE.SCHEDULE,
1224
+ repeat: 'time_range',
1225
+ time_start: '09:30:00',
1226
+ time_end: '10:00:00',
1227
+ weekday_repeat: null,
1228
+ },
1229
+ ],
1230
+ },
1231
+ };
1232
+ await act(async () => {
1233
+ tree = await create(wrapComponent(route));
1234
+ });
1235
+ const instance = tree.root;
1236
+ const itemAutomate = instance.findByType(ItemConditionScriptDetail);
1237
+ expect(itemAutomate.findAllByType(Text)[1].props.children).toEqual(
1238
+ 'From 09:30 to 10:00 Everyday'
1239
+ );
1240
+ });
1241
+
1242
+ it('Test render textCondition schedule time range with weekday', async () => {
1243
+ route.params = {
1244
+ ...route.params,
1245
+ preAutomate: {
1246
+ type: AUTOMATE_TYPE.SCHEDULE,
1247
+ conditions: [
1248
+ {
1249
+ type: AUTOMATE_TYPE.SCHEDULE,
1250
+ repeat: 'time_range',
1251
+ time_start: '09:30:00',
1252
+ time_end: '10:00:00',
1253
+ weekday_repeat: ['1', '3', '0'],
1254
+ },
1255
+ ],
1256
+ },
1257
+ };
1258
+ await act(async () => {
1259
+ tree = await create(wrapComponent(route));
1260
+ });
1261
+ const instance = tree.root;
1262
+ const itemAutomate = instance.findByType(ItemConditionScriptDetail);
1263
+ expect(itemAutomate.findAllByType(Text)[1].props.children).toEqual(
1264
+ 'From 09:30 to 10:00 on Mon, Wed, Sun'
1265
+ );
1266
+ });
1267
+
1268
+ it('Test render textCondition schedule interval repeat', async () => {
1269
+ route.params = {
1270
+ ...route.params,
1271
+ preAutomate: {
1272
+ type: AUTOMATE_TYPE.SCHEDULE,
1273
+ conditions: [
1274
+ {
1275
+ type: AUTOMATE_TYPE.SCHEDULE,
1276
+ repeat: 'interval',
1277
+ interval_repeat: 5,
1278
+ },
1279
+ ],
1280
+ },
1281
+ };
1282
+ await act(async () => {
1283
+ tree = await create(wrapComponent(route));
1284
+ });
1285
+ const instance = tree.root;
1286
+ const itemAutomate = instance.findByType(ItemConditionScriptDetail);
1287
+ expect(itemAutomate.findAllByType(Text)[1].props.children).toEqual(
1288
+ 'Repeat every 5 minutes'
1289
+ );
1290
+ });
1291
+
1214
1292
  it('test navigate to UnitDetail on event beforeRemove', async () => {
1215
1293
  Platform.OS = 'ios';
1216
1294
  route.params.closeScreen = Routes.UnitDetail;
@@ -110,11 +110,7 @@ const ScriptDetail = ({ route }) => {
110
110
  chip_id_local_control,
111
111
  } = script || {};
112
112
 
113
- const [local_control, setLocalControl] = useState({
114
- is_local_control,
115
- chip_local_control,
116
- chip_id_local_control,
117
- });
113
+ const [local_control, setLocalControl] = useState({});
118
114
  const [enableScript, setEnableScript] = useState(enable);
119
115
  const [listMenuItemCondition, setListMenuItemCondition] = useState([]);
120
116
  const [isShowAddCondition, setIsShowAddCondition] = useState(false);
@@ -384,6 +380,14 @@ const ScriptDetail = ({ route }) => {
384
380
  newActionsList && setData(newActionsList);
385
381
  }, [newActionsList]);
386
382
 
383
+ useEffect(() => {
384
+ setLocalControl({
385
+ is_local_control,
386
+ chip_local_control,
387
+ chip_id_local_control,
388
+ });
389
+ }, [is_local_control, chip_local_control, chip_id_local_control]);
390
+
387
391
  const rightComponent = useMemo(
388
392
  () => (
389
393
  <View style={styles.rightComponent}>
@@ -766,7 +770,7 @@ const ScriptDetail = ({ route }) => {
766
770
  />
767
771
  <AddActionScript
768
772
  automate={automate}
769
- numberActionAdded={data.length}
773
+ scriptItems={data}
770
774
  isVisible={isShowAddAction}
771
775
  setIsVisible={setIsShowAddAction}
772
776
  type={type}
@@ -778,7 +782,7 @@ const ScriptDetail = ({ route }) => {
778
782
  isVisible={isShowAddCondition}
779
783
  setIsVisible={setIsShowAddCondition}
780
784
  type={type}
781
- navigate={navigate}
785
+ navigate={handleNavigate}
782
786
  />
783
787
  <RenameScript
784
788
  automate={automate}
@@ -790,6 +794,7 @@ const ScriptDetail = ({ route }) => {
790
794
  automate={automate}
791
795
  setIsVisible={setIsShowDelete}
792
796
  isVisible={isShowDelete}
797
+ goBack={handleGoBack}
793
798
  />
794
799
  <DeleteCondition
795
800
  automate={automate}
@@ -895,12 +900,13 @@ const Item = ({ item, index, enableScript, t, local_control }) => {
895
900
  const paddedIndex = (index + 1).toString().padStart(2, '0');
896
901
  const {
897
902
  action_script,
903
+ toggle_script,
898
904
  notify_script,
899
905
  delay_script,
900
906
  email_script,
901
907
  sms_script,
902
908
  } = item;
903
- if (action_script) {
909
+ if (action_script || toggle_script) {
904
910
  const {
905
911
  sensor_icon_kit,
906
912
  unit_name,
@@ -908,7 +914,7 @@ const Item = ({ item, index, enableScript, t, local_control }) => {
908
914
  sensor_name,
909
915
  action_name,
910
916
  data,
911
- } = action_script;
917
+ } = action_script || toggle_script;
912
918
  return (
913
919
  <View style={styles.wrapItem}>
914
920
  <View style={styles.leftItem}>
@@ -942,7 +948,7 @@ const Item = ({ item, index, enableScript, t, local_control }) => {
942
948
  {sensor_name}
943
949
  </Text>
944
950
  <Text numberOfLines={1} type="H4" color={color}>
945
- {action_name} {data}
951
+ {toggle_script ? t('toggle') : `${action_name} ${data || ''}`}
946
952
  </Text>
947
953
  </View>
948
954
  </View>
@@ -1,5 +1,5 @@
1
1
  import { AUTOMATE_TYPE } from '../../../configs/Constants';
2
- import { REPEAT_OPTIONS } from '../SetSchedule/components/RepeatOptionsPopup';
2
+ import { REPEAT_OPTIONS } from '../Constants';
3
3
 
4
4
  export const getAutomationData = (automate) => {
5
5
  const { type, event_sensor, event_action, schedule, event, value_change } =
@@ -73,11 +73,16 @@ export const generateAutomationConditionText = (
73
73
  }
74
74
  return `${config_name} ${t(text)} ${value}`;
75
75
  } else if (type === AUTOMATE_TYPE.SCHEDULE) {
76
- const { repeat, time_repeat, date_repeat, weekday_repeat } = conditionItem;
77
- const time =
78
- time_repeat?.length >= 8
79
- ? time_repeat.substring(0, time_repeat?.length - 3)
80
- : time_repeat;
76
+ const {
77
+ repeat,
78
+ time_repeat,
79
+ time_start,
80
+ time_end,
81
+ date_repeat,
82
+ weekday_repeat,
83
+ interval_repeat,
84
+ } = conditionItem;
85
+
81
86
  const date = date_repeat?.split('-').reverse().join('/');
82
87
  const weekday = {
83
88
  1: t('mon'),
@@ -89,14 +94,14 @@ export const generateAutomationConditionText = (
89
94
  0: t('sun'),
90
95
  };
91
96
 
92
- if (repeat === REPEAT_OPTIONS.ONCE) {
93
- return `${time} ${date}`;
94
- } else if (repeat === REPEAT_OPTIONS.EVERYDAY) {
95
- return `${t('every_day_at', { time })}`;
96
- } else if (repeat === REPEAT_OPTIONS.EVERYWEEK) {
97
+ const shortTime = (time) => {
98
+ return time?.length >= 8 ? time.substring(0, time?.length - 3) : time;
99
+ };
100
+
101
+ const getTextWeekDay = (weekdayRepeat) => {
97
102
  //sort 0 is last number, exp: [1,3,5,0]
98
- const newWeekdayRepeat = weekday_repeat.map((item) => parseInt(item, 10));
99
- const sortWeekday = newWeekdayRepeat.sort((a, b) => {
103
+ const newWeekdayRepeat = weekdayRepeat?.map((item) => parseInt(item, 10));
104
+ const sortWeekday = newWeekdayRepeat?.sort((a, b) => {
100
105
  if (a !== 0 && b === 0) {
101
106
  return -1;
102
107
  }
@@ -105,8 +110,27 @@ export const generateAutomationConditionText = (
105
110
  }
106
111
  return a - b;
107
112
  });
108
- const textWeekday = sortWeekday.map((item) => weekday[item]).join(', ');
109
- return `${textWeekday} ${t('at')} ${time}`;
113
+ return sortWeekday?.map((item) => weekday[item]).join(', ');
114
+ };
115
+
116
+ if (repeat === REPEAT_OPTIONS.ONCE) {
117
+ return `${shortTime(time_repeat)} ${date}`;
118
+ } else if (repeat === REPEAT_OPTIONS.EVERYDAY) {
119
+ return `${t('every_day_at', { time: shortTime(time_repeat) })}`;
120
+ } else if (repeat === REPEAT_OPTIONS.EVERYWEEK) {
121
+ const textWeekday = getTextWeekDay(weekday_repeat);
122
+ return `${textWeekday} ${t('at')} ${shortTime(time_repeat)}`;
123
+ } else if (repeat === REPEAT_OPTIONS.TIMERANGE) {
124
+ const textWeekday = getTextWeekDay(weekday_repeat);
125
+ return `${t('from_to', {
126
+ from: shortTime(time_start),
127
+ to: shortTime(time_end),
128
+ weekdays: textWeekday
129
+ ? `${t('on_weekday')} ${textWeekday}`
130
+ : t('every_day'),
131
+ })}`;
132
+ } else if (repeat === REPEAT_OPTIONS.INTERVAL) {
133
+ return `${t('repeat_every', { interval: interval_repeat })}`;
110
134
  }
111
135
  }
112
136
  return null;
@@ -1,173 +1,40 @@
1
1
  import { useNavigation } from '@react-navigation/native';
2
- import moment from 'moment';
3
- import React, { memo, useCallback, useState } from 'react';
4
- import { ScrollView } from 'react-native';
5
- import Calendar from '../../../commons/Calendar';
6
- import WheelDateTimePicker from '../../../commons/WheelDateTimePicker';
7
- import { useBoolean } from '../../../hooks/Common';
8
- import { useTranslations } from '../../../hooks/Common/useTranslations';
2
+ import React, { useCallback } from 'react';
3
+ import EditSchedule from './EditSchedule';
9
4
  import Routes from '../../../utils/Route';
10
- import NewActionWrapper from '../AddNewAction/NewActionWrapper';
11
- import RepeatOptionsPopup, {
12
- REPEAT_OPTIONS,
13
- } from './components/RepeatOptionsPopup';
14
- import RowItem from './components/RowItem';
15
- import SelectWeekday from './components/SelectWeekday';
16
- import styles from './styles/indexStyles';
17
5
  import { axiosPost, axiosPut } from '../../../utils/Apis/axios';
18
6
  import { API } from '../../../configs';
19
- import { AUTOMATE_TYPE } from '../../../configs/Constants';
20
-
21
- const getDateString = (date, t) => {
22
- //Move outside component to prevent re-creation on every render.
23
- const today = moment();
24
- if (date.isSame(today, 'day')) {
25
- return date.format(`[${t('today')}], D MMMM YYYY `);
26
- }
27
- return date.format('ddd, D MMMM YYYY');
28
- };
29
7
 
30
8
  const AddEditConditionSchedule = ({ route }) => {
31
- const t = useTranslations();
32
- const { automate, condition = {}, isUpdateCondition } = route.params;
9
+ const { automate, condition, isUpdateCondition } = route.params;
33
10
  const { navigate } = useNavigation();
34
- const [repeat, setRepeat] = useState(
35
- condition.repeat ? condition.repeat : REPEAT_OPTIONS.ONCE
36
- );
37
- const [time, setTime] = useState(
38
- condition.time_repeat
39
- ? moment(condition.time_repeat, 'HH:mm:ss')
40
- : moment().second(0)
41
- );
42
- const [date, setDate] = useState(
43
- condition.date_repeat
44
- ? moment(condition.date_repeat, 'YYYY-MM-DD')
45
- : moment()
46
- );
47
- const [weekday, setWeekday] = useState(
48
- condition.weekday_repeat ? condition.weekday_repeat : []
49
- );
50
-
51
- const [showRepeatOptions, setShowRepeatOptions, setHideRepeatOptions] =
52
- useBoolean();
53
- const [showTimePicker, setShowTimePicker, setHideTimePicker] = useBoolean();
54
- const [showCalendar, setShowCalendar, setHideCalendar] = useBoolean();
55
-
56
- const handleOnSave = useCallback(async () => {
57
- const data = {
58
- type: AUTOMATE_TYPE.SCHEDULE,
59
- repeat,
60
- time_repeat: time.format('HH:mm:ss'),
61
- date_repeat:
62
- repeat === REPEAT_OPTIONS.ONCE ? date.format('YYYY-MM-DD') : null,
63
- weekday_repeat: repeat === REPEAT_OPTIONS.EVERYWEEK ? weekday : null,
64
- };
65
11
 
66
- let response;
67
- if (isUpdateCondition) {
68
- response = await axiosPut(
69
- API.AUTOMATE.UPDATE_CONDITION(automate.id, condition.id),
70
- data
71
- );
72
- } else {
73
- response = await axiosPost(API.AUTOMATE.ADD_CONDITION(automate.id), data);
74
- }
75
- const { success } = response;
76
- if (success) {
77
- navigate({
78
- name: Routes.ScriptDetail,
79
- params: { preAutomate: automate },
80
- });
81
- }
82
- }, [
83
- repeat,
84
- time,
85
- date,
86
- weekday,
87
- isUpdateCondition,
88
- automate,
89
- condition.id,
90
- navigate,
91
- ]);
92
-
93
- const onSetRepeatOption = useCallback(
94
- (value) => {
95
- setRepeat(value);
96
- setHideRepeatOptions();
97
- },
98
- [setRepeat, setHideRepeatOptions]
99
- );
100
-
101
- const onTimePicked = useCallback(
102
- (timeData) => {
103
- setTime(moment(timeData));
12
+ const handleOnSave = useCallback(
13
+ async (data) => {
14
+ let response;
15
+ if (isUpdateCondition) {
16
+ response = await axiosPut(
17
+ API.AUTOMATE.UPDATE_CONDITION(automate.id, condition?.id),
18
+ data
19
+ );
20
+ } else {
21
+ response = await axiosPost(
22
+ API.AUTOMATE.ADD_CONDITION(automate.id),
23
+ data
24
+ );
25
+ }
26
+ const { success } = response;
27
+ if (success) {
28
+ navigate({
29
+ name: Routes.ScriptDetail,
30
+ params: { preAutomate: automate },
31
+ });
32
+ }
104
33
  },
105
- [setTime]
34
+ [isUpdateCondition, automate, condition?.id, navigate]
106
35
  );
107
36
 
108
- const onDatePicked = useCallback(
109
- (datePicked) => {
110
- setDate(datePicked);
111
- },
112
- [setDate]
113
- );
114
-
115
- return (
116
- <NewActionWrapper
117
- name={t('set_schedule')}
118
- onNext={handleOnSave}
119
- canNext={true}
120
- nextTitle={t('save')}
121
- >
122
- <ScrollView
123
- contentContainerStyle={styles.scollView}
124
- scrollIndicatorInsets={{ right: 1 }}
125
- >
126
- <RowItem
127
- title={t('set_time')}
128
- value={time.format('HH:mm')}
129
- icon="clock-circle"
130
- onPress={setShowTimePicker}
131
- />
132
- {repeat === REPEAT_OPTIONS.ONCE && (
133
- <RowItem
134
- title={t('select_date')}
135
- value={getDateString(date, t)}
136
- icon="calendar"
137
- onPress={setShowCalendar}
138
- />
139
- )}
140
- {repeat === REPEAT_OPTIONS.EVERYWEEK && (
141
- <SelectWeekday weekday={weekday} setWeekday={setWeekday} />
142
- )}
143
- <RowItem
144
- title={t('repeat')}
145
- value={t(`${repeat}`)}
146
- arrow
147
- onPress={setShowRepeatOptions}
148
- />
149
- </ScrollView>
150
- <RepeatOptionsPopup
151
- isVisible={showRepeatOptions}
152
- onHide={setHideRepeatOptions}
153
- onSetRepeat={onSetRepeatOption}
154
- />
155
- <WheelDateTimePicker
156
- mode="time"
157
- isVisible={showTimePicker}
158
- defaultValue={time.valueOf()}
159
- onCancel={setHideTimePicker}
160
- onPicked={onTimePicked}
161
- />
162
- <Calendar
163
- isVisible={showCalendar}
164
- defaultDate={date}
165
- minDate={moment()}
166
- onCancel={setHideCalendar}
167
- onConfirm={onDatePicked}
168
- />
169
- </NewActionWrapper>
170
- );
37
+ return <EditSchedule condition={condition} onSave={handleOnSave} />;
171
38
  };
172
39
 
173
- export default memo(AddEditConditionSchedule);
40
+ export default AddEditConditionSchedule;