Improve utils files to match linter

This commit is contained in:
Arnaud Vergnet 2020-08-05 18:52:18 +02:00
parent fcbc70956b
commit 569e659779
3 changed files with 91 additions and 71 deletions

View file

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

View file

@ -1,10 +1,14 @@
// @flow // @flow
import {checkNotifications, requestNotifications, RESULTS} from 'react-native-permissions'; import {
import AsyncStorageManager from "../managers/AsyncStorageManager"; checkNotifications,
import i18n from "i18n-js"; requestNotifications,
RESULTS,
} from 'react-native-permissions';
import i18n from 'i18n-js';
import AsyncStorageManager from '../managers/AsyncStorageManager';
const PushNotification = require("react-native-push-notification"); const PushNotification = require('react-native-push-notification');
// Used to multiply the normal notification id to create the reminder one. It allows to find it back easily // Used to multiply the normal notification id to create the reminder one. It allows to find it back easily
const reminderIdFactor = 100; const reminderIdFactor = 100;
@ -13,25 +17,21 @@ const reminderIdFactor = 100;
* Async function asking permission to send notifications to the user. * Async function asking permission to send notifications to the user.
* Used on ios. * Used on ios.
* *
* @returns {Promise} * @returns {Promise<void>}
*/ */
export async function askPermissions() { export async function askPermissions(): Promise<void> {
return new Promise(((resolve, reject) => { return new Promise((resolve: () => void, reject: () => void) => {
checkNotifications().then(({status}) => { checkNotifications().then(({status}: {status: string}) => {
if (status === RESULTS.GRANTED) if (status === RESULTS.GRANTED) resolve();
resolve(); else if (status === RESULTS.BLOCKED) reject();
else if (status === RESULTS.BLOCKED)
reject()
else { else {
requestNotifications().then(({status}) => { requestNotifications().then((result: {status: string}) => {
if (status === RESULTS.GRANTED) if (result.status === RESULTS.GRANTED) resolve();
resolve(); else reject();
else
reject();
}); });
} }
}); });
})); });
} }
/** /**
@ -43,26 +43,32 @@ export async function askPermissions() {
* @param date The date to trigger the notification at * @param date The date to trigger the notification at
*/ */
function createNotifications(machineID: string, date: Date) { function createNotifications(machineID: string, date: Date) {
let reminder = AsyncStorageManager.getNumber(AsyncStorageManager.PREFERENCES.proxiwashNotifications.key); const reminder = AsyncStorageManager.getNumber(
if (!isNaN(reminder) && reminder > 0) { AsyncStorageManager.PREFERENCES.proxiwashNotifications.key,
let id = reminderIdFactor * parseInt(machineID); );
let reminderDate = new Date(date); if (!Number.isNaN(reminder) && reminder > 0) {
const id = reminderIdFactor * parseInt(machineID, 10);
const reminderDate = new Date(date);
reminderDate.setMinutes(reminderDate.getMinutes() - reminder); reminderDate.setMinutes(reminderDate.getMinutes() - reminder);
PushNotification.localNotificationSchedule({ PushNotification.localNotificationSchedule({
title: i18n.t("screens.proxiwash.notifications.machineRunningTitle", {time: reminder}), title: i18n.t('screens.proxiwash.notifications.machineRunningTitle', {
message: i18n.t("screens.proxiwash.notifications.machineRunningBody", {number: machineID}), time: reminder,
}),
message: i18n.t('screens.proxiwash.notifications.machineRunningBody', {
number: machineID,
}),
id: id.toString(), id: id.toString(),
date: reminderDate, date: reminderDate,
}); });
console.log("Setting up notifications for ", date, " and reminder for ", reminderDate); }
} else
console.log("Setting up notifications for ", date);
PushNotification.localNotificationSchedule({ PushNotification.localNotificationSchedule({
title: i18n.t("screens.proxiwash.notifications.machineFinishedTitle"), title: i18n.t('screens.proxiwash.notifications.machineFinishedTitle'),
message: i18n.t("screens.proxiwash.notifications.machineFinishedBody", {number: machineID}), message: i18n.t('screens.proxiwash.notifications.machineFinishedBody', {
number: machineID,
}),
id: machineID, id: machineID,
date: date, date,
}); });
} }
@ -76,8 +82,12 @@ function createNotifications(machineID: string, date: Date) {
* @param isEnabled True to enable notifications, false to disable * @param isEnabled True to enable notifications, false to disable
* @param endDate The trigger date, or null if disabling notifications * @param endDate The trigger date, or null if disabling notifications
*/ */
export async function setupMachineNotification(machineID: string, isEnabled: boolean, endDate: Date | null) { export async function setupMachineNotification(
return new Promise((resolve, reject) => { machineID: string,
isEnabled: boolean,
endDate: Date | null,
): Promise<void> {
return new Promise((resolve: () => void, reject: () => void) => {
if (isEnabled && endDate != null) { if (isEnabled && endDate != null) {
askPermissions() askPermissions()
.then(() => { .then(() => {
@ -89,7 +99,7 @@ export async function setupMachineNotification(machineID: string, isEnabled: boo
}); });
} else { } else {
PushNotification.cancelLocalNotifications({id: machineID}); PushNotification.cancelLocalNotifications({id: machineID});
let reminderId = reminderIdFactor * parseInt(machineID); const reminderId = reminderIdFactor * parseInt(machineID, 10);
PushNotification.cancelLocalNotifications({id: reminderId.toString()}); PushNotification.cancelLocalNotifications({id: reminderId.toString()});
resolve(); resolve();
} }

View file

@ -1,5 +1,7 @@
import React from 'react'; // @flow
import {useCollapsibleStack} from "react-navigation-collapsible";
import * as React from 'react';
import {useCollapsibleStack} from 'react-navigation-collapsible';
/** /**
* Function used to manipulate Collapsible Hooks from a class. * Function used to manipulate Collapsible Hooks from a class.
@ -14,12 +16,20 @@ import {useCollapsibleStack} from "react-navigation-collapsible";
* @param Component The component to use Collapsible with * @param Component The component to use Collapsible with
* @returns {React.ComponentType<any>} * @returns {React.ComponentType<any>}
*/ */
export const withCollapsible = (Component: React.ComponentType<any>) => { export default function withCollapsible(
return React.forwardRef((props: any, ref: any) => { // eslint-disable-next-line flowtype/no-weak-types
return <Component Component: React.ComponentType<any>,
// eslint-disable-next-line flowtype/no-weak-types
): React$AbstractComponent<any, any> {
// eslint-disable-next-line flowtype/no-weak-types
return React.forwardRef((props: any, ref: any): React.Node => {
return (
<Component
collapsibleStack={useCollapsibleStack()} collapsibleStack={useCollapsibleStack()}
ref={ref} ref={ref}
// eslint-disable-next-line react/jsx-props-no-spreading
{...props} {...props}
/>; />
);
}); });
}; }