2018-04-12 20:23:26 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
2018-04-25 20:42:08 +00:00
|
|
|
import { AttachmentType } from './types/AttachmentType';
|
2018-04-15 01:11:40 +00:00
|
|
|
import { DocumentListItem } from './DocumentListItem';
|
2018-04-25 20:42:08 +00:00
|
|
|
import { ItemClickEvent } from './types/ItemClickEvent';
|
2018-04-15 01:11:40 +00:00
|
|
|
import { MediaGridItem } from './MediaGridItem';
|
2018-04-25 20:42:08 +00:00
|
|
|
import { Message } from './types/Message';
|
2018-04-13 21:54:53 +00:00
|
|
|
import { missingCaseError } from '../../../util/missingCaseError';
|
2018-04-13 00:56:05 +00:00
|
|
|
|
2018-04-12 20:23:26 +00:00
|
|
|
const styles = {
|
|
|
|
container: {
|
|
|
|
width: '100%',
|
|
|
|
},
|
2018-04-13 20:47:35 +00:00
|
|
|
header: {
|
|
|
|
fontSize: 14,
|
|
|
|
fontWeight: 'normal',
|
|
|
|
lineHeight: '28px',
|
|
|
|
} as React.CSSProperties,
|
2018-04-12 20:23:26 +00:00
|
|
|
itemContainer: {
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'row',
|
|
|
|
flexWrap: 'wrap',
|
|
|
|
justifyContent: 'flex-start',
|
|
|
|
alignItems: 'flex-start',
|
|
|
|
} as React.CSSProperties,
|
|
|
|
};
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
i18n: (value: string) => string;
|
|
|
|
header?: string;
|
2018-04-25 20:42:08 +00:00
|
|
|
type: AttachmentType;
|
2018-04-12 20:23:26 +00:00
|
|
|
messages: Array<Message>;
|
2018-04-15 06:16:39 +00:00
|
|
|
onItemClick?: (event: ItemClickEvent) => void;
|
2018-04-12 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 19:31:43 +00:00
|
|
|
export class AttachmentSection extends React.Component<Props> {
|
2018-04-15 06:53:19 +00:00
|
|
|
public render() {
|
|
|
|
const { header } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div style={styles.container}>
|
|
|
|
<h2 style={styles.header}>{header}</h2>
|
|
|
|
<div style={styles.itemContainer}>{this.renderItems()}</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-15 06:16:39 +00:00
|
|
|
private renderItems() {
|
2018-04-12 20:23:26 +00:00
|
|
|
const { i18n, messages, type } = this.props;
|
2018-04-13 00:56:05 +00:00
|
|
|
|
2018-05-08 01:43:55 +00:00
|
|
|
return messages.map((message, index, array) => {
|
2018-05-08 19:56:10 +00:00
|
|
|
const shouldShowSeparator = index < array.length - 1;
|
2018-04-13 00:56:05 +00:00
|
|
|
const { attachments } = message;
|
|
|
|
const firstAttachment = attachments[0];
|
|
|
|
|
2018-04-15 06:16:39 +00:00
|
|
|
const onClick = this.createClickHandler(message);
|
2018-04-13 00:56:05 +00:00
|
|
|
switch (type) {
|
|
|
|
case 'media':
|
2018-04-15 06:16:39 +00:00
|
|
|
return (
|
|
|
|
<MediaGridItem
|
2018-04-24 16:23:03 +00:00
|
|
|
key={message.id}
|
2018-04-15 06:16:39 +00:00
|
|
|
message={message}
|
|
|
|
onClick={onClick}
|
|
|
|
/>
|
|
|
|
);
|
2018-04-13 00:56:05 +00:00
|
|
|
case 'documents':
|
|
|
|
return (
|
2018-04-15 01:11:40 +00:00
|
|
|
<DocumentListItem
|
2018-04-24 16:23:03 +00:00
|
|
|
key={message.id}
|
2018-04-13 00:56:05 +00:00
|
|
|
fileName={firstAttachment.fileName}
|
2018-05-08 01:43:55 +00:00
|
|
|
fileSize={firstAttachment.size}
|
|
|
|
i18n={i18n}
|
2018-05-08 19:56:10 +00:00
|
|
|
shouldShowSeparator={shouldShowSeparator}
|
2018-04-15 06:16:39 +00:00
|
|
|
onClick={onClick}
|
2018-05-08 01:43:55 +00:00
|
|
|
timestamp={message.received_at}
|
2018-04-13 00:56:05 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
return missingCaseError(type);
|
|
|
|
}
|
|
|
|
});
|
2018-04-12 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-15 06:16:39 +00:00
|
|
|
private createClickHandler = (message: Message) => () => {
|
2018-04-25 20:42:08 +00:00
|
|
|
const { onItemClick, type } = this.props;
|
2018-04-15 06:16:39 +00:00
|
|
|
if (!onItemClick) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-25 20:42:08 +00:00
|
|
|
onItemClick({ type, message });
|
2018-04-15 06:16:39 +00:00
|
|
|
};
|
2018-04-12 20:23:26 +00:00
|
|
|
}
|