summaryrefslogtreecommitdiff
path: root/client/components/App.jsx
blob: 4f9c0f23d053d841b8f9290213c9a6e04b690c12 (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
import { h, Component } from 'preact'
import { isMobile } from '../util'
import db from '../db'
import { Link, withRouter } from 'react-router-dom'

import Header from './Header.jsx'
import Paintings from './Paintings.jsx'
import Details from './Details.jsx'
import Modal from './Modal.jsx'

class App extends Component {
  constructor(props) {
    super()
    this.state = {
      data: db.backupDB,
      painting: null,
    }
    db.fetch( data => {
      document.body.parentNode.classList.remove('loading')
      this.setState({ data }) 
    })
  }
  setIdFromLocation(props){
    const id = props.location.pathname.split('/')[2]
    if (id) {
      this.setState({ painting: id })
    }
  }
  componentWillMount(){
    this.setIdFromLocation(this.props)
  }
  componentWillReceiveProps(props){
    this.setIdFromLocation(props)
  }
  render() {
    let painting;
    this.state.data.painting.some( (el) => {
      if (el.id == this.state.painting) {
        painting = el
        return true
      }
      return false
    })
    return (
      <div>
        <Header location={this.props.location} />
        <Paintings data={this.state.data} />
        <Modal visible={this.props.location.pathname !== '/paintings/'}>
          <Details painting={painting} />
        </Modal>
      </div>
    )
  }
}

export default withRouter(App)