Guest User

Untitled

a guest
Jul 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. /* @flow */
  2.  
  3. import React from 'react'
  4.  
  5. import isEqual from 'lodash/isEqual'
  6. import validator from 'is-my-json-valid'
  7. import Flatpickr from 'react-flatpickr'
  8. import Select from 'react-select'
  9. import moment from 'moment'
  10.  
  11. const debounce = require('throttle-debounce/debounce')
  12.  
  13. type ReactEvent = { preventDefault: Function, target: { value: ?string } }
  14.  
  15. type Educ = {
  16. id: ?string,
  17. uid: ?string,
  18. school: ?string,
  19. degree: ?string,
  20. fieldOfStudy: ?string,
  21. startDate: ?string,
  22. endDate: ?string
  23. }
  24.  
  25. type Props = {
  26. educ: Educ,
  27. onSave: Function,
  28. onRemove: Function
  29. }
  30.  
  31. type State = {
  32. attributes: Educ
  33. }
  34.  
  35. class EducationForm extends React.Component<Props, State> {
  36. flatOptions = {
  37. altInput: true,
  38. altFormat: 'd/m/Y'
  39. }
  40. options = Array(moment().year() - 1929).fill()
  41. .map((_, index) => {
  42. const value = moment().year() - index
  43. return {
  44. value: value,
  45. label: value
  46. }
  47. })
  48. validate = validator({
  49. required: true,
  50. type: 'object',
  51. properties: {
  52. id: {
  53. type: 'string'
  54. },
  55. uid: {
  56. type: 'string'
  57. },
  58. school: {
  59. type: 'string',
  60. required: true,
  61. minLength: 1
  62. },
  63. degree: {
  64. type: 'string',
  65. required: true,
  66. minLength: 1
  67. },
  68. fieldOfStudy: {
  69. type: 'string',
  70. required: true,
  71. minLength: 1
  72. },
  73. startDate: {
  74. type: 'string',
  75. required: true,
  76. minLength: 1
  77. },
  78. endDate: {
  79. type: 'string',
  80. required: true,
  81. minLength: 1
  82. }
  83. }
  84. })
  85.  
  86. constructor (props: Props) {
  87. super(props)
  88.  
  89. this.submitForm = debounce(1000, this.submitForm)
  90. this.state = {
  91. attributes: props.educ
  92. }
  93. this.state.attributes.startDate = ''
  94. this.state.attributes.endDate = ''
  95. }
  96.  
  97. componentWillUpdate (nextProps: Props) {
  98. if (!isEqual(this.props.educ, nextProps.educ)) {
  99. this.setState({
  100. attributes: nextProps.educ
  101. })
  102. }
  103. }
  104.  
  105. updateAttribute = (e: ReactEvent, attribute: string) => {
  106. e.preventDefault && e.preventDefault()
  107.  
  108. const attributes = {
  109. ...this.state.attributes,
  110. [attribute]: e.target ? e.target.value : e
  111. }
  112. this.setState({ attributes: attributes }, () => this.submitForm())
  113. }
  114.  
  115. onRemove = (e: ReactEvent) => {
  116. e.preventDefault()
  117. this.props.onRemove(this.state.attributes)
  118. }
  119.  
  120. onSave = _ => {
  121. this.validate(this.state.attributes) &&
  122. this.props.onSave(this.state.attributes)
  123. }
  124.  
  125. formValid = () => {
  126. return (
  127. this.validate(this.state.attributes) &&
  128. !isEqual(this.props.educ, this.state.attributes)
  129. )
  130. }
  131.  
  132. submitForm = _ => {
  133. if (this.formValid()) {
  134. this.onSave()
  135. }
  136. }
  137.  
  138. render () {
  139. const attributes = this.state.attributes
  140.  
  141. return (
  142. <form
  143. key={attributes.id || attributes.uid}
  144. className='form'
  145. onSubmit={this.onSave}
  146. >
  147. <div className='row'>
  148. <div className='col-sm-11'>
  149. <div className='form-group'>
  150. <input
  151. type='text'
  152. className='form-control'
  153. name='school'
  154. value={attributes.school}
  155. required='required'
  156. placeholder='University'
  157. onChange={e => {
  158. this.updateAttribute(e, 'school')
  159. }}
  160. />
  161. </div>
  162. <div className='form-group'>
  163. <input
  164. type='text'
  165. className='form-control'
  166. name='degree'
  167. value={attributes.degree}
  168. required='required'
  169. placeholder='Degree'
  170. onChange={e => {
  171. this.updateAttribute(e, 'degree')
  172. }}
  173. />
  174. </div>
  175. <div className='form-group'>
  176. <input
  177. type='text'
  178. className='form-control'
  179. name='fieldOfStudy'
  180. value={attributes.fieldOfStudy}
  181. required='required'
  182. placeholder='Field of Study'
  183. onChange={e => {
  184. this.updateAttribute(e, 'fieldOfStudy')
  185. }}
  186. />
  187. </div>
  188. <div className='row'>
  189. <div className='col-sm-6'>
  190. <div className='form-group'>
  191. <label>Start Date</label>
  192. <Select
  193. className='form-control'
  194. name='startDate'
  195. value={attributes.startDate}
  196. required={true}
  197. onChange={e => {
  198. this.updateAttribute(e, 'startDate')
  199. }}
  200. options={this.options}
  201. />
  202. </div>
  203. </div>
  204. <div className='col-sm-6'>
  205. <div className='form-group'>
  206. <label>End Date</label>
  207. <Select
  208. className='form-control'
  209. name='endDate'
  210. value={attributes.endDate}
  211. required={true}
  212. onChange={e => {
  213. this.updateAttribute(e, 'endDate')
  214. }}
  215. options={this.options}
  216. />
  217. </div>
  218. </div>
  219. </div>
  220. </div>
  221. <div className='col-sm-1'>
  222. <button
  223. href='#'
  224. className='btn btn-minim -danger pl-0'
  225. onClick={this.onRemove}
  226. >
  227. <i className='nc-icon-glyph ui-1_circle-delete' />
  228. Remove
  229. </button>
  230. </div>
  231. </div>
  232. </form>
  233. )
  234. }
  235. }
  236.  
  237. export default EducationForm
Add Comment
Please, Sign In to add comment