diff options
author | Stefan Malzner <stefan@adlk.io> | 2019-04-30 15:23:38 +0200 |
---|---|---|
committer | Stefan Malzner <stefan@adlk.io> | 2019-04-30 15:23:38 +0200 |
commit | 1bae1dfcbc4a5f590c51103635006198ae6a91d6 (patch) | |
tree | c838e9e7e18342c01e3c3b46c8e9ca4b74895e3b /src/features | |
parent | Update CHANGELOG.md (diff) | |
download | ferdium-app-1bae1dfcbc4a5f590c51103635006198ae6a91d6.tar.gz ferdium-app-1bae1dfcbc4a5f590c51103635006198ae6a91d6.tar.zst ferdium-app-1bae1dfcbc4a5f590c51103635006198ae6a91d6.zip |
Enforce service limit
Diffstat (limited to 'src/features')
-rw-r--r-- | src/features/serviceLimit/components/LimitReachedInfobox.js | 74 | ||||
-rw-r--r-- | src/features/serviceLimit/index.js | 33 | ||||
-rw-r--r-- | src/features/serviceLimit/store.js | 41 |
3 files changed, 148 insertions, 0 deletions
diff --git a/src/features/serviceLimit/components/LimitReachedInfobox.js b/src/features/serviceLimit/components/LimitReachedInfobox.js new file mode 100644 index 000000000..ee0d7cb27 --- /dev/null +++ b/src/features/serviceLimit/components/LimitReachedInfobox.js | |||
@@ -0,0 +1,74 @@ | |||
1 | import React, { Component } from 'react'; | ||
2 | import PropTypes from 'prop-types'; | ||
3 | import { inject, observer } from 'mobx-react'; | ||
4 | import { defineMessages, intlShape } from 'react-intl'; | ||
5 | import injectSheet from 'react-jss'; | ||
6 | import { Infobox } from '@meetfranz/ui'; | ||
7 | |||
8 | import { gaEvent } from '../../../lib/analytics'; | ||
9 | |||
10 | const messages = defineMessages({ | ||
11 | limitReached: { | ||
12 | id: 'feature.serviceLimit.limitReached', | ||
13 | defaultMessage: '!!!You have added {amount} of {limit} services. Please upgrade your account to add more services.', | ||
14 | }, | ||
15 | action: { | ||
16 | id: 'premiumFeature.button.upgradeAccount', | ||
17 | defaultMessage: '!!!Upgrade account', | ||
18 | }, | ||
19 | }); | ||
20 | |||
21 | const styles = theme => ({ | ||
22 | container: { | ||
23 | height: 'auto', | ||
24 | background: theme.styleTypes.primary.accent, | ||
25 | color: theme.styleTypes.primary.contrast, | ||
26 | borderRadius: 0, | ||
27 | marginBottom: 0, | ||
28 | |||
29 | '& button': { | ||
30 | color: theme.styleTypes.primary.contrast, | ||
31 | }, | ||
32 | }, | ||
33 | }); | ||
34 | |||
35 | |||
36 | @inject('stores', 'actions') @injectSheet(styles) @observer | ||
37 | class LimitReachedInfobox extends Component { | ||
38 | static propTypes = { | ||
39 | classes: PropTypes.object.isRequired, | ||
40 | stores: PropTypes.object.isRequired, | ||
41 | actions: PropTypes.object.isRequired, | ||
42 | }; | ||
43 | |||
44 | static contextTypes = { | ||
45 | intl: intlShape, | ||
46 | }; | ||
47 | |||
48 | render() { | ||
49 | const { classes, stores, actions } = this.props; | ||
50 | const { intl } = this.context; | ||
51 | |||
52 | const { | ||
53 | serviceLimit, | ||
54 | } = stores; | ||
55 | |||
56 | if (!serviceLimit.userHasReachedServiceLimit) return null; | ||
57 | |||
58 | return ( | ||
59 | <Infobox | ||
60 | icon="mdiInformation" | ||
61 | className={classes.container} | ||
62 | ctaLabel={intl.formatMessage(messages.action)} | ||
63 | ctaOnClick={() => { | ||
64 | actions.ui.openSettings({ path: 'user' }); | ||
65 | gaEvent('Service Limit', 'upgrade', 'Upgrade account'); | ||
66 | }} | ||
67 | > | ||
68 | {intl.formatMessage(messages.limitReached, { amount: serviceLimit.serviceCount, limit: serviceLimit.serviceLimit })} | ||
69 | </Infobox> | ||
70 | ); | ||
71 | } | ||
72 | } | ||
73 | |||
74 | export default LimitReachedInfobox; | ||
diff --git a/src/features/serviceLimit/index.js b/src/features/serviceLimit/index.js new file mode 100644 index 000000000..76f996195 --- /dev/null +++ b/src/features/serviceLimit/index.js | |||
@@ -0,0 +1,33 @@ | |||
1 | import { reaction } from 'mobx'; | ||
2 | import { ServiceLimitStore } from './store'; | ||
3 | |||
4 | const debug = require('debug')('Franz:feature:serviceLimit'); | ||
5 | |||
6 | export const DEFAULT_SERVICE_LIMIT = 3; | ||
7 | |||
8 | let store = null; | ||
9 | |||
10 | export const serviceLimitStore = new ServiceLimitStore(); | ||
11 | |||
12 | export default function initServiceLimit(stores, actions) { | ||
13 | const { features } = stores; | ||
14 | |||
15 | // Toggle serviceLimit feature | ||
16 | reaction( | ||
17 | () => ( | ||
18 | features.features.hasServiceLimit | ||
19 | ), | ||
20 | (isEnabled) => { | ||
21 | if (isEnabled) { | ||
22 | debug('Initializing `serviceLimit` feature'); | ||
23 | store = serviceLimitStore.start(stores, actions); | ||
24 | } else if (store) { | ||
25 | debug('Disabling `serviceLimit` feature'); | ||
26 | serviceLimitStore.stop(); | ||
27 | } | ||
28 | }, | ||
29 | { | ||
30 | fireImmediately: true, | ||
31 | }, | ||
32 | ); | ||
33 | } | ||
diff --git a/src/features/serviceLimit/store.js b/src/features/serviceLimit/store.js new file mode 100644 index 000000000..752f71371 --- /dev/null +++ b/src/features/serviceLimit/store.js | |||
@@ -0,0 +1,41 @@ | |||
1 | import { computed, observable } from 'mobx'; | ||
2 | import { FeatureStore } from '../utils/FeatureStore'; | ||
3 | import { DEFAULT_SERVICE_LIMIT } from '.'; | ||
4 | |||
5 | const debug = require('debug')('Franz:feature:serviceLimit:store'); | ||
6 | |||
7 | export class ServiceLimitStore extends FeatureStore { | ||
8 | @observable isServiceLimitEnabled = false; | ||
9 | |||
10 | start(stores, actions) { | ||
11 | debug('start'); | ||
12 | this.stores = stores; | ||
13 | this.actions = actions; | ||
14 | |||
15 | this.isServiceLimitEnabled = true; | ||
16 | } | ||
17 | |||
18 | stop() { | ||
19 | super.stop(); | ||
20 | |||
21 | this.isServiceLimitEnabled = false; | ||
22 | } | ||
23 | |||
24 | @computed get userHasReachedServiceLimit() { | ||
25 | if (!this.isServiceLimitEnabled) return false; | ||
26 | |||
27 | const { user } = this.stores; | ||
28 | |||
29 | return !user.isPremium && this.serviceCount >= this.serviceLimit; | ||
30 | } | ||
31 | |||
32 | @computed get serviceLimit() { | ||
33 | return this.stores.features.features.serviceLimitCount || DEFAULT_SERVICE_LIMIT; | ||
34 | } | ||
35 | |||
36 | @computed get serviceCount() { | ||
37 | return this.stores.services.all.length; | ||
38 | } | ||
39 | } | ||
40 | |||
41 | export default ServiceLimitStore; | ||