@janiscommerce/app-tracking-shift 1.2.0 → 1.4.0-beta.1

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.
@@ -1,5 +1,5 @@
1
1
  import React, {useEffect, useMemo, useState} from 'react';
2
- import {useMMKVString} from 'react-native-mmkv';
2
+ import {useStorageValue} from '../hooks/useStorageValue';
3
3
  import ShiftTrackingContext from '../context/ShiftTrackingContext';
4
4
  import {
5
5
  openShift,
@@ -17,20 +17,19 @@ import {
17
17
  STAFF_AUTH,
18
18
  WORKLOG_TYPES_DATA,
19
19
  } from '../constant';
20
- import {useMMKVObject} from '../hooks/useMMKVObject';
21
20
  import {isValidObject, promiseWrapper} from '../utils/helpers';
22
- import Storage from '../db/StorageService';
21
+ import Shift from '../Shift';
23
22
 
24
23
  const ShiftTrackingProvider = ({children, onError = null}) => {
25
- const [shiftStatus] = useMMKVString(SHIFT_STATUS);
26
- const [shiftId] = useMMKVString(SHIFT_ID);
27
- const [shiftData] = useMMKVObject(SHIFT_DATA, {});
24
+ const shiftStatus = useStorageValue(SHIFT_STATUS);
25
+ const shiftId = useStorageValue(SHIFT_ID);
26
+ const shiftData = useStorageValue(SHIFT_DATA, {});
28
27
 
29
- const [currentWorkLogId] = useMMKVString(CURRENT_WORKLOG_ID);
30
- const [workLogData] = useMMKVObject(WORKLOG_TYPES_DATA, {});
31
- const [currentWorkLogData] = useMMKVObject(CURRENT_WORKLOG_DATA, {});
28
+ const currentWorkLogId = useStorageValue(CURRENT_WORKLOG_ID);
29
+ const workLogData = useStorageValue(WORKLOG_TYPES_DATA, {});
30
+ const currentWorkLogData = useStorageValue(CURRENT_WORKLOG_DATA, {});
32
31
 
33
- const [staffAuthData] = useMMKVObject(STAFF_AUTH, {});
32
+ const staffAuthData = useStorageValue(STAFF_AUTH, {});
34
33
 
35
34
  const [error, setError] = useState(null);
36
35
  const [openShiftResult, setOpenShiftResult] = useState({
@@ -48,6 +47,7 @@ const ShiftTrackingProvider = ({children, onError = null}) => {
48
47
  shiftStatus,
49
48
  shiftData,
50
49
  workLogTypes,
50
+ hasWorkTypes: !!workLogTypes?.length,
51
51
  currentWorkLogData,
52
52
  currentWorkLogId,
53
53
  hasStaffAuthorization,
@@ -132,12 +132,11 @@ const ShiftTrackingProvider = ({children, onError = null}) => {
132
132
  const isExcludedWork = EXCLUDED_WORKLOG_TYPES.includes(currentWorkLog?.referenceId);
133
133
 
134
134
  if (isValidObject(currentWorkLog)) {
135
- Storage.set(CURRENT_WORKLOG_ID, currentWorkLog.id);
136
- Storage.set(CURRENT_WORKLOG_DATA, JSON.stringify(currentWorkLog));
135
+ Shift.setCurrentWorkLog(currentWorkLog);
137
136
  }
138
137
 
139
138
  if (isValidObject(currentWorkLog) && !isExcludedWork) {
140
- Storage.set(SHIFT_STATUS, 'paused');
139
+ Shift.status = 'paused';
141
140
  }
142
141
 
143
142
  setOpenShiftResult((prev) => ({
@@ -1,3 +1,7 @@
1
+ export {default as isApiError} from './isApiError';
2
+
3
+ export {default as isInternetReachable} from './isInternetReachable';
4
+
1
5
  export const generateRandomId = () => Math.random().toString(32).slice(2);
2
6
 
3
7
  export const isFunction = (fn) => !!({}.toString.call(fn) === '[object Function]');
@@ -0,0 +1,7 @@
1
+ const isApiError = (error) => {
2
+ const {result, statusCode = null} = error || {};
3
+
4
+ return result?.message && Boolean(statusCode);
5
+ };
6
+
7
+ export default isApiError;
@@ -0,0 +1,16 @@
1
+ import {getInternetReachability} from '@janiscommerce/app-device-info';
2
+
3
+ /**
4
+ * Check if internet connection is reachable
5
+ * @returns {Promise<boolean>} true if internet is reachable, false otherwise
6
+ */
7
+ const isInternetReachable = async () => {
8
+ try {
9
+ const isReachable = await getInternetReachability();
10
+ return isReachable;
11
+ } catch (error) {
12
+ return false;
13
+ }
14
+ };
15
+
16
+ export default isInternetReachable;
@@ -19,7 +19,7 @@ const downloadWorkLogTypes = async (onDownloadError) => {
19
19
  expirationTime: Date.now() + WORKLOG_TYPES_EXPIRATION_TIME,
20
20
  };
21
21
 
22
- Storage.set(WORKLOG_TYPES_DATA, JSON.stringify(data));
22
+ Storage.set(WORKLOG_TYPES_DATA, data);
23
23
  return null;
24
24
  } catch (error) {
25
25
  const parsedError = errorParser(error);
@@ -15,24 +15,24 @@ const isAuthorizedToUseStaffMS = async () => {
15
15
 
16
16
  const isAuthorized = await Shift.checkStaffMSAuthorization();
17
17
 
18
- const data = {
18
+ const staffAuthData = {
19
19
  hasStaffAuthorization: isAuthorized,
20
20
  expirationTime: Date.now() + STAFF_MS_AUTHORIZATION_EXPIRATION_TIME,
21
21
  isExpired: false,
22
22
  };
23
23
 
24
- Storage.set(STAFF_AUTH, JSON.stringify(data));
24
+ Storage.set(STAFF_AUTH, staffAuthData);
25
25
 
26
26
  return isAuthorized;
27
27
  } catch (error) {
28
28
  const parsedError = errorParser(error);
29
29
  Crashlytics.recordError(parsedError, 'Error checking staff MS authorization');
30
- const data = {
30
+ const staffAuthData = {
31
31
  hasStaffAuthorization: false,
32
32
  expirationTime: 0,
33
33
  isExpired: true,
34
34
  };
35
- Storage.set(STAFF_AUTH, JSON.stringify(data));
35
+ Storage.set(STAFF_AUTH, staffAuthData);
36
36
 
37
37
  return Promise.reject(parsedError);
38
38
  }
@@ -1,15 +1,11 @@
1
- import Storage from '../../../db/StorageService';
2
1
  import Crashlytics from '../../crashlytics';
3
- import {SHIFT_ID, SHIFT_STATUS, SHIFT_DATA} from '../../../constant';
4
2
  import getUserId from '../../userInfo/getUserId';
5
3
  import Shift from '../../../Shift';
6
4
  import {isFunction} from '../../helpers';
7
- import {deleteStoredWorkLog, setObject} from '../../storage';
8
5
  import errorParser from '../../errorParser';
9
6
 
10
7
  const openShift = async (onOpenShiftError) => {
11
8
  try {
12
- const shiftId = Storage.getString(SHIFT_ID);
13
9
  const userId = await getUserId();
14
10
 
15
11
  if (!userId) {
@@ -21,8 +17,7 @@ const openShift = async (onOpenShiftError) => {
21
17
  };
22
18
  }
23
19
 
24
- const currentShift = await Shift.getUserOpenShift({id: shiftId, userId});
25
-
20
+ const currentShift = await Shift.getUserOpenShift({id: Shift.id, userId});
26
21
  if (currentShift?.status !== 'opened') {
27
22
  await Shift.deleteShiftRegisters();
28
23
  const openShiftId = await Shift.open();
@@ -32,17 +27,16 @@ const openShift = async (onOpenShiftError) => {
32
27
  };
33
28
  }
34
29
 
35
- if (shiftId === currentShift.id)
30
+ if (Shift.id === currentShift.id)
36
31
  return {
37
32
  openShiftId: currentShift.id,
38
33
  getWorkLogs: false,
39
34
  };
40
35
 
41
- deleteStoredWorkLog();
42
- Storage.set(SHIFT_ID, currentShift.id);
43
- Storage.set(SHIFT_STATUS, currentShift.status);
44
- setObject(SHIFT_DATA, currentShift);
45
-
36
+ Shift.deleteCurrentWorkLog();
37
+ Shift.id = currentShift.id;
38
+ Shift.status = currentShift.status;
39
+ Shift.data = currentShift;
46
40
  return {
47
41
  openShiftId: currentShift.id,
48
42
  getWorkLogs: true,
@@ -5,9 +5,11 @@ import errorParser from '../../errorParser';
5
5
 
6
6
  const getStaffAuthorizationData = () => {
7
7
  try {
8
- const staffAuthRaw = Storage.getString(STAFF_AUTH);
9
- const parsedAuth = JSON.parse(staffAuthRaw);
10
- const {hasStaffAuthorization = false, expirationTime = 0, isExpired = false} = parsedAuth;
8
+ const {
9
+ hasStaffAuthorization = false,
10
+ expirationTime = 0,
11
+ isExpired = false,
12
+ } = Storage.get(STAFF_AUTH);
11
13
 
12
14
  return {
13
15
  hasStaffAuthorization,
@@ -16,9 +16,7 @@ import errorParser from '../../errorParser';
16
16
  const getWorkLogTypesData = () => {
17
17
  try {
18
18
  Crashlytics.log('getWorkLogTypesData');
19
- const workLogRaw = Storage.getString(WORKLOG_TYPES_DATA);
20
- const parsedWorkLogs = JSON.parse(workLogRaw);
21
- const {workLogTypes = [], expirationTime = 0} = parsedWorkLogs;
19
+ const {workLogTypes = [], expirationTime = 0} = Storage.get(WORKLOG_TYPES_DATA);
22
20
 
23
21
  return {
24
22
  workLogTypes,
@@ -1,20 +1,2 @@
1
- import Storage from '../../db/StorageService';
2
-
3
1
  export {default as getWorkLogTypesData} from './getWorkLogTypesData';
4
- export {default as getShiftData} from './getShiftData';
5
2
  export {default as getStaffAuthorizationData} from './getStaffAuthorizationData';
6
- export {default as deleteStoredWorkLog} from './deleteStoredWorkLog';
7
-
8
- export const setObject = (key, value) => {
9
- const jsonValue = JSON.stringify(value);
10
- Storage.set(key, jsonValue);
11
- };
12
-
13
- export const getObject = (key, defaultValue = {}) => {
14
- try {
15
- const jsonValue = Storage.getString(key);
16
- return JSON.parse(jsonValue);
17
- } catch (error) {
18
- return defaultValue;
19
- }
20
- };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@janiscommerce/app-tracking-shift",
3
- "version": "1.2.0",
3
+ "version": "1.4.0-beta.1",
4
4
  "main": "lib/index.js",
5
5
  "module": "lib/index.js",
6
6
  "exports": {
@@ -46,16 +46,13 @@
46
46
  "homepage": "https://github.com/janis-commerce/app-tracking-shift#readme",
47
47
  "peerDependencies": {
48
48
  "@janiscommerce/app-crashlytics": ">=2.1.0",
49
+ "@janiscommerce/app-device-info": "^1.1.0",
49
50
  "@janiscommerce/app-request": ">=2.0.0",
51
+ "@janiscommerce/app-storage": ">=1.1.0",
50
52
  "react": ">=17.0.2 <19",
51
53
  "react-dom": ">=17.0.2 <19",
52
54
  "react-native": ">=0.67.5 <0.75"
53
55
  },
54
- "dependencies": {
55
- "@janiscommerce/app-device-info": "^1.1.0",
56
- "@janiscommerce/oauth-native": "^1.10.2",
57
- "react-native-mmkv": "2.12.2"
58
- },
59
56
  "devDependencies": {
60
57
  "@babel/core": "^7.0.0",
61
58
  "@babel/eslint-parser": "^7.22.5",
@@ -63,6 +60,8 @@
63
60
  "@babel/preset-react": "^7.0.0",
64
61
  "@janiscommerce/app-crashlytics": "^2.1.0",
65
62
  "@janiscommerce/app-request": "^2.6.0",
63
+ "@janiscommerce/app-storage": "^1.1.0",
64
+ "@janiscommerce/oauth-native": "^1.10.2",
66
65
  "@testing-library/react": "^16.3.0",
67
66
  "@testing-library/react-native": "^12.0.1",
68
67
  "babel-jest": "^29.0.0",
@@ -83,6 +82,7 @@
83
82
  "react": "18.2.0",
84
83
  "react-native": "0.71.6",
85
84
  "react-native-device-info": "^10.12.0",
85
+ "react-native-mmkv": "2.12.2",
86
86
  "react-native-inappbrowser-reborn": "^3.7.0",
87
87
  "react-test-renderer": "18.2.0"
88
88
  }
@@ -1,21 +0,0 @@
1
- import {useMMKVString} from 'react-native-mmkv';
2
- import {useMemo} from 'react';
3
- import Crashlytics from '../../utils/crashlytics';
4
- import errorParser from '../../utils/errorParser';
5
-
6
- export const useMMKVObject = (key, defaultValue = null) => {
7
- const [raw] = useMMKVString(key);
8
-
9
- const storageValue = useMemo(() => {
10
- if (!raw) return defaultValue;
11
- try {
12
- return JSON.parse(raw);
13
- } catch (e) {
14
- const parsedError = errorParser(e);
15
- Crashlytics.recordError(parsedError, `Invalid JSON in MMKV key: ${key}`);
16
- return defaultValue;
17
- }
18
- }, [raw]);
19
-
20
- return [storageValue];
21
- };
@@ -1,9 +0,0 @@
1
- import {CURRENT_WORKLOG_DATA, CURRENT_WORKLOG_ID} from '../../../constant';
2
- import Storage from '../../../db/StorageService';
3
-
4
- const deleteStoredWorkLog = () => {
5
- Storage.delete(CURRENT_WORKLOG_ID);
6
- Storage.delete(CURRENT_WORKLOG_DATA);
7
- };
8
-
9
- export default deleteStoredWorkLog;
@@ -1,33 +0,0 @@
1
- import Storage from '../../../db/StorageService';
2
- import {SHIFT_DATA} from '../../../constant';
3
-
4
- /**
5
- * @description Get the shift data from the storage
6
- * @returns {Object} The shift data
7
- * @example
8
- * const shiftData = getShiftData();
9
- * console.log(shiftData); // {
10
- "id": "631fb04c8fe08f51a8ee5949",
11
- "startDate": "2024-01-15T09:00:00.000Z",
12
- "dateToClose": "2024-01-15T17:00:00.000Z",
13
- "dateMaxToClose": "2024-01-15T17:00:00.000Z",
14
- "userId": "6a1fc1eeb5b68406e0487a10",
15
- "displayId": "240715-EARPIQ",
16
- "dateCreated": "2019-07-12T19:00:00.000Z",
17
- "dateModified": "2019-07-20T19:00:00.000Z",
18
- "userCreated": "6a1fc1eeb5b68406e0487a10",
19
- "userModified": "7e1fc1eeb5b68406e048796",
20
- "status": "opened"
21
- }
22
- */
23
-
24
- const getShiftData = () => {
25
- const shiftData = Storage.getString(SHIFT_DATA);
26
- try {
27
- return JSON.parse(shiftData);
28
- } catch (error) {
29
- return shiftData;
30
- }
31
- };
32
-
33
- export default getShiftData;