Browse Source

Improve utils files to match linter

Arnaud Vergnet 3 years ago
parent
commit
569e659779
3 changed files with 91 additions and 71 deletions
  1. 2
    2
      src/utils/EquipmentBooking.js
  2. 68
    58
      src/utils/Notifications.js
  3. 21
    11
      src/utils/withCollapsible.js

+ 2
- 2
src/utils/EquipmentBooking.js View File

@@ -3,7 +3,7 @@
3 3
 import i18n from 'i18n-js';
4 4
 import type {DeviceType} from '../screens/Amicale/Equipment/EquipmentListScreen';
5 5
 import DateManager from '../managers/DateManager';
6
-import type {CustomTheme} from '../managers/ThemeManager';
6
+import type {CustomThemeType} from '../managers/ThemeManager';
7 7
 import type {MarkedDatesObjectType} from '../screens/Amicale/Equipment/EquipmentRentScreen';
8 8
 
9 9
 /**
@@ -161,7 +161,7 @@ export function getValidRange(
161 161
  */
162 162
 export function generateMarkedDates(
163 163
   isSelection: boolean,
164
-  theme: CustomTheme,
164
+  theme: CustomThemeType,
165 165
   range: Array<string>,
166 166
 ): MarkedDatesObjectType {
167 167
   const markedDates = {};

+ 68
- 58
src/utils/Notifications.js View File

@@ -1,10 +1,14 @@
1 1
 // @flow
2 2
 
3
-import {checkNotifications, requestNotifications, RESULTS} from 'react-native-permissions';
4
-import AsyncStorageManager from "../managers/AsyncStorageManager";
5
-import i18n from "i18n-js";
3
+import {
4
+  checkNotifications,
5
+  requestNotifications,
6
+  RESULTS,
7
+} from 'react-native-permissions';
8
+import i18n from 'i18n-js';
9
+import AsyncStorageManager from '../managers/AsyncStorageManager';
6 10
 
7
-const PushNotification = require("react-native-push-notification");
11
+const PushNotification = require('react-native-push-notification');
8 12
 
9 13
 // Used to multiply the normal notification id to create the reminder one. It allows to find it back easily
10 14
 const reminderIdFactor = 100;
@@ -13,25 +17,21 @@ const reminderIdFactor = 100;
13 17
  * Async function asking permission to send notifications to the user.
14 18
  * Used on ios.
15 19
  *
16
- * @returns {Promise}
20
+ * @returns {Promise<void>}
17 21
  */
18
-export async function askPermissions() {
19
-    return new Promise(((resolve, reject) => {
20
-        checkNotifications().then(({status}) => {
21
-            if (status === RESULTS.GRANTED)
22
-                resolve();
23
-            else if (status === RESULTS.BLOCKED)
24
-                reject()
25
-            else {
26
-                requestNotifications().then(({status}) => {
27
-                    if (status === RESULTS.GRANTED)
28
-                        resolve();
29
-                    else
30
-                        reject();
31
-                });
32
-            }
22
+export async function askPermissions(): Promise<void> {
23
+  return new Promise((resolve: () => void, reject: () => void) => {
24
+    checkNotifications().then(({status}: {status: string}) => {
25
+      if (status === RESULTS.GRANTED) resolve();
26
+      else if (status === RESULTS.BLOCKED) reject();
27
+      else {
28
+        requestNotifications().then((result: {status: string}) => {
29
+          if (result.status === RESULTS.GRANTED) resolve();
30
+          else reject();
33 31
         });
34
-    }));
32
+      }
33
+    });
34
+  });
35 35
 }
36 36
 
37 37
 /**
@@ -43,27 +43,33 @@ export async function askPermissions() {
43 43
  * @param date The date to trigger the notification at
44 44
  */
45 45
 function createNotifications(machineID: string, date: Date) {
46
-    let reminder = AsyncStorageManager.getNumber(AsyncStorageManager.PREFERENCES.proxiwashNotifications.key);
47
-    if (!isNaN(reminder) && reminder > 0) {
48
-        let id = reminderIdFactor * parseInt(machineID);
49
-        let reminderDate = new Date(date);
50
-        reminderDate.setMinutes(reminderDate.getMinutes() - reminder);
51
-        PushNotification.localNotificationSchedule({
52
-            title: i18n.t("screens.proxiwash.notifications.machineRunningTitle", {time: reminder}),
53
-            message: i18n.t("screens.proxiwash.notifications.machineRunningBody", {number: machineID}),
54
-            id: id.toString(),
55
-            date: reminderDate,
56
-        });
57
-        console.log("Setting up notifications for ", date, " and reminder for ", reminderDate);
58
-    } else
59
-        console.log("Setting up notifications for ", date);
60
-
46
+  const reminder = AsyncStorageManager.getNumber(
47
+    AsyncStorageManager.PREFERENCES.proxiwashNotifications.key,
48
+  );
49
+  if (!Number.isNaN(reminder) && reminder > 0) {
50
+    const id = reminderIdFactor * parseInt(machineID, 10);
51
+    const reminderDate = new Date(date);
52
+    reminderDate.setMinutes(reminderDate.getMinutes() - reminder);
61 53
     PushNotification.localNotificationSchedule({
62
-        title: i18n.t("screens.proxiwash.notifications.machineFinishedTitle"),
63
-        message: i18n.t("screens.proxiwash.notifications.machineFinishedBody", {number: machineID}),
64
-        id: machineID,
65
-        date: date,
54
+      title: i18n.t('screens.proxiwash.notifications.machineRunningTitle', {
55
+        time: reminder,
56
+      }),
57
+      message: i18n.t('screens.proxiwash.notifications.machineRunningBody', {
58
+        number: machineID,
59
+      }),
60
+      id: id.toString(),
61
+      date: reminderDate,
66 62
     });
63
+  }
64
+
65
+  PushNotification.localNotificationSchedule({
66
+    title: i18n.t('screens.proxiwash.notifications.machineFinishedTitle'),
67
+    message: i18n.t('screens.proxiwash.notifications.machineFinishedBody', {
68
+      number: machineID,
69
+    }),
70
+    id: machineID,
71
+    date,
72
+  });
67 73
 }
68 74
 
69 75
 /**
@@ -76,22 +82,26 @@ function createNotifications(machineID: string, date: Date) {
76 82
  * @param isEnabled True to enable notifications, false to disable
77 83
  * @param endDate The trigger date, or null if disabling notifications
78 84
  */
79
-export async function setupMachineNotification(machineID: string, isEnabled: boolean, endDate: Date | null) {
80
-    return new Promise((resolve, reject) => {
81
-        if (isEnabled && endDate != null) {
82
-            askPermissions()
83
-                .then(() => {
84
-                    createNotifications(machineID, endDate);
85
-                    resolve();
86
-                })
87
-                .catch(() => {
88
-                    reject();
89
-                });
90
-        } else {
91
-            PushNotification.cancelLocalNotifications({id: machineID});
92
-            let reminderId = reminderIdFactor * parseInt(machineID);
93
-            PushNotification.cancelLocalNotifications({id: reminderId.toString()});
94
-            resolve();
95
-        }
96
-    });
85
+export async function setupMachineNotification(
86
+  machineID: string,
87
+  isEnabled: boolean,
88
+  endDate: Date | null,
89
+): Promise<void> {
90
+  return new Promise((resolve: () => void, reject: () => void) => {
91
+    if (isEnabled && endDate != null) {
92
+      askPermissions()
93
+        .then(() => {
94
+          createNotifications(machineID, endDate);
95
+          resolve();
96
+        })
97
+        .catch(() => {
98
+          reject();
99
+        });
100
+    } else {
101
+      PushNotification.cancelLocalNotifications({id: machineID});
102
+      const reminderId = reminderIdFactor * parseInt(machineID, 10);
103
+      PushNotification.cancelLocalNotifications({id: reminderId.toString()});
104
+      resolve();
105
+    }
106
+  });
97 107
 }

+ 21
- 11
src/utils/withCollapsible.js View File

@@ -1,5 +1,7 @@
1
-import React from 'react';
2
-import {useCollapsibleStack} from "react-navigation-collapsible";
1
+// @flow
2
+
3
+import * as React from 'react';
4
+import {useCollapsibleStack} from 'react-navigation-collapsible';
3 5
 
4 6
 /**
5 7
  * Function used to manipulate Collapsible Hooks from a class.
@@ -14,12 +16,20 @@ import {useCollapsibleStack} from "react-navigation-collapsible";
14 16
  * @param Component The component to use Collapsible with
15 17
  * @returns {React.ComponentType<any>}
16 18
  */
17
-export const withCollapsible = (Component: React.ComponentType<any>) => {
18
-    return React.forwardRef((props: any, ref: any) => {
19
-        return <Component
20
-            collapsibleStack={useCollapsibleStack()}
21
-            ref={ref}
22
-            {...props}
23
-        />;
24
-    });
25
-};
19
+export default function withCollapsible(
20
+  // eslint-disable-next-line flowtype/no-weak-types
21
+  Component: React.ComponentType<any>,
22
+  // eslint-disable-next-line flowtype/no-weak-types
23
+): React$AbstractComponent<any, any> {
24
+  // eslint-disable-next-line flowtype/no-weak-types
25
+  return React.forwardRef((props: any, ref: any): React.Node => {
26
+    return (
27
+      <Component
28
+        collapsibleStack={useCollapsibleStack()}
29
+        ref={ref}
30
+        // eslint-disable-next-line react/jsx-props-no-spreading
31
+        {...props}
32
+      />
33
+    );
34
+  });
35
+}

Loading…
Cancel
Save