Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Webviews for Articles. #4

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ const AdminPanelStack = createStackNavigator({
defaultNavigationOptions: defaultHeader
});

const ArticlesStack = createStackNavigator({
ArticlesView: {
getScreen: () => require('./views/ArticlesView').default
}
}, {
defaultNavigationOptions: defaultHeader
});

SettingsStack.navigationOptions = ({ navigation }) => {
let drawerLockMode = 'unlocked';
if (navigation.state.index > 0) {
Expand All @@ -185,7 +193,8 @@ const ChatsDrawer = createDrawerNavigator({
ChatsStack,
ProfileStack,
SettingsStack,
AdminPanelStack
AdminPanelStack,
ArticlesStack
}, {
contentComponent: Sidebar
});
Expand Down
8 changes: 8 additions & 0 deletions app/lib/rocketchat.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,14 @@ const RocketChat = {
// RC 0.55.0
return this.sdk.post('users.resetAvatar', { userId });
},
redirectToUserArticles(userId) {
// Method added in https://github.com/WideChat/Rocket.Chat/pull/65
return this.sdk.methodCall('redirectToUsersArticles', userId);
},
redirectUserToArticles(token) {
// Method added in https://github.com/WideChat/Rocket.Chat/pull/63
return this.sdk.methodCall('redirectUserToArticles', token);
},
setAvatarFromService({ data, contentType = '', service = null }) {
// RC 0.51.0
return this.sdk.methodCall('setAvatarFromService', data, contentType, service);
Expand Down
90 changes: 90 additions & 0 deletions app/views/ArticlesView/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import React from 'react';
import PropTypes from 'prop-types';
import { WebView } from 'react-native-webview';
import {
BackHandler,
Platform
} from 'react-native';
import { SafeAreaView } from 'react-navigation';
import { connect } from 'react-redux';
// import CookieManager from 'react-native-cookies';
import styles from '../Styles';
// import I18n from '../../i18n';
import StatusBar from '../../containers/StatusBar';
import { DrawerButton } from '../../containers/HeaderButton';

@connect(state => ({
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
userId: state.login.user && state.login.user.id,
authToken: state.login.user && state.login.user.token
}))
export default class AdminPanelView extends React.Component {
static navigationOptions = ({ navigation }) => ({
headerLeft: <DrawerButton navigation={navigation} />,
title: 'Articles'
});

static propTypes = {
baseUrl: PropTypes.string,
// userId: PropTypes.string,
authToken: PropTypes.string,
navigation: PropTypes.object
}

webView = {
canGoBack: false,
ref: null
}
// [TODO] Make a back button for iOS

componentWillMount() {
if (Platform.OS === 'android') {
BackHandler.addEventListener('hardwareBackPress', this.onAndroidBackPress);
}
}

componentWillUnmount() {
if (Platform.OS === 'android') {
BackHandler.removeEventListener('hardwareBackPress');
}
}

onAndroidBackPress = () => {
if (this.webView.canGoBack && this.webView.ref) {
this.webView.ref.goBack();
return true;
}
return false;
}

render() {
const {
navigation, authToken, baseUrl
} = this.props;
this.articlesLink = navigation.getParam('articlesLink');
if (!this.articlesLink) {
return null;
}
// CookieManager.get(baseUrl)
// .then((res) => {
// console.warn('CookieManager.get =>', res);
// /* [TODO]
// * Based on whether baseUrl has cookies required for the articlesLink,
// * in this case, rc_uid and rc_token,
// * change injectedJavaScript such that
// * no-login appears if cookies are already present.
// */
// });
return (
<SafeAreaView style={styles.container} testID='articles-view'>
<StatusBar />
<WebView
source={{ uri: `${ baseUrl }/admin/info?layout=embedded` }}
ref={(webView) => { this.webView.ref = webView; }}
injectedJavaScript={`Meteor.loginWithToken('${ authToken }', function() { }); setTimeout(function(){ window.location.assign('${ this.articlesLink }')}, 50)`}
onNavigationStateChange={(navState) => { this.webView.canGoBack = navState.canGoBack; }}
/>
</SafeAreaView>
);
}
}
28 changes: 24 additions & 4 deletions app/views/ProfileView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default class ProfileView extends React.Component {
})

static propTypes = {
navigation: PropTypes.object,
baseUrl: PropTypes.string,
user: PropTypes.object,
Accounts_CustomFields: PropTypes.string,
Expand All @@ -67,7 +68,8 @@ export default class ProfileView extends React.Component {
avatarUrl: null,
avatar: {},
avatarSuggestions: {},
customFields: {}
customFields: {},
articlesLink: ''
}

async componentDidMount() {
Expand Down Expand Up @@ -102,11 +104,12 @@ export default class ProfileView extends React.Component {
this.setState({ avatar });
}

init = (user) => {
init = async(user) => {
const { user: userProps } = this.props;
const {
name, username, emails, customFields
name, username, emails, customFields, token
} = user || userProps;
const result = await RocketChat.redirectUserToArticles(token);

this.setState({
name,
Expand All @@ -116,7 +119,8 @@ export default class ProfileView extends React.Component {
currentPassword: null,
avatarUrl: null,
avatar: {},
customFields: customFields || {}
customFields: customFields || {},
articlesLink: result ? result.link : ''
});
}

Expand Down Expand Up @@ -280,6 +284,14 @@ export default class ProfileView extends React.Component {
</Touch>
)

renderArticlesWebView = () => {
const { navigation } = this.props;
const { articlesLink } = this.state;
console.warn('link is', articlesLink);
navigation.navigate('ArticlesView', { articlesLink });
}


renderAvatarButtons = () => {
const { avatarUrl, avatarSuggestions } = this.state;
const { user, baseUrl } = this.props;
Expand Down Expand Up @@ -404,6 +416,14 @@ export default class ProfileView extends React.Component {
token={user.token}
/>
</View>
<View>
<Button
title='My Articles'
type='primary'
onPress={this.renderArticlesWebView}
testID='render-articles-button'
/>
</View>
<RCTextInput
inputRef={(e) => { this.name = e; }}
label={I18n.t('Name')}
Expand Down
34 changes: 33 additions & 1 deletion app/views/RoomInfoView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import I18n from '../../i18n';
import { CustomHeaderButtons, Item } from '../../containers/HeaderButton';
import StatusBar from '../../containers/StatusBar';
import log from '../../utils/log';
import Button from '../../containers/Button';

const PERMISSION_EDIT_ROOM = 'edit-room';

Expand Down Expand Up @@ -76,7 +77,8 @@ export default class RoomInfoView extends React.Component {
};
this.state = {
room: this.rooms[0] || room || {},
roomUser: {}
roomUser: {},
articlesLink: ''
};
}

Expand All @@ -100,6 +102,12 @@ export default class RoomInfoView extends React.Component {
} catch (error) {
log('err_get_user_info', error);
}
try {
const result = await RocketChat.redirectToUserArticles(roomUserId);
this.setState({ articlesLink: result.link });
} catch (error) {
console.warn('error in fetching link ', error);
}
}
}

Expand Down Expand Up @@ -202,6 +210,29 @@ export default class RoomInfoView extends React.Component {
);
}

renderArticlesWebView = () => {
const { navigation } = this.props;
const { articlesLink } = this.state;
console.warn('link is', articlesLink);
navigation.navigate('ArticlesView', { articlesLink });
}

renderArticlesButton = () => {
const { roomUser } = this.state;
console.warn(roomUser);
const title = `Articles By ${ roomUser.name }`;
return (
<View>
<Button
title={title}
type='primary'
onPress={this.renderArticlesWebView}
testID='render-articles-button'
/>
</View>
);
}

renderBroadcast = () => (
<View style={styles.item}>
<Text style={styles.itemLabel}>{I18n.t('Broadcast_Channel')}</Text>
Expand Down Expand Up @@ -255,6 +286,7 @@ export default class RoomInfoView extends React.Component {
{!this.isDirect() ? this.renderItem('description', room) : null}
{!this.isDirect() ? this.renderItem('topic', room) : null}
{!this.isDirect() ? this.renderItem('announcement', room) : null}
{this.isDirect() ? this.renderArticlesButton() : null}
{this.isDirect() ? this.renderRoles() : null}
{this.isDirect() ? this.renderTimezone() : null}
{this.isDirect() ? this.renderCustomFields(roomUser._id) : null}
Expand Down