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 ROOMS = createRequestTypes('ROOMS');
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 INCREMENT = 'INCREMENT';

View File

@ -18,3 +18,11 @@ export function connectFailure(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 {
type: types.LOGIN.SUCCESS
// token,
// user
type: types.LOGIN.SUCCESS,
token
};
}
@ -23,6 +23,7 @@ export function loginFailure(err) {
}
export function logout() {
console.log('LOGOUT');
return {
type: types.LOGOUT
};

View File

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

View File

@ -15,12 +15,13 @@ const styles = StyleSheet.create({
@connect(state => ({
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 {
render() {
const { connecting, authenticating } = this.props;
const { connecting, authenticating, offline } = this.props;
if (connecting) {
return (
<View style={[styles.bannerContainer, { backgroundColor: '#0d0' }]}>
@ -36,6 +37,13 @@ export default class Banner extends React.PureComponent {
</View>
);
}
if (offline) {
return (
<View style={[styles.bannerContainer, { backgroundColor: 'red' }]}>
<Text style={[styles.bannerText, { color: '#a00' }]}>offline...</Text>
</View>
);
}
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({
schema: [settingsSchema, serversSchema, subscriptionSchema, messagesSchema, usersSchema, attachment]

View File

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

View File

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

View File

@ -31,6 +31,7 @@ export default function login(state = initialState, action) {
errorMessage: action.err
};
case types.LOGOUT:
console.log('LOGOUT');
return initialState;
default:
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 RocketChat from '../lib/rocketchat';
import { connectSuccess, connectFailure } from '../actions/connect';
import { connectSuccess, connectRequest, connectFailure } from '../actions/connect';
function 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() {
while (true) {
yield take(METEOR.REQUEST);
console.log('\n\n[METEOR CONNECTED]\n\n');
try {
const response = yield call(connect);
yield put(connectSuccess(response));
yield takeLatest(METEOR.REQUEST, test);
} catch (err) {
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 { 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 { loginSuccess, loginFailure } from '../actions/login';
import { loginSuccess, loginFailure, logout } from '../actions/login';
import RocketChat from '../lib/rocketchat';
const getUser = state => state.login;
function loginCall(args) {
return RocketChat.loginWithPassword(args);
}
const watchLoginRequest = function* watchLoginRequest() {
const auto = function* auto() {
while (true) {
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);
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 user = yield select(getUser);
if (user.token) {
RocketChat.login({ resume: user.token });
}
}
};
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;

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 });
class RoomsListItem extends React.PureComponent {
static propTypes = {