diff --git a/src/components/Amicale/LogoutDialog.js b/src/components/Amicale/LogoutDialog.js index 71e208b..1c6066b 100644 --- a/src/components/Amicale/LogoutDialog.js +++ b/src/components/Amicale/LogoutDialog.js @@ -2,45 +2,46 @@ import * as React from 'react'; import i18n from 'i18n-js'; -import LoadingConfirmDialog from "../Dialogs/LoadingConfirmDialog"; -import ConnectionManager from "../../managers/ConnectionManager"; -import {StackNavigationProp} from "@react-navigation/stack"; +import {StackNavigationProp} from '@react-navigation/stack'; +import LoadingConfirmDialog from '../Dialogs/LoadingConfirmDialog'; +import ConnectionManager from '../../managers/ConnectionManager'; -type Props = { - navigation: StackNavigationProp, - visible: boolean, - onDismiss: () => void, -} +type PropsType = { + navigation: StackNavigationProp, + visible: boolean, + onDismiss: () => void, +}; -class LogoutDialog extends React.PureComponent { - - onClickAccept = async () => { - return new Promise((resolve) => { - ConnectionManager.getInstance().disconnect() - .then(() => { - this.props.navigation.reset({ - index: 0, - routes: [{name: 'main'}], - }); - this.props.onDismiss(); - resolve(); - }); +class LogoutDialog extends React.PureComponent { + onClickAccept = async (): Promise => { + const {props} = this; + return new Promise((resolve: () => void) => { + ConnectionManager.getInstance() + .disconnect() + .then(() => { + props.navigation.reset({ + index: 0, + routes: [{name: 'main'}], + }); + props.onDismiss(); + resolve(); }); - }; + }); + }; - render() { - return ( - - ); - } + render(): React.Node { + const {props} = this; + return ( + + ); + } } export default LogoutDialog; diff --git a/src/components/Dialogs/AlertDialog.js b/src/components/Dialogs/AlertDialog.js index d0564cc..9409688 100644 --- a/src/components/Dialogs/AlertDialog.js +++ b/src/components/Dialogs/AlertDialog.js @@ -2,34 +2,32 @@ import * as React from 'react'; import {Button, Dialog, Paragraph, Portal} from 'react-native-paper'; -import i18n from "i18n-js"; +import i18n from 'i18n-js'; -type Props = { - visible: boolean, - onDismiss: () => void, - title: string, - message: string, -} +type PropsType = { + visible: boolean, + onDismiss: () => void, + title: string, + message: string, +}; -class AlertDialog extends React.PureComponent { - - render() { - return ( - - - {this.props.title} - - {this.props.message} - - - - - - - ); - } +class AlertDialog extends React.PureComponent { + render(): React.Node { + const {props} = this; + return ( + + + {props.title} + + {props.message} + + + + + + + ); + } } export default AlertDialog; diff --git a/src/components/Dialogs/ErrorDialog.js b/src/components/Dialogs/ErrorDialog.js index 68d4f3f..36558b4 100644 --- a/src/components/Dialogs/ErrorDialog.js +++ b/src/components/Dialogs/ErrorDialog.js @@ -1,64 +1,71 @@ // @flow import * as React from 'react'; -import i18n from "i18n-js"; -import {ERROR_TYPE} from "../../utils/WebData"; -import AlertDialog from "./AlertDialog"; +import i18n from 'i18n-js'; +import {ERROR_TYPE} from '../../utils/WebData'; +import AlertDialog from './AlertDialog'; -type Props = { - visible: boolean, - onDismiss: () => void, - errorCode: number, -} +type PropsType = { + visible: boolean, + onDismiss: () => void, + errorCode: number, +}; -class ErrorDialog extends React.PureComponent { +class ErrorDialog extends React.PureComponent { + title: string; - title: string; - message: string; + message: string; - generateMessage() { - this.title = i18n.t("errors.title"); - switch (this.props.errorCode) { - case ERROR_TYPE.BAD_CREDENTIALS: - this.message = i18n.t("errors.badCredentials"); - break; - case ERROR_TYPE.BAD_TOKEN: - this.message = i18n.t("errors.badToken"); - break; - case ERROR_TYPE.NO_CONSENT: - this.message = i18n.t("errors.noConsent"); - break; - case ERROR_TYPE.TOKEN_SAVE: - this.message = i18n.t("errors.tokenSave"); - break; - case ERROR_TYPE.TOKEN_RETRIEVE: - this.message = i18n.t("errors.unknown"); - break; - case ERROR_TYPE.BAD_INPUT: - this.message = i18n.t("errors.badInput"); - break; - case ERROR_TYPE.FORBIDDEN: - this.message = i18n.t("errors.forbidden"); - break; - case ERROR_TYPE.CONNECTION_ERROR: - this.message = i18n.t("errors.connectionError"); - break; - case ERROR_TYPE.SERVER_ERROR: - this.message = i18n.t("errors.serverError"); - break; - default: - this.message = i18n.t("errors.unknown"); - break; - } - this.message += "\n\nCode " + this.props.errorCode; + generateMessage() { + const {props} = this; + this.title = i18n.t('errors.title'); + switch (props.errorCode) { + case ERROR_TYPE.BAD_CREDENTIALS: + this.message = i18n.t('errors.badCredentials'); + break; + case ERROR_TYPE.BAD_TOKEN: + this.message = i18n.t('errors.badToken'); + break; + case ERROR_TYPE.NO_CONSENT: + this.message = i18n.t('errors.noConsent'); + break; + case ERROR_TYPE.TOKEN_SAVE: + this.message = i18n.t('errors.tokenSave'); + break; + case ERROR_TYPE.TOKEN_RETRIEVE: + this.message = i18n.t('errors.unknown'); + break; + case ERROR_TYPE.BAD_INPUT: + this.message = i18n.t('errors.badInput'); + break; + case ERROR_TYPE.FORBIDDEN: + this.message = i18n.t('errors.forbidden'); + break; + case ERROR_TYPE.CONNECTION_ERROR: + this.message = i18n.t('errors.connectionError'); + break; + case ERROR_TYPE.SERVER_ERROR: + this.message = i18n.t('errors.serverError'); + break; + default: + this.message = i18n.t('errors.unknown'); + break; } + this.message += `\n\nCode ${props.errorCode}`; + } - render() { - this.generateMessage(); - return ( - - ); - } + render(): React.Node { + this.generateMessage(); + const {props} = this; + return ( + + ); + } } export default ErrorDialog; diff --git a/src/components/Dialogs/LoadingConfirmDialog.js b/src/components/Dialogs/LoadingConfirmDialog.js index 922bc7c..127aadb 100644 --- a/src/components/Dialogs/LoadingConfirmDialog.js +++ b/src/components/Dialogs/LoadingConfirmDialog.js @@ -1,92 +1,106 @@ // @flow import * as React from 'react'; -import {ActivityIndicator, Button, Dialog, Paragraph, Portal} from 'react-native-paper'; -import i18n from "i18n-js"; +import { + ActivityIndicator, + Button, + Dialog, + Paragraph, + Portal, +} from 'react-native-paper'; +import i18n from 'i18n-js'; -type Props = { - visible: boolean, - onDismiss: () => void, - onAccept: () => Promise, // async function to be executed - title: string, - titleLoading: string, - message: string, - startLoading: boolean, -} +type PropsType = { + visible: boolean, + onDismiss?: () => void, + onAccept?: () => Promise, // async function to be executed + title?: string, + titleLoading?: string, + message?: string, + startLoading?: boolean, +}; -type State = { - loading: boolean, -} +type StateType = { + loading: boolean, +}; -class LoadingConfirmDialog extends React.PureComponent { +class LoadingConfirmDialog extends React.PureComponent { + static defaultProps = { + onDismiss: () => {}, + onAccept: (): Promise => { + return Promise.resolve(); + }, + title: '', + titleLoading: '', + message: '', + startLoading: false, + }; - static defaultProps = { - title: '', - message: '', - onDismiss: () => {}, - onAccept: () => {return Promise.resolve()}, - startLoading: false, - } - - state = { - loading: this.props.startLoading, + constructor(props: PropsType) { + super(props); + this.state = { + loading: + props.startLoading != null + ? props.startLoading + : LoadingConfirmDialog.defaultProps.startLoading, }; + } - /** - * Set the dialog into loading state and closes it when operation finishes - */ - onClickAccept = () => { - this.setState({loading: true}); - this.props.onAccept().then(this.hideLoading); - }; + /** + * Set the dialog into loading state and closes it when operation finishes + */ + onClickAccept = () => { + const {props} = this; + this.setState({loading: true}); + if (props.onAccept != null) props.onAccept().then(this.hideLoading); + }; - /** - * Waits for fade out animations to finish before hiding loading - * @returns {TimeoutID} - */ - hideLoading = () => setTimeout(() => { - this.setState({loading: false}) + /** + * Waits for fade out animations to finish before hiding loading + * @returns {TimeoutID} + */ + hideLoading = (): TimeoutID => + setTimeout(() => { + this.setState({loading: false}); }, 200); - /** - * Hide the dialog if it is not loading - */ - onDismiss = () => { - if (!this.state.loading) - this.props.onDismiss(); - }; + /** + * Hide the dialog if it is not loading + */ + onDismiss = () => { + const {state, props} = this; + if (!state.loading && props.onDismiss != null) props.onDismiss(); + }; - render() { - return ( - - - - {this.state.loading - ? this.props.titleLoading - : this.props.title} - - - {this.state.loading - ? - : {this.props.message} - } - - {this.state.loading - ? null - : - - - - } - - - ); - } + render(): React.Node { + const {state, props} = this; + return ( + + + + {state.loading ? props.titleLoading : props.title} + + + {state.loading ? ( + + ) : ( + {props.message} + )} + + {state.loading ? null : ( + + + + + )} + + + ); + } } export default LoadingConfirmDialog; diff --git a/src/components/Dialogs/OptionsDialog.js b/src/components/Dialogs/OptionsDialog.js index 3503a44..ea3e11d 100644 --- a/src/components/Dialogs/OptionsDialog.js +++ b/src/components/Dialogs/OptionsDialog.js @@ -2,55 +2,50 @@ import * as React from 'react'; import {Button, Dialog, Paragraph, Portal} from 'react-native-paper'; -import {FlatList} from "react-native"; +import {FlatList} from 'react-native'; -export type OptionsDialogButton = { - title: string, - onPress: () => void, -} +export type OptionsDialogButtonType = { + title: string, + onPress: () => void, +}; -type Props = { - visible: boolean, - title: string, - message: string, - buttons: Array, - onDismiss: () => void, -} +type PropsType = { + visible: boolean, + title: string, + message: string, + buttons: Array, + onDismiss: () => void, +}; -class OptionsDialog extends React.PureComponent { +class OptionsDialog extends React.PureComponent { + getButtonRender = ({item}: {item: OptionsDialogButtonType}): React.Node => { + return ; + }; - getButtonRender = ({item}: { item: OptionsDialogButton }) => { - return ; - } + keyExtractor = (item: OptionsDialogButtonType): string => item.title; - keyExtractor = (item: OptionsDialogButton) => item.title; - - render() { - return ( - - - {this.props.title} - - {this.props.message} - - - - - - - ); - } + render(): React.Node { + const {props} = this; + return ( + + + {props.title} + + {props.message} + + + + + + + ); + } } export default OptionsDialog;