From df4d195ec7135c76f8f5de54c3939d2e74e80d4a Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Sat, 24 Nov 2018 20:20:31 +0100 Subject: Delay app based on featureAPI settings --- src/features/delayApp/Component.js | 86 ++++++++++++++++++++++++++++++++++++++ src/features/delayApp/index.js | 67 +++++++++++++++++++++++++++++ src/features/delayApp/styles.js | 26 ++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 src/features/delayApp/Component.js create mode 100644 src/features/delayApp/index.js create mode 100644 src/features/delayApp/styles.js (limited to 'src/features/delayApp') diff --git a/src/features/delayApp/Component.js b/src/features/delayApp/Component.js new file mode 100644 index 000000000..2bfa1162e --- /dev/null +++ b/src/features/delayApp/Component.js @@ -0,0 +1,86 @@ +import React, { Component } from 'react'; +import PropTypes from 'prop-types'; +import { inject, observer } from 'mobx-react'; +import { defineMessages, intlShape } from 'react-intl'; +import injectSheet from 'react-jss'; + +import Button from '../../components/ui/Button'; + +import { config } from './'; +import styles from './styles'; + +const messages = defineMessages({ + headline: { + id: 'feature.delayApp.headline', + defaultMessage: '!!!Please purchase license to skip waiting', + }, + action: { + id: 'feature.delayApp.action', + defaultMessage: '!!!Get a Franz Supporter License', + }, + text: { + id: 'feature.delayApp.text', + defaultMessage: '!!!Franz will continue in {seconds} seconds.', + }, +}); + +export default @inject('actions') @observer @injectSheet(styles) class DelayApp extends Component { + static propTypes = { + // eslint-disable-next-line + classes: PropTypes.object.isRequired, + }; + + static contextTypes = { + intl: intlShape, + }; + + state = { + countdown: config.delayDuration, + } + + componentDidMount() { + // const { reload } = this.props; + + this.countdownInterval = setInterval(() => { + this.setState({ + countdown: this.state.countdown - this.countdownIntervalTimeout, + }); + + if (this.state.countdown <= 0) { + // reload(); + clearInterval(this.countdownInterval); + } + }, this.countdownIntervalTimeout); + } + + countdownInterval = null; + countdownIntervalTimeout = 1000; + + render() { + const { classes, actions } = this.props; + const { intl } = this.context; + + return ( +
+

{intl.formatMessage(messages.headline)}

+
+ ); + } +} + +DelayApp.wrappedComponent.propTypes = { + actions: PropTypes.shape({ + ui: PropTypes.shape({ + openSettings: PropTypes.func.isRequired, + }).isRequired, + }).isRequired, +}; diff --git a/src/features/delayApp/index.js b/src/features/delayApp/index.js new file mode 100644 index 000000000..a3cce03ee --- /dev/null +++ b/src/features/delayApp/index.js @@ -0,0 +1,67 @@ +import { autorun, observable, reaction } from 'mobx'; +import moment from 'moment'; +import DelayAppComponent from './Component'; + +const debug = require('debug')('Franz:feature:delayApp'); + +const DEFAULT_DELAY_DURATION = 10000; // 10 seconds +const DEFAULT_DELAY_OFFSET = 3600000; // 1 hour +const DEFAULT_VISIBILITY = false; + +export const config = { + delayOffset: DEFAULT_DELAY_OFFSET, + delayDuration: DEFAULT_DELAY_DURATION, +}; + +export const state = observable({ + isDelayAppScreenVisible: DEFAULT_VISIBILITY, +}); + +function setVisibility(value) { + Object.assign(state, { + isDelayAppScreenVisible: value, + }); +} + +export default function init(stores) { + reaction( + () => stores.features.features.needToWaitToProceed, + (enabled, r) => { + if (enabled) { + debug('Initializing `delayApp` feature'); + + // Dispose the reaction to run this only once + r.dispose(); + + const { needToWaitToProceedConfig: globalConfig } = stores.features.features; + + let shownAfterLaunch = false; + let timeLastDelay = moment(); + + config.delayOffset = globalConfig.delayOffset || DEFAULT_DELAY_OFFSET; + config.delayDuration = globalConfig.wait || DEFAULT_DELAY_DURATION; + + autorun(() => { + const diff = moment().diff(timeLastDelay); + if ((stores.app.isFocused && diff >= config.delayOffset) || !shownAfterLaunch) { + debug(`App will be delayed for ${config.delayDuration / 1000}s`); + + setVisibility(true); + + timeLastDelay = moment(); + shownAfterLaunch = true; + + setTimeout(() => { + debug('Resetting app delay'); + + setVisibility(false); + }, DEFAULT_DELAY_DURATION + 1000); // timer needs to be able to hit 0 + } + }); + } + }, + ); +} + +export const component = DelayAppComponent; + diff --git a/src/features/delayApp/styles.js b/src/features/delayApp/styles.js new file mode 100644 index 000000000..097368d9a --- /dev/null +++ b/src/features/delayApp/styles.js @@ -0,0 +1,26 @@ +export default (theme) => { + console.log(theme); + return ({ + container: { + background: theme.colorBackground, + position: 'absolute', + top: 0, + width: '100%', + display: 'flex', + 'flex-direction': 'column', + 'align-items': 'center', + 'justify-content': 'center', + 'z-index': 150, + }, + headline: { + color: theme.colorHeadline, + margin: [25, 0, 40], + 'max-width': 500, + 'text-align': 'center', + 'line-height': '1.3em', + }, + button: { + margin: [40, 0, 20], + }, + }); +}; -- cgit v1.2.3-54-g00ecf