2018-04-12 16:23:26 -04:00
|
|
|
import React from 'react';
|
|
|
|
|
2018-04-14 21:11:40 -04:00
|
|
|
import { DocumentListItem } from './DocumentListItem';
|
2018-04-25 16:42:08 -04:00
|
|
|
import { ItemClickEvent } from './types/ItemClickEvent';
|
2018-04-14 21:11:40 -04:00
|
|
|
import { MediaGridItem } from './MediaGridItem';
|
2018-11-14 10:47:19 -08:00
|
|
|
import { MediaItemType } from '../../LightboxGallery';
|
2018-04-13 17:54:53 -04:00
|
|
|
import { missingCaseError } from '../../../util/missingCaseError';
|
2019-01-14 13:49:58 -08:00
|
|
|
import { LocalizerType } from '../../../types/Util';
|
2018-04-12 16:23:26 -04:00
|
|
|
|
|
|
|
interface Props {
|
2019-01-14 13:49:58 -08:00
|
|
|
i18n: LocalizerType;
|
2018-04-12 16:23:26 -04:00
|
|
|
header?: string;
|
2018-11-14 10:47:19 -08:00
|
|
|
type: 'media' | 'documents';
|
|
|
|
mediaItems: Array<MediaItemType>;
|
2018-04-15 02:16:39 -04:00
|
|
|
onItemClick?: (event: ItemClickEvent) => void;
|
2018-04-12 16:23:26 -04:00
|
|
|
}
|
|
|
|
|
2018-05-22 12:31:43 -07:00
|
|
|
export class AttachmentSection extends React.Component<Props> {
|
2018-04-15 02:53:19 -04:00
|
|
|
public render() {
|
|
|
|
const { header } = this.props;
|
|
|
|
|
|
|
|
return (
|
2018-07-17 17:15:34 -07:00
|
|
|
<div className="module-attachment-section">
|
|
|
|
<h2 className="module-attachment-section__header">{header}</h2>
|
|
|
|
<div className="module-attachment-section__items">
|
|
|
|
{this.renderItems()}
|
|
|
|
</div>
|
2018-04-15 02:53:19 -04:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-15 02:16:39 -04:00
|
|
|
private renderItems() {
|
2018-11-14 10:47:19 -08:00
|
|
|
const { i18n, mediaItems, type } = this.props;
|
2018-04-12 20:56:05 -04:00
|
|
|
|
2018-11-14 10:47:19 -08:00
|
|
|
return mediaItems.map((mediaItem, position, array) => {
|
|
|
|
const shouldShowSeparator = position < array.length - 1;
|
|
|
|
const { message, index, attachment } = mediaItem;
|
2018-04-12 20:56:05 -04:00
|
|
|
|
2018-11-14 10:47:19 -08:00
|
|
|
const onClick = this.createClickHandler(mediaItem);
|
2018-04-12 20:56:05 -04:00
|
|
|
switch (type) {
|
|
|
|
case 'media':
|
2018-04-15 02:16:39 -04:00
|
|
|
return (
|
|
|
|
<MediaGridItem
|
2018-11-14 10:47:19 -08:00
|
|
|
key={`${message.id}-${index}`}
|
|
|
|
mediaItem={mediaItem}
|
2018-04-15 02:16:39 -04:00
|
|
|
onClick={onClick}
|
2018-07-17 17:15:34 -07:00
|
|
|
i18n={i18n}
|
2018-04-15 02:16:39 -04:00
|
|
|
/>
|
|
|
|
);
|
2018-04-12 20:56:05 -04:00
|
|
|
case 'documents':
|
|
|
|
return (
|
2018-04-14 21:11:40 -04:00
|
|
|
<DocumentListItem
|
2018-11-14 10:47:19 -08:00
|
|
|
key={`${message.id}-${index}`}
|
|
|
|
fileName={attachment.fileName}
|
|
|
|
fileSize={attachment.size}
|
2018-05-08 15:56:10 -04:00
|
|
|
shouldShowSeparator={shouldShowSeparator}
|
2018-04-15 02:16:39 -04:00
|
|
|
onClick={onClick}
|
2018-05-07 21:43:55 -04:00
|
|
|
timestamp={message.received_at}
|
2018-04-12 20:56:05 -04:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
return missingCaseError(type);
|
|
|
|
}
|
|
|
|
});
|
2018-04-12 16:23:26 -04:00
|
|
|
}
|
|
|
|
|
2019-01-14 13:49:58 -08:00
|
|
|
private readonly createClickHandler = (mediaItem: MediaItemType) => () => {
|
2018-04-25 16:42:08 -04:00
|
|
|
const { onItemClick, type } = this.props;
|
2018-11-14 10:47:19 -08:00
|
|
|
const { message, attachment } = mediaItem;
|
|
|
|
|
2018-04-15 02:16:39 -04:00
|
|
|
if (!onItemClick) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-14 10:47:19 -08:00
|
|
|
onItemClick({ type, message, attachment });
|
2018-04-15 02:16:39 -04:00
|
|
|
};
|
2018-04-12 16:23:26 -04:00
|
|
|
}
|