summaryrefslogtreecommitdiff
path: root/animism-align/frontend/views/index/components/graph.form.js
blob: d90b663c273c9f42f848114e6ee51ce308102fed (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import React, { Component } from 'react'
import { Link } from 'react-router-dom'

import { session } from '../../../session'

import { TextInput, LabelDescription, TextArea, Checkbox, SubmitButton, Loader } from '../../../common'

const newGraph = () => ({
  path: '',
  title: '',
  username: session('username'),
  description: '',
})

export default class GraphForm extends Component {
  state = {
    title: "",
    submitTitle: "",
    data: { ...newGraph() },
    errorFields: new Set([]),
  }

  componentDidMount() {
    const { data, isNew } = this.props
    const title = isNew ? 'new project' : 'editing ' + data.title
    const submitTitle = isNew ? "Create Graph" : "Save Changes"
    this.setState({
      title,
      submitTitle,
      errorFields: new Set([]),
      data: { 
        ...newGraph(),
        ...data
      },
    })
  }

  handleChange(e) {
    const { errorFields } = this.state
    const { name, value } = e.target
    if (errorFields.has(name)) {
      errorFields.delete(name)
    }
    let sanitizedValue = value
    if (name === 'path') {
      sanitizedValue = sanitizedValue.toLowerCase().replace(/ /, '-').replace(/[!@#$%^&*()[\]{}]/, '-').replace(/-+/, '-')
    }
    this.setState({
      errorFields,
      data: {
        ...this.state.data,
        [name]: sanitizedValue,
      }
    })
  }

  handleSelect(name, value) {
    const { errorFields } = this.state
    if (errorFields.has(name)) {
      errorFields.delete(name)
    }
    this.setState({
      errorFields,
      data: {
        ...this.state.data,
        [name]: value,
      }
    })
  }

  handleSubmit(e) {
    e.preventDefault()
    const { isNew, onSubmit } = this.props
    const { data } = this.state
    const requiredKeys = "title username path description".split(" ")
    const validKeys = "title username path description".split(" ")
    const validData = validKeys.reduce((a,b) => { a[b] = data[b]; return a }, {})
    const errorFields = requiredKeys.filter(key => !validData[key])
    if (errorFields.length) {
      console.log('error', errorFields, validData)
      this.setState({ errorFields: new Set(errorFields) })
    } else {
      if (isNew) {
        // side effect: set username if we're creating a new graph
        session.set('username', data.username)
      } else {
        validData.id = data.id
      }
      console.log('submit', validData)
      onSubmit(validData)
    }
  }

  render() {
    const { isNew } = this.props
    const { title, submitTitle, errorFields, data } = this.state
    return (
      <div className='form'>
        <h1>{title}</h1>
        <form onSubmit={this.handleSubmit.bind(this)}>
          <TextInput
            title="Path"
            name="path"
            required
            data={data}
            error={errorFields.has('path')}
            onChange={this.handleChange.bind(this)}
            autoComplete="off"
          />
          <LabelDescription>
            {data.path
              ? 'Project URLs will be: /' + data.path + '/example'
              : 'Enter the base path for this project.'}
          </LabelDescription>
          <TextInput
            title="Title"
            name="title"
            required
            data={data}
            error={errorFields.has('title')}
            onChange={this.handleChange.bind(this)}
            autoComplete="off"
          />
          <TextInput
            title="Author"
            name="username"
            required
            data={data}
            error={errorFields.has('username')}
            onChange={this.handleChange.bind(this)}
            autoComplete="off"
          />
          <TextArea
            title="Description"
            name="description"
            data={data}
            onChange={this.handleChange.bind(this)}
          />
          <SubmitButton
            title={submitTitle}
            onClick={this.handleSubmit.bind(this)}
          />
          {!!errorFields.size &&
            <label>
              <span></span>
              <span>Please complete the required fields =)</span>
            </label>
          }
        </form>
      </div>
    )
  }
}