Attachment.js 2.4 KB
import React,{PropTypes} from 'react';  
import moment from 'moment';
import cx from 'classnames'; 
import s from './Attachment.scss';  
import accessoryUtil from '../../utils/accessoryUtil';
import fileUtil from '../../utils/fileUtil';

export class Attachment extends React.Component { 
	constructor(props) {
        super(props);
		this.downloadFile = this.downloadFile.bind(this);
		this.onRemove = this.onRemove.bind(this);
        this.state={
        }
    }
    static propTypes = { 
		attachment:PropTypes.object,
		onDelete: PropTypes.func,
		index: PropTypes.number,
		readOnly:PropTypes.bool	
	} 

	componentDidMount(){
	}

    componentWillReceiveProps(nextProps){
         
    }
	componentWillUnmount() {
		 
	} 
	
	onRemove(){
		const {index, onDelete, attachment} = this.props;
		const self = this;
		console.log(index);
		if(attachment.status!='active'){
					
			// fileUtil.deleteFile(attachment.file_id).
			// 	then(res=>{ 
			// 		console.log(res);
			// 	}).
			// 	catch(function(error){
			// 		console.log(error);
			// 	});
		}	
		onDelete(index);
	}

	downloadFile(){
		const {attachment} = this.props;
		console.log(attachment);

		const name=attachment.name;
		const path = attachment.path+name;
		fileUtil.downloadFile(attachment.bucket, path, name,attachment.file_id);
	}
	renderIcon(){
		const {attachment:{type=''}}=this.props;
		if(type=='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
			return <i className={cx('kr_icon',s.file_icon)}>&#xe64d;</i>;
		else if(type=='application/msword')
			return <i className={cx('kr_icon',s.file_icon)}>&#xe64c;</i>;
		else
			return <i className={cx('kr_icon',s.file_icon)}>&#xe64a;</i>;
	}
    render(){ 
    	const {attachment, readOnly}=this.props, self=this; 
    	return(
			 		<div className={cx(s.file_wrap)}>
								<div className={cx(s.actions)}>
									<a onClick={this.downloadFile} className={cx('kr_icon','ts_icon','ts_icon_share',s.download)}>&#xe659;</a>
									{!readOnly&&<a className={cx('kr_icon','ts_icon','ts_icon_share',s.download)} onClick={this.onRemove}>
										&#xe612;</a>}
								</div>
								{this.renderIcon()} 
			 					<div className={cx(s.contents)}>
			 						<span className={cx(s.tiem)}>{moment(attachment.created_at).format('M月D日 H:mmA')}</span>
			 						<div className={cx(s.title)}>{attachment.name}</div>
			 						<span className={cx(s.type)}>{attachment.type}</span>
			 					</div> 
			 			</div>
			);
	}
} 

export default Attachment