summaryrefslogtreecommitdiff
path: root/animism-align/frontend/views/upload/components/upload.show.js
blob: 6b36269902b870c01ecac01a0c6a965e8c62adc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import { connect } from 'react-redux'

import actions from '../../../actions'
import { formatDate, formatTime, formatAge, uploadUri } from '../../../util'
import { history } from '../../../store'
import { Loader, MenuButton } from '../../../common'

class UploadShow extends Component {
  componentDidMount() {
    actions.upload.show(this.props.match.params.id)
  }

  componentDidUpdate(prevProps) {
    if (prevProps.match.params.id !== this.props.match.params.id) {
      actions.upload.show(this.props.match.params.id)
    }
  }

  handleDestroy() {
    const { res: data } = this.props.upload.show
    if (confirm("Really delete this upload?")) {
      actions.upload.destroy(data).then(() => {
        history.push('/upload/')
      })
    }
  }

  render() {
    const { show, destroy } = this.props.upload
    if (show.loading || destroy.loading) {
      return <Loader />
    }
    if (!show.loading && !show.res || show.not_found) {
      return <div className='gray'>Upload {this.props.match.params.id} not found</div>
    }
    const { res: data } = show
    return (
      <section className="row uploadShow">
        <div className="menuButtons">
          <MenuButton name="delete" onClick={this.handleDestroy.bind(this)} />
          <MenuButton name="search" href={'/search/upload/' + data.id + '/'} />
        </div>
        <div>
          <img src={uploadUri(data)} />
          <div className='byline'>
            {'Uploaded by '}
            {data.username}
            {' on '}
            {formatDate(data.created_at)}
            {' at '}
            {formatTime(data.created_at)}
            {'. '}
          </div>
        </div>
      </section>
    )
  }
}

const mapStateToProps = state => ({
  upload: state.upload,
})

const mapDispatchToProps = dispatch => ({
  // searchActions: bindActionCreators({ ...searchActions }, dispatch),
})

export default connect(mapStateToProps, mapDispatchToProps)(UploadShow)