From 8b8be2d7cafccbaee56233acd1fcac491a8e82eb Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 8 Jan 2019 20:49:47 +0100 Subject: [PATCH] Updated stateFlagTimeout to get the setTimeout function injected as a dependency --- src/servers/CreateServer.js | 3 +-- src/servers/services/provideServices.js | 2 +- src/short-urls/CreateShortUrl.js | 3 +-- src/short-urls/helpers/CreateShortUrlResult.js | 7 ++++--- src/short-urls/helpers/ShortUrlsRow.js | 7 +++++-- src/short-urls/services/provideServices.js | 6 ++++-- src/utils/services/provideServices.js | 4 ++++ src/utils/utils.js | 7 ++++++- 8 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/servers/CreateServer.js b/src/servers/CreateServer.js index fbea083e..42f41410 100644 --- a/src/servers/CreateServer.js +++ b/src/servers/CreateServer.js @@ -2,12 +2,11 @@ import { assoc, dissoc, pipe } from 'ramda'; import React from 'react'; import { v4 as uuid } from 'uuid'; import PropTypes from 'prop-types'; -import { stateFlagTimeout } from '../utils/utils'; import './CreateServer.scss'; const SHOW_IMPORT_MSG_TIME = 4000; -const CreateServer = (ImportServersBtn) => class CreateServer extends React.Component { +const CreateServer = (ImportServersBtn, stateFlagTimeout) => class CreateServer extends React.Component { static propTypes = { createServer: PropTypes.func, history: PropTypes.shape({ diff --git a/src/servers/services/provideServices.js b/src/servers/services/provideServices.js index c12f1b35..ff18b1b5 100644 --- a/src/servers/services/provideServices.js +++ b/src/servers/services/provideServices.js @@ -12,7 +12,7 @@ import ServersExporter from './ServersExporter'; const provideServices = (bottle, connect, withRouter) => { // Components - bottle.serviceFactory('CreateServer', CreateServer, 'ImportServersBtn'); + bottle.serviceFactory('CreateServer', CreateServer, 'ImportServersBtn', 'stateFlagTimeout'); bottle.decorator('CreateServer', connect([ 'selectedServer' ], [ 'createServer', 'resetSelectedServer' ])); bottle.serviceFactory('ServersDropdown', ServersDropdown, 'ServersExporter'); diff --git a/src/short-urls/CreateShortUrl.js b/src/short-urls/CreateShortUrl.js index fdcd5bca..89838947 100644 --- a/src/short-urls/CreateShortUrl.js +++ b/src/short-urls/CreateShortUrl.js @@ -5,13 +5,12 @@ import React from 'react'; import { Collapse } from 'reactstrap'; import * as PropTypes from 'prop-types'; import DateInput from '../utils/DateInput'; -import CreateShortUrlResult from './helpers/CreateShortUrlResult'; import { createShortUrlResultType } from './reducers/shortUrlCreation'; const normalizeTag = pipe(trim, replace(/ /g, '-')); const formatDate = (date) => isNil(date) ? date : date.format(); -const CreateShortUrl = (TagsSelector) => class CreateShortUrl extends React.Component { +const CreateShortUrl = (TagsSelector, CreateShortUrlResult) => class CreateShortUrl extends React.Component { static propTypes = { createShortUrl: PropTypes.func, shortUrlCreationResult: createShortUrlResultType, diff --git a/src/short-urls/helpers/CreateShortUrlResult.js b/src/short-urls/helpers/CreateShortUrlResult.js index 5e43e8cd..6bebb8f1 100644 --- a/src/short-urls/helpers/CreateShortUrlResult.js +++ b/src/short-urls/helpers/CreateShortUrlResult.js @@ -6,10 +6,9 @@ import { CopyToClipboard } from 'react-copy-to-clipboard'; import { Card, CardBody, Tooltip } from 'reactstrap'; import PropTypes from 'prop-types'; import { createShortUrlResultType } from '../reducers/shortUrlCreation'; -import { stateFlagTimeout } from '../../utils/utils'; import './CreateShortUrlResult.scss'; -export default class CreateShortUrlResult extends React.Component { +const CreateShortUrlResult = (stateFlagTimeout) => class CreateShortUrlResult extends React.Component { static propTypes = { resetCreateShortUrl: PropTypes.func, error: PropTypes.bool, @@ -61,4 +60,6 @@ export default class CreateShortUrlResult extends React.Component { ); } -} +}; + +export default CreateShortUrlResult; diff --git a/src/short-urls/helpers/ShortUrlsRow.js b/src/short-urls/helpers/ShortUrlsRow.js index 9d9e874d..80bc60ef 100644 --- a/src/short-urls/helpers/ShortUrlsRow.js +++ b/src/short-urls/helpers/ShortUrlsRow.js @@ -6,11 +6,14 @@ import { shortUrlsListParamsType } from '../reducers/shortUrlsListParams'; import { serverType } from '../../servers/prop-types'; import ExternalLink from '../../utils/ExternalLink'; import { shortUrlType } from '../reducers/shortUrlsList'; -import { stateFlagTimeout } from '../../utils/utils'; import Tag from '../../tags/helpers/Tag'; import './ShortUrlsRow.scss'; -const ShortUrlsRow = (ShortUrlsRowMenu, colorGenerator) => class ShortUrlsRow extends React.Component { +const ShortUrlsRow = ( + ShortUrlsRowMenu, + colorGenerator, + stateFlagTimeout +) => class ShortUrlsRow extends React.Component { static propTypes = { refreshList: PropTypes.func, shortUrlsListParams: shortUrlsListParamsType, diff --git a/src/short-urls/services/provideServices.js b/src/short-urls/services/provideServices.js index c86811f4..05518828 100644 --- a/src/short-urls/services/provideServices.js +++ b/src/short-urls/services/provideServices.js @@ -8,6 +8,7 @@ import ShortUrlsRowMenu from '../helpers/ShortUrlsRowMenu'; import CreateShortUrl from '../CreateShortUrl'; import DeleteShortUrlModal from '../helpers/DeleteShortUrlModal'; import EditTagsModal from '../helpers/EditTagsModal'; +import CreateShortUrlResult from '../helpers/CreateShortUrlResult'; import { listShortUrls } from '../reducers/shortUrlsList'; import { createShortUrl, resetCreateShortUrl } from '../reducers/shortUrlCreation'; import { deleteShortUrl, resetDeleteShortUrl, shortUrlDeleted } from '../reducers/shortUrlDeletion'; @@ -30,11 +31,12 @@ const provideServices = (bottle, connect) => { [ 'listShortUrls', 'resetShortUrlParams' ] )); - bottle.serviceFactory('ShortUrlsRow', ShortUrlsRow, 'ShortUrlsRowMenu', 'ColorGenerator'); + bottle.serviceFactory('ShortUrlsRow', ShortUrlsRow, 'ShortUrlsRowMenu', 'ColorGenerator', 'stateFlagTimeout'); bottle.serviceFactory('ShortUrlsRowMenu', ShortUrlsRowMenu, 'DeleteShortUrlModal', 'EditTagsModal'); + bottle.serviceFactory('CreateShortUrlResult', CreateShortUrlResult, 'stateFlagTimeout'); - bottle.serviceFactory('CreateShortUrl', CreateShortUrl, 'TagsSelector'); + bottle.serviceFactory('CreateShortUrl', CreateShortUrl, 'TagsSelector', 'CreateShortUrlResult'); bottle.decorator( 'CreateShortUrl', connect([ 'shortUrlCreationResult' ], [ 'createShortUrl', 'resetCreateShortUrl' ]) diff --git a/src/utils/services/provideServices.js b/src/utils/services/provideServices.js index fddb74bd..4c165f37 100644 --- a/src/utils/services/provideServices.js +++ b/src/utils/services/provideServices.js @@ -1,4 +1,5 @@ import axios from 'axios'; +import { stateFlagTimeout } from '../utils'; import Storage from './Storage'; import ColorGenerator from './ColorGenerator'; import buildShlinkApiClient from './ShlinkApiClientBuilder'; @@ -10,6 +11,9 @@ const provideServices = (bottle) => { bottle.constant('axios', axios); bottle.serviceFactory('buildShlinkApiClient', buildShlinkApiClient, 'axios'); + + bottle.constant('setTimeout', global.setTimeout); + bottle.serviceFactory('stateFlagTimeout', stateFlagTimeout, 'setTimeout'); }; export default provideServices; diff --git a/src/utils/utils.js b/src/utils/utils.js index 080005bf..e9d60fbd 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -5,7 +5,12 @@ import markerShadow from 'leaflet/dist/images/marker-shadow.png'; const DEFAULT_TIMEOUT_DELAY = 2000; -export const stateFlagTimeout = (setState, flagName, initialValue = true, delay = DEFAULT_TIMEOUT_DELAY) => { +export const stateFlagTimeout = (setTimeout) => ( + setState, + flagName, + initialValue = true, + delay = DEFAULT_TIMEOUT_DELAY +) => { setState({ [flagName]: initialValue }); setTimeout(() => setState({ [flagName]: !initialValue }), delay); };