return async (dispatch, getState) => {
        const state = getState();

        const {ExperimentalPrimaryTeam} = getConfig(state);
        const {teams: allTeams, myMembers} = state.entities.teams;
        const teams = Object.keys(myMembers).map((key) => allTeams[key]);

        let defaultTeam = selectFirstAvailableTeam(teams, ExperimentalPrimaryTeam);

        if (defaultTeam) {
            dispatch(handleTeamChange(defaultTeam.id));
        } else if (state.requests.teams.getTeams.status === RequestStatus.FAILURE || state.requests.teams.getMyTeams.status === RequestStatus.FAILURE) {
            EventEmitter.emit(NavigationTypes.NAVIGATION_ERROR_TEAMS);
        } else {
            // If for some reason we reached this point cause of a failure in rehydration or something
            // lets fetch the teams one more time to make sure the user does not belong to any team
            const {data, error} = await dispatch(getMyTeams());
            if (error) {
                EventEmitter.emit(NavigationTypes.NAVIGATION_ERROR_TEAMS);
                return;
            }

            if (data) {
                defaultTeam = selectFirstAvailableTeam(data, ExperimentalPrimaryTeam);
            }

            if (defaultTeam) {
                dispatch(handleTeamChange(defaultTeam.id));
            } else {
                EventEmitter.emit(NavigationTypes.NAVIGATION_NO_TEAMS);
            }
        }
    };
Example #2
0
const onPushNotification = async (deviceNotification) => {
    const {dispatch, getState} = store;
    let unsubscribeFromStore = null;
    let stopLoadingNotification = false;

    // mark the app as started as soon as possible
    if (Platform.OS === 'android' && !app.appStarted) {
        app.setStartAppFromPushNotification(true);
    }

    const {data, foreground, message, userInfo, userInteraction} = deviceNotification;
    const notification = {
        data,
        message,
    };

    if (userInfo) {
        notification.localNotification = userInfo.localNotification;
    }

    if (data.type === 'clear') {
        dispatch(markChannelAsRead(data.channel_id, null, false));
    } else {
        // get the posts for the channel as soon as possible
        retryGetPostsAction(getPosts(data.channel_id), dispatch, getState);

        if (foreground) {
            EventEmitter.emit(ViewTypes.NOTIFICATION_IN_APP, notification);
        } else if (userInteraction && !notification.localNotification) {
            EventEmitter.emit('close_channel_drawer');
            if (getState().views.root.hydrationComplete) {
                setTimeout(() => {
                    loadFromNotification(notification);
                }, 0);
            } else {
                const waitForHydration = () => {
                    if (getState().views.root.hydrationComplete && !stopLoadingNotification) {
                        stopLoadingNotification = true;
                        unsubscribeFromStore();
                        loadFromNotification(notification);
                    }
                };

                unsubscribeFromStore = store.subscribe(waitForHydration);
            }
        }
    }
};
    selectChannel = (channel, currentChannelId, closeDrawer = true) => {
        const {
            actions,
        } = this.props;

        const {
            setChannelLoading,
            setChannelDisplayName,
        } = actions;

        tracker.channelSwitch = Date.now();

        if (closeDrawer) {
            this.closeChannelDrawer();
            setChannelLoading(channel.id !== currentChannelId);
        }

        if (!channel) {
            const utils = require('app/utils/general');
            const {intl} = this.context;

            const unableToJoinMessage = {
                id: t('mobile.open_unknown_channel.error'),
                defaultMessage: "We couldn't join the channel. Please reset the cache and try again.",
            };
            const erroMessage = {};

            utils.alertErrorWithFallback(intl, erroMessage, unableToJoinMessage);
            setChannelLoading(false);
            return;
        }

        setChannelDisplayName(channel.display_name);
        EventEmitter.emit('switch_channel', channel, currentChannelId);
    };
Example #4
0
const loadFromNotification = async (notification) => {
    await store.dispatch(loadFromPushNotification(notification));
    if (!app.startAppFromPushNotification) {
        EventEmitter.emit(ViewTypes.NOTIFICATION_TAPPED);
        PushNotifications.resetNotification();
    }
};
Example #5
0
    componentDidUpdate(prevProps) {
        if (tracker.teamSwitch) {
            this.props.actions.recordLoadTime('Switch Team', 'teamSwitch');
        }

        // When the team changes emit the event to render the drawer content
        if (this.props.currentChannelId && !prevProps.currentChannelId) {
            EventEmitter.emit('renderDrawer');
        }
    }
Example #6
0
    componentDidMount() {
        if (tracker.initialLoad) {
            this.props.actions.recordLoadTime('Start time', 'initialLoad');
        }

        if (this.props.showTermsOfService && !this.props.disableTermsModal) {
            this.showTermsOfServiceModal();
        }

        EventEmitter.emit('renderDrawer');
    }
Example #7
0
    startApp = () => {
        if (this.appStarted || this.waitForRehydration) {
            return;
        }

        const {dispatch} = store;

        Linking.getInitialURL().then((url) => {
            dispatch(setDeepLinkURL(url));
        });

        let screen = 'SelectServer';
        if (this.token && this.url) {
            screen = 'Channel';
            tracker.initialLoad = Date.now();

            try {
                dispatch(loadMe());
            } catch (e) {
                // Fall through since we should have a previous version of the current user because we have a token
                console.warn('Failed to load current user when starting on Channel screen', e); // eslint-disable-line no-console
            }
        }

        switch (screen) {
        case 'SelectServer':
            EventEmitter.emit(ViewTypes.LAUNCH_LOGIN, true);
            break;
        case 'Channel':
            EventEmitter.emit(ViewTypes.LAUNCH_CHANNEL, true);
            break;
        }

        this.setStartAppFromPushNotification(false);
        this.setAppStarted(true);
    }
Example #8
0
    startConversation = async (selectedId) => {
        const {
            currentDisplayName,
            actions,
        } = this.props;

        if (this.state.startingConversation) {
            return;
        }

        this.setState({
            startingConversation: true,
        });

        // Save the current channel display name in case it fails
        const currentChannelDisplayName = currentDisplayName;

        const selectedIds = selectedId ? Object.keys(selectedId) : Object.keys(this.state.selectedIds);
        let success;
        if (selectedIds.length === 0) {
            success = false;
        } else if (selectedIds.length > 1) {
            success = await this.makeGroupChannel(selectedIds);
        } else {
            success = await this.makeDirectChannel(selectedIds[0]);
        }

        if (success) {
            EventEmitter.emit('close_channel_drawer');
            requestAnimationFrame(() => {
                this.close();
            });
        } else {
            this.setState({
                startingConversation: false,
            });

            actions.setChannelDisplayName(currentChannelDisplayName);
        }
    };
Example #9
0
 action: () => {
     EventEmitter.emit(NavigationTypes.NAVIGATION_CLOSE_MODAL);
     this.onRemovePost(this.props.post);
 },
Example #10
0
                action: () => {
                    const {failed, id, ...post} = this.props.post; // eslint-disable-line

                    EventEmitter.emit(NavigationTypes.NAVIGATION_CLOSE_MODAL);
                    this.props.actions.createPost(post);
                },