summaryrefslogtreecommitdiff
path: root/lib/awprint/client/components/App.jsx
blob: 4c4f9bea402ae756a76e8c19f45b8d44a32f3fcd (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import React from 'react'

import Modal from './Modal.jsx'

const MAX_JOB_SIZE = 120

export default class App extends React.Component {
  constructor(props) {
    super()
    this.state = {
      jobs: [],
      modalVisible: false,
    }
    this.load()
    this.socket = io()
    this.socket.on('job', (job) => this.prepend(job))
  }
  
  load() {
    fetch('/_services/awprint/index')
    .then( res => {
      return res.json()
    }).then( jobs => {
      this.setState({ jobs })
    }).catch( e => {
      console.error(e)
    })
  }
  
  prepend(job) {
    let jobs = this.state.jobs
    if (this.state.jobs.length > MAX_JOB_SIZE) this.state.jobs.pop()
    jobs.unshift(job)
    console.log(job)
    this.setState({ jobs })
  }
  
  print(job) {
    job.printed = true
    this.setState({ modalVisible: true })

    // first request updates database
    fetch('/_services/awprint/print', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        _id: job._id,
      })
    })
    .then( res => {
      setTimeout( () => {
        this.setState({ modalVisible: false })
      }, 1500)
    })
  /*
    // second request actually prints it?
    fetch('/_services/awprint/print', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        url: job.url,
      })
    })
    .then( res => {
      this.setState({ modalVisible: false })
    })
  */
  }
  render() {
    const jobs = this.state.jobs.map( (job,i) => {
      const className = job.printed ? 'print printed' : 'print'
      const datePartz = job.date.split('T')
      const timePartz = datePartz[1].split(':')
      const hour = Number(timePartz[0]) - 4
      const mins = timePartz[1]
      const date = datePartz[0] + ' - ' + hour + ':' + mins
      return (
        <div key={i} className={className} onClick={() => this.print(job)}>
          <img src={job.url} />
          <div className='date'>{date}</div>
        </div>
      )
    })
    return (
      <div>
        {jobs}
        <Modal visible={this.state.modalVisible}>
          <img src='public/spinner.svg' />
          <div>PRINTING YOUR PICTURE</div>
        </Modal>
      </div>
    )
  }
}