Permalink action

This commit is contained in:
Diego Mello 2017-11-16 15:49:49 -02:00
parent ff97888aa9
commit b5067d3526
6 changed files with 99 additions and 8 deletions

View File

@ -39,7 +39,10 @@ export const MESSAGES = createRequestTypes('MESSAGES', [
'EDIT_FAILURE', 'EDIT_FAILURE',
'STAR_REQUEST', 'STAR_REQUEST',
'STAR_SUCCESS', 'STAR_SUCCESS',
'STAR_FAILURE' 'STAR_FAILURE',
'PERMALINK_REQUEST',
'PERMALINK_SUCCESS',
'PERMALINK_FAILURE'
]); ]);
export const CREATE_CHANNEL = createRequestTypes('CREATE_CHANNEL', [ export const CREATE_CHANNEL = createRequestTypes('CREATE_CHANNEL', [
...defaultTypes, ...defaultTypes,

View File

@ -84,3 +84,24 @@ export function starFailure() {
type: types.MESSAGES.STAR_FAILURE type: types.MESSAGES.STAR_FAILURE
}; };
} }
export function permalinkRequest(message) {
return {
type: types.MESSAGES.PERMALINK_REQUEST,
message
};
}
export function permalinkSuccess(permalink) {
return {
type: types.MESSAGES.PERMALINK_SUCCESS,
permalink
};
}
export function permalinkFailure(err) {
return {
type: types.MESSAGES.PERMALINK_FAILURE,
err
};
}

View File

@ -9,7 +9,7 @@ import { connect } from 'react-redux';
import Card from './Card'; import Card from './Card';
import User from './User'; import User from './User';
import Avatar from '../Avatar'; import Avatar from '../Avatar';
import { deleteRequest, editInit, starRequest } from '../../actions/messages'; import { deleteRequest, editInit, starRequest, permalinkRequest } from '../../actions/messages';
const title = 'Message actions'; const title = 'Message actions';
const options = ['Cancel', 'Reply', 'Edit', 'Permalink', 'Copy', 'Quote', 'Star Message', 'Delete']; const options = ['Cancel', 'Reply', 'Edit', 'Permalink', 'Copy', 'Quote', 'Star Message', 'Delete'];
@ -38,11 +38,13 @@ const styles = StyleSheet.create({
}); });
@connect(state => ({ @connect(state => ({
message: state.messages.message message: state.messages.message,
permalink: state.messages.permalink
}), dispatch => ({ }), dispatch => ({
deleteRequest: message => dispatch(deleteRequest(message)), deleteRequest: message => dispatch(deleteRequest(message)),
editInit: message => dispatch(editInit(message)), editInit: message => dispatch(editInit(message)),
starRequest: message => dispatch(starRequest(message)) starRequest: message => dispatch(starRequest(message)),
permalinkRequest: message => dispatch(permalinkRequest(message))
})) }))
export default class Message extends React.Component { export default class Message extends React.Component {
static propTypes = { static propTypes = {
@ -52,15 +54,28 @@ export default class Message extends React.Component {
deleteRequest: PropTypes.func.isRequired, deleteRequest: PropTypes.func.isRequired,
editInit: PropTypes.func.isRequired, editInit: PropTypes.func.isRequired,
starRequest: PropTypes.func.isRequired, starRequest: PropTypes.func.isRequired,
message: PropTypes.object permalinkRequest: PropTypes.func.isRequired,
message: PropTypes.object,
permalink: PropTypes.string
} }
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { copyPermalink: false };
this.handleActionPress = this.handleActionPress.bind(this); this.handleActionPress = this.handleActionPress.bind(this);
this.showActions = this.showActions.bind(this); this.showActions = this.showActions.bind(this);
} }
async componentWillReceiveProps(props) {
if (props.permalink) {
if (this.state.copyPermalink) {
this.setState({ copyPermalink: false });
await Clipboard.setString(props.permalink);
Alert.alert('Permalink copied to clipboard!');
}
}
}
isDeleted() { isDeleted() {
return !this.props.item.msg; return !this.props.item.msg;
} }
@ -103,18 +118,25 @@ export default class Message extends React.Component {
handleCopy = async() => { handleCopy = async() => {
await Clipboard.setString(this.props.item.msg); await Clipboard.setString(this.props.item.msg);
Alert.alert('Copied to Clipboard!'); Alert.alert('Copied to clipboard!');
} }
handleStar() { handleStar() {
this.props.starRequest(this.props.item); this.props.starRequest(this.props.item);
} }
handlePermalink() {
this.setState({ copyPermalink: true });
this.props.permalinkRequest(this.props.item);
}
handleActionPress = (actionIndex) => { handleActionPress = (actionIndex) => {
if (actionIndex === 7) { if (actionIndex === 7) {
this.handleDelete(); this.handleDelete();
} else if (actionIndex === 2) { } else if (actionIndex === 2) {
this.handleEdit(); this.handleEdit();
} else if (actionIndex === 3) {
this.handlePermalink();
} else if (actionIndex === 4) { } else if (actionIndex === 4) {
this.handleCopy(); this.handleCopy();
} else if (actionIndex === 6) { } else if (actionIndex === 6) {

View File

@ -464,6 +464,32 @@ const RocketChat = {
}, },
starMessage(message) { starMessage(message) {
return call('starMessage', message); return call('starMessage', message);
},
getPermalink(message) {
return new Promise((resolve, reject) => {
const result = realm.objects('subscriptions').filtered('rid = $0', message.rid);
if (result.length === 0) {
return reject(new Error('Room not found'));
}
const room = result[0];
let roomType;
switch (room.t) {
case 'p':
roomType = 'group';
break;
case 'c':
roomType = 'channel';
break;
case 'd':
roomType = 'direct';
break;
default:
break;
}
return resolve(`${ room._server.id }/${ roomType }/${ room.name }?msg=${ message._id }`);
});
} }
}; };

View File

@ -4,7 +4,8 @@ const initialState = {
isFetching: false, isFetching: false,
failure: false, failure: false,
message: {}, message: {},
editing: false editing: false,
permalink: ''
}; };
export default function messages(state = initialState, action) { export default function messages(state = initialState, action) {
@ -38,6 +39,11 @@ export default function messages(state = initialState, action) {
message: {}, message: {},
editing: false editing: false
}; };
case types.MESSAGES.PERMALINK_SUCCESS:
return {
...state,
permalink: action.permalink
};
default: default:
return state; return state;
} }

View File

@ -8,13 +8,16 @@ import {
editSuccess, editSuccess,
editFailure, editFailure,
starSuccess, starSuccess,
starFailure starFailure,
permalinkSuccess,
permalinkFailure
} from '../actions/messages'; } from '../actions/messages';
import RocketChat from '../lib/rocketchat'; import RocketChat from '../lib/rocketchat';
const deleteMessage = message => RocketChat.deleteMessage(message); const deleteMessage = message => RocketChat.deleteMessage(message);
const editMessage = message => RocketChat.editMessage(message); const editMessage = message => RocketChat.editMessage(message);
const starMessage = message => RocketChat.starMessage(message); const starMessage = message => RocketChat.starMessage(message);
const getPermalink = message => RocketChat.getPermalink(message);
const get = function* get({ rid }) { const get = function* get({ rid }) {
const auth = yield select(state => state.login.isAuthenticated); const auth = yield select(state => state.login.isAuthenticated);
@ -58,10 +61,20 @@ const handleStarRequest = function* handleStarRequest({ message }) {
} }
}; };
const handlePermalinkRequest = function* handlePermalinkRequest({ message }) {
try {
const permalink = yield call(getPermalink, message);
yield put(permalinkSuccess(permalink));
} catch (error) {
yield put(permalinkFailure(error));
}
};
const root = function* root() { const root = function* root() {
yield takeLatest(MESSAGES.REQUEST, get); yield takeLatest(MESSAGES.REQUEST, get);
yield takeLatest(MESSAGES.DELETE_REQUEST, handleDeleteRequest); yield takeLatest(MESSAGES.DELETE_REQUEST, handleDeleteRequest);
yield takeLatest(MESSAGES.EDIT_REQUEST, handleEditRequest); yield takeLatest(MESSAGES.EDIT_REQUEST, handleEditRequest);
yield takeLatest(MESSAGES.STAR_REQUEST, handleStarRequest); yield takeLatest(MESSAGES.STAR_REQUEST, handleStarRequest);
yield takeLatest(MESSAGES.PERMALINK_REQUEST, handlePermalinkRequest);
}; };
export default root; export default root;