This commit is contained in:
Guilherme Gazzo 2017-08-17 13:55:47 -03:00
parent 566a22c389
commit ea63582676
No known key found for this signature in database
GPG Key ID: 1F85C9AD922D0829
15 changed files with 1268 additions and 89 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
ÖŠ<EFBFBD>tùþ춥Z'ŸFöà.â°'

View File

@ -13,7 +13,7 @@ function createRequestTypes(base, types = defaultTypes) {
export const LOGIN = createRequestTypes('LOGIN'); export const LOGIN = createRequestTypes('LOGIN');
export const ROOMS = createRequestTypes('ROOMS'); export const ROOMS = createRequestTypes('ROOMS');
export const MESSAGES = createRequestTypes('MESSAGES'); export const MESSAGES = createRequestTypes('MESSAGES');
export const METEOR = createRequestTypes('METEOR_CONNECT'); export const METEOR = createRequestTypes('METEOR_CONNECT', [...defaultTypes, 'DISCONNECT']);
export const LOGOUT = 'LOGOUT'; // logout is always success export const LOGOUT = 'LOGOUT'; // logout is always success
export const INCREMENT = 'INCREMENT'; export const INCREMENT = 'INCREMENT';

View File

@ -18,3 +18,11 @@ export function connectFailure(err) {
err err
}; };
} }
export function disconnect(err) {
console.log('types.METEOR.DISCONNECT');
return {
type: types.METEOR.DISCONNECT,
err
};
}

View File

@ -7,11 +7,11 @@ export function loginRequest(credentials) {
}; };
} }
export function loginSuccess(/* { token, user } */) { export function loginSuccess({ token = {} }) {
console.log('loginSuccess', token);
return { return {
type: types.LOGIN.SUCCESS type: types.LOGIN.SUCCESS,
// token, token
// user
}; };
} }
@ -23,6 +23,7 @@ export function loginFailure(err) {
} }
export function logout() { export function logout() {
console.log('LOGOUT');
return { return {
type: types.LOGOUT type: types.LOGOUT
}; };

View File

@ -38,19 +38,16 @@ export default class MessageBox extends React.PureComponent {
constructor(props) { constructor(props) {
super(props); super(props);
// this._textInput.setNativeProps({ text: '' });
this.state = {
text: ''
};
} }
submit(message) { submit(message) {
// console.log(this.state); // console.log(this.state);
const text = message; const text = message;
this.setState({ text: '' });
if (text.trim() === '') { if (text.trim() === '') {
return; return;
} }
this.component && this.component.setNativeProps({ text: '' });
this.props.onSubmit(text); this.props.onSubmit(text);
} }
@ -90,13 +87,14 @@ export default class MessageBox extends React.PureComponent {
<TextInput <TextInput
ref={component => this.component = component} ref={component => this.component = component}
style={styles.textBoxInput} style={styles.textBoxInput}
value={this.state.text} // value={this.state.text}
onChangeText={text => this.setState({ text })} // onChangeText={text => this.setState({ text })}
returnKeyType='send' returnKeyType='send'
onSubmitEditing={event => this.submit(event.nativeEvent.text)} onSubmitEditing={event => this.submit(event.nativeEvent.text)}
blurOnSubmit={false} blurOnSubmit={false}
placeholder='New message' placeholder='New message'
underlineColorAndroid='transparent' underlineColorAndroid='transparent'
defaultValue={''}
/> />
</View> </View>
); );

View File

@ -15,12 +15,13 @@ const styles = StyleSheet.create({
@connect(state => ({ @connect(state => ({
connecting: state.meteor && state.meteor.connecting, connecting: state.meteor && state.meteor.connecting,
authenticating: state.login && state.login.isFetching authenticating: state.login && state.login.isFetching,
offline: !state.meteor.connected
})) }))
export default class Banner extends React.PureComponent { export default class Banner extends React.PureComponent {
render() { render() {
const { connecting, authenticating } = this.props; const { connecting, authenticating, offline } = this.props;
if (connecting) { if (connecting) {
return ( return (
<View style={[styles.bannerContainer, { backgroundColor: '#0d0' }]}> <View style={[styles.bannerContainer, { backgroundColor: '#0d0' }]}>
@ -36,6 +37,13 @@ export default class Banner extends React.PureComponent {
</View> </View>
); );
} }
if (offline) {
return (
<View style={[styles.bannerContainer, { backgroundColor: 'red' }]}>
<Text style={[styles.bannerText, { color: '#a00' }]}>offline...</Text>
</View>
);
}
return null; return null;
} }
} }

0
app/lib/meteor-redux.js Normal file
View File

View File

@ -154,7 +154,7 @@ const messagesSchema = {
// } // }
}; };
Realm.clearTestState(); // Realm.clearTestState();
const realm = new Realm({ const realm = new Realm({
schema: [settingsSchema, serversSchema, subscriptionSchema, messagesSchema, usersSchema, attachment] schema: [settingsSchema, serversSchema, subscriptionSchema, messagesSchema, usersSchema, attachment]

View File

@ -8,7 +8,8 @@ import reduxStore from '../lib/createStore';
import settingsType from '../constants/settings'; import settingsType from '../constants/settings';
import realm from './realm'; import realm from './realm';
import * as actions from '../actions'; import * as actions from '../actions';
import { disconnect, connectSuccess } from '../actions/connect';
import { logout, loginSuccess } from '../actions/login';
export { Accounts } from 'react-native-meteor'; export { Accounts } from 'react-native-meteor';
@ -50,69 +51,83 @@ const RocketChat = {
}, },
connect(cb) { connect(cb) {
const url = `${ RocketChat.currentServer }/websocket`; return new Promise((resolve, reject) => {
const url = `${ RocketChat.currentServer }/websocket`;
Meteor.connect(url); Meteor.connect(url, { autoConnect: true, autoReconnect: true });
// , { autoConnect: false, autoReconnect: false }
Meteor.ddp.on('connected', () => { Meteor.ddp.on('disconnected', () => {
Meteor.call('public-settings/get', (err, data) => { console.log('disconnected');
if (err) { reduxStore.dispatch(disconnect());
console.error(err);
}
const settings = {};
realm.write(() => {
data.forEach((item) => {
const setting = {
_id: item._id
};
setting._server = { id: RocketChat.currentServer };
if (settingsType[item.type]) {
setting[settingsType[item.type]] = item.value;
realm.create('settings', setting, true);
}
settings[item._id] = item.value;
});
});
reduxStore.dispatch(actions.setAllSettings(settings));
if (typeof cb === 'function') {
cb();
}
}); });
Meteor.ddp.on('connected', (err) => {
!err && reduxStore.dispatch(connectSuccess());
!err && resolve();
});
Meteor.ddp.on('loggin', () => {
reduxStore.dispatch(loginSuccess({}));
});
Meteor.ddp.on('connected', () => {
Meteor.call('public-settings/get', (err, data) => {
if (err) {
console.error(err);
}
Meteor.ddp.on('changed', (ddbMessage) => { const settings = {};
// console.log('changed', ddbMessage);
if (ddbMessage.collection === 'stream-room-messages') {
realm.write(() => { realm.write(() => {
const message = ddbMessage.fields.args[0]; data.forEach((item) => {
message.temp = false; const setting = {
message._server = { id: RocketChat.currentServer }; _id: item._id
// write('messages', message); };
realm.create('messages', message, true); setting._server = { id: RocketChat.currentServer };
}); if (settingsType[item.type]) {
} setting[settingsType[item.type]] = item.value;
realm.create('settings', setting, true);
}
if (ddbMessage.collection === 'stream-notify-user') { settings[item._id] = item.value;
// console.log(ddbMessage); });
realm.write(() => {
const data = ddbMessage.fields.args[1];
data._server = { id: RocketChat.currentServer };
realm.create('subscriptions', data, true);
}); });
} reduxStore.dispatch(actions.setAllSettings(settings));
if (typeof cb === 'function') {
cb();
}
});
Meteor.ddp.on('changed', (ddbMessage) => {
// console.log('changed', ddbMessage);
if (ddbMessage.collection === 'stream-room-messages') {
realm.write(() => {
const message = ddbMessage.fields.args[0];
message.temp = false;
message._server = { id: RocketChat.currentServer };
// write('messages', message);
realm.create('messages', message, true);
});
}
if (ddbMessage.collection === 'stream-notify-user') {
// console.log(ddbMessage);
realm.write(() => {
const data = ddbMessage.fields.args[1];
data._server = { id: RocketChat.currentServer };
realm.create('subscriptions', data, true);
});
}
});
}); });
}); });
}, },
async login(params, callback) { login(params, callback) {
await new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
Meteor._startLoggingIn(); Meteor._startLoggingIn();
console.log('meteor login', params); console.log('meteor login', params);
return Meteor.call('login', params, (err, result) => { return Meteor.call('login', params, (err, result) => {
Meteor._endLoggingIn(); Meteor._endLoggingIn();
Meteor._handleLoginCallback(err, result); Meteor._handleLoginCallback(err, result);
console.log(result);
err ? reject(err) : resolve(result); err ? reject(err) : resolve(result);
if (typeof callback === 'function') { if (typeof callback === 'function') {
callback(err, result); callback(err, result);

View File

@ -26,6 +26,8 @@ export default function connect(state = initialState, action) {
failure: true, failure: true,
errorMessage: action.err errorMessage: action.err
}; };
case METEOR.DISCONNECT:
return initialState;
default: default:
return state; return state;
} }

View File

@ -31,6 +31,7 @@ export default function login(state = initialState, action) {
errorMessage: action.err errorMessage: action.err
}; };
case types.LOGOUT: case types.LOGOUT:
console.log('LOGOUT');
return initialState; return initialState;
default: default:
return state; return state;

View File

@ -1,23 +1,37 @@
import { take, put, call } from 'redux-saga/effects'; import { take, put, call, fork, takeLatest } from 'redux-saga/effects';
import { METEOR } from '../actions/actionsTypes'; import { METEOR } from '../actions/actionsTypes';
import RocketChat from '../lib/rocketchat'; import RocketChat from '../lib/rocketchat';
import { connectSuccess, connectFailure } from '../actions/connect'; import { connectSuccess, connectRequest, connectFailure } from '../actions/connect';
function connect(...args) { function connect(...args) {
return RocketChat.connect(...args); return RocketChat.connect(...args);
} }
const auto = function* auto() {
while (true) {
yield take(METEOR.DISCONNECT);
console.log('\n\n[METEOR DISCONNECT]\n\n');
yield put(connectRequest());
}
};
const test = function* test() {
const response = yield call(connect);
yield put(connectSuccess(response));
console.log('\n\n[METEOR CONNECTED]\n\n');
};
const watchConnect = function* watchConnect() { const watchConnect = function* watchConnect() {
while (true) { while (true) {
yield take(METEOR.REQUEST);
console.log('\n\n[METEOR CONNECTED]\n\n');
try { try {
const response = yield call(connect); yield takeLatest(METEOR.REQUEST, test);
yield put(connectSuccess(response));
} catch (err) { } catch (err) {
yield put(connectFailure(err.status)); yield put(connectFailure(err.status));
} }
yield take(METEOR.DISCONNECT);
console.log('\n\n[METEOR DISCONNECT]\n\n');
} }
}; };
export default watchConnect; const root = function* root() {
yield fork(watchConnect);
// yield fork(auto);
};
export default root;

View File

@ -1,26 +1,48 @@
import React from 'react'; import React from 'react';
import { take, put, call, takeLast } from 'redux-saga/effects'; import { take, put, call, takeLast, fork, select } from 'redux-saga/effects';
import * as types from '../actions/actionsTypes'; import * as types from '../actions/actionsTypes';
import { loginSuccess, loginFailure } from '../actions/login'; import { loginSuccess, loginFailure, logout } from '../actions/login';
import RocketChat from '../lib/rocketchat'; import RocketChat from '../lib/rocketchat';
const getUser = state => state.login;
function loginCall(args) { function loginCall(args) {
return RocketChat.loginWithPassword(args); return RocketChat.loginWithPassword(args);
} }
const auto = function* auto() {
const watchLoginRequest = function* watchLoginRequest() {
while (true) { while (true) {
yield take(types.METEOR.SUCCESS); yield take(types.METEOR.SUCCESS);
console.log('\n\n[LOGIN METEOR CONNECTED]\n\n'); const user = yield select(getUser);
const payload = yield take(types.LOGIN.REQUEST); if (user.token) {
try { RocketChat.login({ resume: user.token });
const response = yield call(loginCall, payload);
yield put(loginSuccess(response));
console.log('\n\n[LOGIN SUCCESS]\n\n');
} catch (err) {
console.log('\n\n[LOGIN FAILURE]\n\n', err);
yield put(loginFailure(err.status));
} }
} }
}; };
const watchLoginRequest = function* watchLoginRequest() {
while (true) {
try {
yield take(types.METEOR.SUCCESS);
console.log('\n\n[LOGIN METEOR CONNECTED]\n\n');
const payload = yield take(types.LOGIN.REQUEST);
try {
const response = yield call(loginCall, payload);
console.log(response);
yield put(loginSuccess(response));
console.log('\n\n[LOGIN SUCCESS]\n\n');
} catch (err) {
console.log('\n\n[LOGIN FAILURE]\n\n', err);
yield put(loginFailure(err.status));
}
yield take(types.METEOR.DISCONNECT);
console.log('\n\n[METEOR DISCONNECT LOGOUT]\n\n');
yield put(logout());
} catch (e) {
console.log(e);
}
}
};
const root = function* root() {
yield fork(watchLoginRequest);
yield fork(auto);
};
export default watchLoginRequest; export default watchLoginRequest;

View File

@ -57,10 +57,6 @@ const styles = StyleSheet.create({
} }
}); });
Meteor.Accounts.onLogin(() => {
console.log('onLogin');
});
const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 }); const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
class RoomsListItem extends React.PureComponent { class RoomsListItem extends React.PureComponent {
static propTypes = { static propTypes = {