summaryrefslogtreecommitdiff
path: root/app/client/dataset/dataset.component.js
blob: c64a51ff48118dedf3671ba1bbabb4d567fc5e4d (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
101
102
import { h, Component } from 'preact'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import * as datasetActions from './dataset.actions'

import Group from '../common/group.component'
import Param from '../common/param.component'
import FileList from '../common/fileList.component'
import FileUpload from '../common/fileUpload.component'
import TextInput from '../common/textInput.component'

class Dataset extends Component {
  constructor(props){
    super()
  }
  render(){
    const {
      loading, status, error,
      module, title, folder, files,
      canRename, canUpload, canAddURL, canDeleteFile,
      linkFiles,
      fileOptions, pickFile, onPick
    } = this.props
    // sort files??
    if (!folder.id) {
      return (
        <div className='dataset'>
          <div className='params row'>
            <div className='column'>
              <Group title={title || 'Dataset'}>
                {canRename
                ? this.renderFolderNameInput(folder.name)
                : <Param title='Dataset name'>{folder.name}</Param>}
              </Group>
            </div>
          </div>
        </div>
      )
    }
    return (
      <div className='dataset'>
        <div className='params row'>
          <div className='column'>
            <Group title={title || 'Dataset'}>
              {canRename
              ? this.renderFolderNameInput(folder.name)
              : <Param title='Dataset name'>{folder.name}</Param>}
              {folder.id && canUpload && this.renderUploadInput()}
              {folder.id && canAddURL && this.renderURLInput()}
            </Group>
          </div>
        </div>
        <div className='params col'>
          <FileList
            title='Files'
            files={files}
            options={fileOptions}
            onClick={() => onPick && onPick(file)}
            canDelete={canDeleteFile}
            linkFiles={linkFiles}
          />
        </div>
      </div>
    )
  }
  curry(action) {
    const { module, folder } = this.props
    return (param) => action(module, folder, param)
  }
  renderFolderNameInput(name){
    return <TextInput
      title={!this.props.folder.id ? 'Create a new dataset' : 'Dataset name'}
      value={name}
      onSave={this.curry(this.props.actions.dataset.createOrUpdateFolder)}
    />
  }
  renderUploadInput(){
    return <FileUpload
      title='Upload a file'
      mime='image.*'
      onUpload={this.curry(this.props.actions.dataset.uploadFile)}
    />
  }
  renderURLInput(){
    return <TextInput
      title='Fetch a URL'
      placeholder='http://'
      onSave={this.curry(this.props.actions.dataset.handleURL)}
    />
  }
}

const mapStateToProps = state => state.dataset

const mapDispatchToProps = (dispatch, ownProps) => ({
  actions: {
    dataset: bindActionCreators(datasetActions, dispatch),
  }
})

export default connect(mapStateToProps, mapDispatchToProps)(Dataset)