Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react'
  2. import { Form, Button, Container, Header } from 'semantic-ui-react'
  3. import TopMenu from '../components/TopMenu'
  4.  
  5. import { makeFunctionSafe } from '../../shared/safe-function'
  6. import { getAllZones } from '../../service/zones'
  7. import { createUser } from '../../service/user'
  8. import MessageModalManager from '../components/message-modals/message-modal-manager'
  9.  
  10. /**
  11.  * Component that represents the form to create users.
  12.  * @extends Component
  13.  */
  14. export class CreateUserForm extends Component {
  15.  
  16.   state = {
  17.     username: '',
  18.     password: '',
  19.     zone: '',
  20.     rwUsername: ''
  21.   }
  22.  
  23.   onSubmit = e => {
  24.     e.preventDefault()
  25.     this.props.onSubmit(this.state)
  26.   }
  27.  
  28.   render() {
  29.     const {zones} = this.props
  30.     const {username, password, zone, rwUsername} = this.state
  31.     return (
  32.       <Form>
  33.         <Form.Input
  34.           label="Username"
  35.           placeholder="Username"
  36.           value={username}
  37.           onChange={e => this.setState({username: e.target.value})} />
  38.        
  39.         <Form.Input
  40.           label="Password"
  41.           placeholder="Password"
  42.           type="password"
  43.           value={password}
  44.           onChange={e => this.setState({password: e.target.value})} />
  45.  
  46.         <Form.Dropdown
  47.           label="Zona"
  48.           placeholder='Zona'
  49.           fluid selection
  50.           options={zones}
  51.           onChange={(e, data) => this.setState({zone: data.value})} />
  52.        
  53.         <Form.Input
  54.           label="Username no Rentway"
  55.           placeholder="Username no Rentway"
  56.           value={rwUsername}
  57.           onChange={e => this.setState({rwUsername: e.target.value})} />
  58.  
  59.         <Button
  60.           type="submit"
  61.           floated="right"
  62.           positive
  63.           disabled={(!username || !password || !zone || !rwUsername)}
  64.           onClick={this.onSubmit}>Criar</Button>
  65.       </Form>
  66.     )
  67.   }
  68. }
  69.  
  70. /**
  71.  * Component that represents the page to create users.
  72.  * @extends Component
  73.  */
  74. class CreateUserPage extends Component {
  75.  
  76.   state = {zones: []}
  77.  
  78.   componentDidMount() {
  79.     makeFunctionSafe(async () => {
  80.       const zonesIds = await getAllZones()
  81.       const zones = zonesIds.map(id => ({text: id, value: id}))
  82.       this.setState({zones})
  83.     })()
  84.   }
  85.  
  86.   onSubmit = makeFunctionSafe(async user => {
  87.     const data = await createUser(user)
  88.     MessageModalManager.openModal(data)
  89.   })
  90.  
  91.   render() {
  92.     return (
  93.       <div>
  94.         <TopMenu activeItem='users' />
  95.         <Container>
  96.           <Header as="h2" content="Criar novo utilizador"/>
  97.           <CreateUserForm zones={this.state.zones} onSubmit={this.onSubmit}/>
  98.         </Container>
  99.       </div>
  100.     )
  101.   }
  102. }
  103.  
  104. export default CreateUserPage
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement