Commit 6dde3241 authored by Martin Santangelo's avatar Martin Santangelo

(fix) settings and feature flags loading logic

No related merge requests found
......@@ -76,9 +76,10 @@ sqliteStorageProviderService.get();
apiService.clearCookies();
const mindsSettingsPromise = mindsService.getSettings();
// On app login (runs if the user login or if it is already logged in)
sessionService.onLogin(async () => {
stores.mindsServiceStore.clear();
const user = sessionService.getUser();
Sentry.configureScope(scope => {
......@@ -88,11 +89,9 @@ sessionService.onLogin(async () => {
logService.info('[App] Getting minds settings and onboarding progress');
// load minds settings and boosted content
await Promise.all([mindsService.getSettings(), boostedContentService.load()]);
await Promise.all([mindsSettingsPromise, boostedContentService.load()]);
logService.info('[App] updatting features');
// reload fatures on login
await featureService.updateFeatures();
// register device token into backend on login
......@@ -153,7 +152,6 @@ sessionService.onLogout(() => {
stores.notifications.clearLocal();
stores.groupsBar.clearLocal();
translationService.purgeLanguagesCache();
stores.mindsServiceStore.clear()
});
// disable yellow boxes
......@@ -193,10 +191,6 @@ export default class App extends Component<Props, State> {
constructor(props) {
super(props);
/*if (props.features) {
featureService.injectFeatures(JSON.parse(props.features));
}*/
if (!Text.defaultProps) {
Text.defaultProps = {};
}
......@@ -325,7 +319,7 @@ export default class App extends Component<Props, State> {
);
const tosModal = (
<TosModal user={stores.user} key="tosModal" mindsServiceStore={stores.mindsServiceStore}/>
<TosModal user={stores.user} key="tosModal" />
)
return [ app, keychainModal, blockchainTransactionModal, tosModal];
......
......@@ -28,8 +28,6 @@ import groupsBar from './src/groups/GroupsBarStore';
import sessionService from './src/common/services/session.service';
import logService from './src/common/services/log.service';
import SubscriptionRequestStore from './src/channel/subscription/SubscriptionRequestStore';
import MindsServiceStore from './src/common/stores/MindsServiceStore';
/**
* App stores
......@@ -62,7 +60,6 @@ const stores = {
hashtag: new hashtag(),
onboarding: new onboarding(),
groupsBar: new groupsBar(),
mindsServiceStore: new MindsServiceStore(),
};
/**
......
......@@ -7,7 +7,6 @@ import featuresService from '../../src/common/services/features.service';
import LoginScreen from '../../src/auth/LoginScreen';
jest.mock('../../src/auth/AuthService');
jest.mock('../../src/common/stores/MindsServiceStore');
jest.mock('../../src/auth/LoginForm', () => 'LoginForm');
jest.mock('../../src/auth/ForgotPassword', () => 'ForgotPassword');
......@@ -15,7 +14,6 @@ jest.mock('../../src/common/components/VideoBackground', () => 'VideoBackground'
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
import MindsServiceStore from '../../src/common/stores/MindsServiceStore'
describe('LoginScreen component', () => {
......@@ -24,17 +22,15 @@ describe('LoginScreen component', () => {
});
it('should renders correctly', () => {
const mindsServiceStore = new MindsServiceStore();
const loginScreen = renderer.create(
<LoginScreen mindsServiceStore={mindsServiceStore} />
<LoginScreen />
).toJSON();
expect(loginScreen).toMatchSnapshot();
});
it('should shows login form component', async () => {
const mindsServiceStore = new MindsServiceStore();
const wrapper = shallow(
<LoginScreen hopFeatures={true} mindsServiceStore={mindsServiceStore} />
<LoginScreen hopFeatures={true} />
);
// search login form
......@@ -46,14 +42,13 @@ describe('LoginScreen component', () => {
});
it('should shows forgot password component if the user press the button', async () => {
const mindsServiceStore = new MindsServiceStore();
const navigation = {
push: jest.fn()
};
const wrapper = shallow(
<LoginScreen navigation={navigation} hopFeatures={true} mindsServiceStore={mindsServiceStore} />
<LoginScreen navigation={navigation} hopFeatures={true} />
);
// search login form
......
......@@ -2,15 +2,11 @@ import React, {
Component
} from 'react';
import { StackActions, NavigationActions } from 'react-navigation';
import FastImage from 'react-native-fast-image';
import {
StyleSheet,
ScrollView,
View,
KeyboardAvoidingView,
Button,
Keyboard,
Animated,
Platform,
......@@ -20,11 +16,9 @@ import * as Animatable from 'react-native-animatable';
import LoginForm from './LoginForm';
import VideoBackground from '../common/components/VideoBackground';
import { CommonStyle } from '../styles/Common';
import { ComponentsStyle } from '../styles/Components';
import logService from '../common/services/log.service';
import featuresService from '../common/services/features.service';
import CenteredLoading from '../common/components/CenteredLoading';
import { inject } from 'mobx-react/native';
const LOGO_HEIGHT = 100;
const LOGO_HEIGHT_SMALL = 50;
......@@ -32,7 +26,6 @@ const LOGO_HEIGHT_SMALL = 50;
/**
* Login screen
*/
@inject('mindsServiceStore')
export default class LoginScreen extends Component {
state = {
......@@ -54,7 +47,7 @@ export default class LoginScreen extends Component {
async componentDidMount() {
if (!this.props.hopFeatures) {
await featuresService.updateFeatures(this.props.mindsServiceStore);
await featuresService.updateFeatures();
if (featuresService.has('homepage-december-2019')) {
this.props.navigation.navigate('LoginNew');
}
......
......@@ -13,7 +13,6 @@ import i18n from './i18n.service';
* Feature service
*/
class FeaturesService {
/**
* Local config features are available without inicialization
*/
......@@ -24,17 +23,15 @@ class FeaturesService {
* Update features from minds services
*/
@action
async updateFeatures(mindsServiceStore = null) {
async updateFeatures() {
let settings;
if (mindsServiceStore) {
settings = await mindsServiceStore.getSettings();
} else {
settings = await mindsService.getSettings();
}
settings = await mindsService.getSettings();
const features = settings.features;
Object.assign(features, MINDS_FEATURES);
this.features = features;
this.loaded = true;
}
......@@ -44,7 +41,7 @@ class FeaturesService {
const features = this.features;
Object.assign(features, otherFeatures);
this.features = features;
}
......
import api from './api.service';
import { Alert } from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
import i18n from './i18n.service';
import featuresService from './features.service';
/**
* Minds Service
*/
class MindsService {
settings;
promise;
/**
* Lazy load default settings
*/
loadDefault = () => require("../../../settings/default.json");
loadDefault = () => require('../../../settings/default.json');
/**
* Update the settings from the server
*/
async update() {
const settings = await api.get('api/v1/minds/config');
await AsyncStorage.setItem('@MindsSettings', JSON.stringify(settings));
this.settings = settings;
// update the features based on the settings
featuresService.updateFeatures();
}
/**
* Get settings
*/
getSettings() {
if (!this.promise || this.settings) {
this.promise = this._getSettings();
}
return this.promise;
}
/**
* Get settings
*/
async getSettings() {
async _getSettings() {
let settings;
if (!this.settings) {
try {
settings = JSON.parse(await AsyncStorage.getItem('@MindsSettings'));
if (!settings) throw Error('No settings stored');
if (!settings) {
throw Error('No settings stored');
}
} catch {
settings = this.loadDefault();
await AsyncStorage.setItem('@MindsSettings', JSON.stringify(settings));
}
this.settings = settings;
await this.update();
// update the features based on the settings
featuresService.updateFeatures();
this.update();
}
return this.settings;
......
import mindsService from "../services/minds.service";
export default class MindsServiceStore {
promise;
async getSettings() {
if (!this.promise) {
this.promise = mindsService.getSettings();
}
return this.promise;
}
clear() {
this.promise = null;
}
}
......@@ -19,7 +19,6 @@ import apiService from '../common/services/api.service';
/**
* Tos Modal
*/
@inject('mindsServiceStore')
@observer
export default class TosModal extends PureComponent {
......@@ -34,7 +33,7 @@ export default class TosModal extends PureComponent {
*/
async componentDidMount() {
try {
const settings = await this.props.mindsServiceStore.getSettings();
const settings = await mindsService.getSettings();
if (settings.last_accepted_tos) {
this.setState({last_accepted_tos: settings.last_accepted_tos});
}
......
Please register or to comment