Skip to content

Commit

Permalink
Merge branch 'post-metadata'
Browse files Browse the repository at this point in the history
  • Loading branch information
hmhealey committed Nov 23, 2018
2 parents 7dcafa9 + fa4abeb commit b6196ad
Show file tree
Hide file tree
Showing 31 changed files with 471 additions and 404 deletions.
35 changes: 20 additions & 15 deletions actions/post_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,11 @@ export function increasePostVisibility(channelId, focusedPostId) {
return true;
}

dispatch(batchActions([
{
type: ActionTypes.LOADING_POSTS,
data: true,
channelId,
},
{
type: ActionTypes.INCREASE_POST_VISIBILITY,
data: channelId,
amount: POST_INCREASE_AMOUNT,
},
]));
dispatch({
type: ActionTypes.LOADING_POSTS,
data: true,
channelId,
});

const page = Math.floor(currentPostVisibility / POST_INCREASE_AMOUNT);

Expand All @@ -198,13 +191,25 @@ export function increasePostVisibility(channelId, focusedPostId) {
}
const posts = result.data;

dispatch({
const actions = [{
type: ActionTypes.LOADING_POSTS,
data: false,
channelId,
});
}];

if (posts) {
actions.push({
type: ActionTypes.INCREASE_POST_VISIBILITY,
data: channelId,
amount: posts.order.length,
});
}

return posts ? posts.order.length >= POST_INCREASE_AMOUNT : false;
dispatch(batchActions(actions));
return {
moreToLoad: posts ? posts.order.length >= POST_INCREASE_AMOUNT : false,
error: result.error,
};
};
}

Expand Down
27 changes: 12 additions & 15 deletions actions/post_actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,43 +216,40 @@ describe('Actions.Posts', () => {

await testStore.dispatch(Actions.increasePostVisibility('current_channel_id'));
expect(testStore.getActions()).toEqual([
{channelId: 'current_channel_id', data: true, type: 'LOADING_POSTS'},
{args: ['current_channel_id', 2, 30], type: 'MOCK_GET_POSTS'},
{
meta: {batch: true},
payload: [
{channelId: 'current_channel_id', data: true, type: 'LOADING_POSTS'},
{amount: 30, data: 'current_channel_id', type: 'INCREASE_POST_VISIBILITY'},
{channelId: 'current_channel_id', data: false, type: 'LOADING_POSTS'},
],
type: 'BATCHING_REDUCER.BATCH',
},
{args: ['current_channel_id', 2, 30], type: 'MOCK_GET_POSTS'},
{channelId: 'current_channel_id', data: false, type: 'LOADING_POSTS'},
]);

await testStore.dispatch(Actions.increasePostVisibility('current_channel_id', 'latest_post_id'));
expect(testStore.getActions()).toEqual([
{channelId: 'current_channel_id', data: true, type: 'LOADING_POSTS'},
{args: ['current_channel_id', 2, 30], type: 'MOCK_GET_POSTS'},
{
meta: {batch: true},
payload: [
{channelId: 'current_channel_id', data: true, type: 'LOADING_POSTS'},
{amount: 30, data: 'current_channel_id', type: 'INCREASE_POST_VISIBILITY'},
{channelId: 'current_channel_id', data: false, type: 'LOADING_POSTS'},
],
type: 'BATCHING_REDUCER.BATCH',
},
{args: ['current_channel_id', 2, 30], type: 'MOCK_GET_POSTS'},
{channelId: 'current_channel_id', data: false, type: 'LOADING_POSTS'},
{channelId: 'current_channel_id', data: true, type: 'LOADING_POSTS'},
{
args: ['current_channel_id', 'latest_post_id', 2, 30],
type: 'MOCK_GET_POSTS_BEFORE',
},
{
meta: {batch: true},
payload: [
{channelId: 'current_channel_id', data: true, type: 'LOADING_POSTS'},
{amount: 30, data: 'current_channel_id', type: 'INCREASE_POST_VISIBILITY'},
{channelId: 'current_channel_id', data: false, type: 'LOADING_POSTS'},
],
type: 'BATCHING_REDUCER.BATCH',
},
{
args: ['current_channel_id', 'latest_post_id', 2, 30],
type: 'MOCK_GET_POSTS_BEFORE',
},
{channelId: 'current_channel_id', data: false, type: 'LOADING_POSTS'},
]);
});

Expand Down
13 changes: 13 additions & 0 deletions components/__snapshots__/markdown_image.test.jsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`components/MarkdownImage should match snapshot 1`] = `
<img
onLoad={[Function]}
src="https://something.com/image.png"
style={
Object {
"height": "200",
}
}
/>
`;
14 changes: 0 additions & 14 deletions components/file_attachment_list/file_attachment_list.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ export default class FileAttachmentList extends React.Component {
isEmbedVisible: PropTypes.bool,

locale: PropTypes.string.isRequired,

actions: PropTypes.shape({

/*
* Function to get file metadata for a post
*/
getMissingFilesForPost: PropTypes.func.isRequired,
}).isRequired,
}

constructor(props) {
Expand All @@ -57,12 +49,6 @@ export default class FileAttachmentList extends React.Component {
this.state = {showPreviewModal: false, startImgIndex: 0};
}

componentDidMount() {
if (this.props.post.file_ids || this.props.post.filenames) {
this.props.actions.getMissingFilesForPost(this.props.post.id);
}
}

handleImageClick(indexClicked) {
this.setState({showPreviewModal: true, startImgIndex: indexClicked});
}
Expand Down
12 changes: 1 addition & 11 deletions components/file_attachment_list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// See LICENSE.txt for license information.

import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {getMissingFilesForPost} from 'mattermost-redux/actions/files';
import {makeGetFilesForPost} from 'mattermost-redux/selectors/entities/files';

import {getCurrentLocale} from 'selectors/i18n';
Expand Down Expand Up @@ -34,12 +32,4 @@ function makeMapStateToProps() {
};
}

function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators({
getMissingFilesForPost,
}, dispatch),
};
}

export default connect(makeMapStateToProps, mapDispatchToProps)(FileAttachmentList);
export default connect(makeMapStateToProps)(FileAttachmentList);
7 changes: 7 additions & 0 deletions components/markdown/markdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,18 @@ export default class Markdown extends React.PureComponent {
* Any extra props that should be passed into the MarkdownImage component
*/
imageProps: PropTypes.object,

/**
* prop for passed down to MarkdownImage component for dimensions
*/
imagesMetadata: PropTypes.object,
};

static defaultProps = {
options: {},
isRHS: false,
proxyImages: true,
imagesMetadata: {},
};

render() {
Expand All @@ -96,6 +102,7 @@ export default class Markdown extends React.PureComponent {
const htmlFormattedText = TextFormatting.formatText(this.props.message, options);
return messageHtmlToComponent(htmlFormattedText, this.props.isRHS, {
imageProps: this.props.imageProps,
imagesMetadata: this.props.imagesMetadata,
});
}
}
1 change: 1 addition & 0 deletions components/markdown/markdown.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('components/Markdown', () => {
siteURL: 'https://markdown.example.com',
team: {name: 'yourteamhere'},
hasImageProxy: false,
metadata: {},
};

test('should render properly', () => {
Expand Down
64 changes: 9 additions & 55 deletions components/markdown_image.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,42 @@
import PropTypes from 'prop-types';
import React from 'react';

const WAIT_FOR_HEIGHT_TIMEOUT = 100;

export default class MarkdownImage extends React.PureComponent {
static propTypes = {

/*
* The href of the image to be loaded
* dimensions object to create empty space required to prevent scroll pop
*/
href: PropTypes.string,
dimensions: PropTypes.object,

/*
* A callback that is called as soon as the image component has a height value
*/
onHeightReceived: PropTypes.func,
}

constructor(props) {
super(props);

this.heightTimeout = 0;
}

componentDidMount() {
this.waitForHeight();
}

componentDidUpdate(prevProps) {
if (this.props.href !== prevProps.href) {
this.waitForHeight();
}
}

componentWillUnmount() {
this.stopWaitingForHeight();
}

waitForHeight = () => {
if (this.refs.image.height) {
if (this.props.onHeightReceived) {
this.props.onHeightReceived(this.refs.image.height);
}

this.heightTimeout = 0;
} else {
this.heightTimeout = setTimeout(this.waitForHeight, WAIT_FOR_HEIGHT_TIMEOUT);
}
}

stopWaitingForHeight = () => {
if (this.heightTimeout !== 0) {
clearTimeout(this.heightTimeout);
this.heightTimeout = 0;

return true;
}

return false;
}

handleLoad = () => {
const wasWaiting = this.stopWaitingForHeight();
// image is loaded but still havent recived new post webscoket event for metadata
// so meanwhile correct manually

// The image loaded before we caught its layout event, so we still need to notify that its height changed
if (wasWaiting && this.props.onHeightReceived) {
if (!this.props.dimensions && this.props.onHeightReceived) {
this.props.onHeightReceived(this.refs.image.height);
}
};

handleError = () => {
this.stopWaitingForHeight();
};

render() {
const props = {...this.props};
Reflect.deleteProperty(props, 'onHeightReceived');
Reflect.deleteProperty(props, 'dimensions');

return (
<img
ref='image'
{...props}
onLoad={this.handleLoad}
onError={this.handleError}
style={{
height: this.props.dimensions ? this.props.dimensions.height : 'initial',
}}
/>
);
}
Expand Down
43 changes: 43 additions & 0 deletions components/markdown_image.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import React from 'react';
import {shallow} from 'enzyme';

import MarkdownImage from 'components/markdown_image';

describe('components/MarkdownImage', () => {
test('should match snapshot', () => {
const wrapper = shallow(
<MarkdownImage
src={'https://something.com/image.png'}
dimensions={{
width: '100',
height: '200',
}}
onHeightReceived={jest.fn()}
/>
);

expect(wrapper).toMatchSnapshot();
});

test('should call onHeightReceived', () => {
const onHeightReceived = jest.fn();
const wrapper = shallow(
<MarkdownImage
src={'https://something.com/image.png'}
onHeightReceived={onHeightReceived}
/>
);
const instance = wrapper.instance();

instance.refs = {
image: {
height: 100,
},
};
wrapper.find('img').prop('onLoad')();
expect(onHeightReceived).toHaveBeenCalledWith(100);
});
});
1 change: 1 addition & 0 deletions components/post_markdown/post_markdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default class PostMarkdown extends React.PureComponent {
proxyImages={proxyImages}
options={this.props.options}
channelNamesMap={channelNamesMap}
imagesMetadata={this.props.post.metadata && this.props.post.metadata.images}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@ export default class CommentedOnFilesMessage extends React.PureComponent {
* An array of file metadata for the parent post
*/
fileInfos: PropTypes.arrayOf(PropTypes.object),

actions: PropTypes.shape({

/*
* Function to get file metadata for a post
*/
getFilesForPost: PropTypes.func.isRequired,
}).isRequired,
}

componentDidMount() {
if (!this.props.fileInfos || this.props.fileInfos.length === 0) {
this.props.actions.getFilesForPost(this.props.parentPostId);
}
}

render() {
Expand Down
Loading

0 comments on commit b6196ad

Please sign in to comment.