[351c43f] | 1 | import React from 'react';
|
---|
[19ad843] | 2 | import 'semantic-ui-react';
|
---|
[7944fab] | 3 | import {Button, Container, Dropdown, Form, Label} from "semantic-ui-react";
|
---|
[351c43f] | 4 | import { Redirect } from 'react-router-dom';
|
---|
| 5 | import SkillFetch from "../../repository/skill_repo";
|
---|
| 6 | import UserRegister from "../../repository/register_repo";
|
---|
[19ad843] | 7 | import {Component} from "react/cjs/react.production.min";
|
---|
[df3a395] | 8 | import Login from "../login/login";
|
---|
[351c43f] | 9 |
|
---|
| 10 | class RegisterUser extends Component {
|
---|
| 11 | constructor(props) {
|
---|
| 12 | super(props);
|
---|
| 13 | this.state = {
|
---|
| 14 | email: "",
|
---|
| 15 | password: "",
|
---|
| 16 | type: "0",
|
---|
| 17 | name: "",
|
---|
| 18 | surname: "",
|
---|
| 19 | retainedSkills: [],
|
---|
| 20 | skillsToLearn: [],
|
---|
[df3a395] | 21 | error: null,
|
---|
| 22 | success: null,
|
---|
[7944fab] | 23 | sortedOptions:[]
|
---|
[351c43f] | 24 | }
|
---|
[7944fab] | 25 | this.attemptRegister = this.attemptRegister.bind(this);
|
---|
[351c43f] | 26 | }
|
---|
| 27 |
|
---|
[19ad843] | 28 | handleCheck = (e, {value}) => {
|
---|
| 29 | this.setState({
|
---|
| 30 | ...this.state,
|
---|
| 31 | [e.target.name]: value
|
---|
| 32 | })
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[7944fab] | 35 | setKnown = (e, {value}) =>{
|
---|
| 36 | this.setState({
|
---|
| 37 | retainedSkills: value
|
---|
| 38 | })
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | setToKnow = (e, {value}) =>{
|
---|
| 42 | this.setState({
|
---|
| 43 | skillsToLearn: value
|
---|
| 44 | })
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | attemptRegister(){
|
---|
| 48 | UserRegister.userRegister(
|
---|
| 49 | this.state.email,
|
---|
| 50 | this.state.password,
|
---|
| 51 | this.state.name,
|
---|
| 52 | this.state.surname,
|
---|
| 53 | this.state.retainedSkills,
|
---|
| 54 | this.state.skillsToLearn
|
---|
| 55 | ).then(res => {
|
---|
| 56 | if(res.data.success!=null){
|
---|
| 57 | this.setState({
|
---|
| 58 | success: res.data.success,
|
---|
| 59 | error: null
|
---|
| 60 | })
|
---|
[df3a395] | 61 |
|
---|
| 62 | this.props.message(this.state.success);
|
---|
[7944fab] | 63 | }else{
|
---|
| 64 | this.setState({
|
---|
| 65 | error: res.data.error,
|
---|
| 66 | success: null
|
---|
| 67 | })
|
---|
| 68 | }
|
---|
| 69 | }).catch(err => {
|
---|
| 70 | this.setState({
|
---|
| 71 | error: "User already exists!",
|
---|
| 72 | success: null
|
---|
| 73 | })
|
---|
| 74 | });
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[351c43f] | 77 | render() {
|
---|
[7944fab] | 78 | if(this.state.success!=null){
|
---|
| 79 | return(
|
---|
[df3a395] | 80 | <Redirect to={"/login"}/>
|
---|
[7944fab] | 81 | );
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[351c43f] | 84 | return (
|
---|
| 85 | <Container>
|
---|
| 86 | <h1 style={{color: "red"}}>{this.state.error}</h1>
|
---|
| 87 | <Form onSubmit={this.attemptRegister}>
|
---|
| 88 | <Form.Input id="email" name="email" type='email' required fluid label='E-mail'
|
---|
[19ad843] | 89 | placeholder='Enter e-mail.' onChange={this.handleCheck}/>
|
---|
[351c43f] | 90 | <Form.Input id="password" name="password" type='password' required fluid label='Password'
|
---|
[19ad843] | 91 | placeholder='Enter password.' onChange={this.handleCheck}/>
|
---|
| 92 | <Form.Input id="name" name="name" type='text' required fluid label='Name'
|
---|
| 93 | placeholder='Enter name.' onChange={this.handleCheck}/>
|
---|
| 94 | <Form.Input id="surname" name="surname" type='text' required fluid label='Surname'
|
---|
| 95 | placeholder='Enter surname.' onChange={this.handleCheck}/>
|
---|
[7944fab] | 96 | <Label>Select skills you know:</Label>
|
---|
| 97 | <Dropdown placeholder="Skills you know..." fluid multiple selection options={this.state.sortedOptions} onChange={this.setKnown}/>
|
---|
| 98 | <br/>
|
---|
| 99 | <Label>Select skills you want to know:</Label>
|
---|
| 100 | <Dropdown placeholder="Skills you want to learn..." fluid multiple selection options={this.state.sortedOptions} onChange={this.setToKnow}/>
|
---|
| 101 | <br/>
|
---|
[351c43f] | 102 | <Button type="submit">Register</Button>
|
---|
| 103 | </Form>
|
---|
| 104 | </Container>
|
---|
| 105 | );
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | componentDidMount(){
|
---|
[df3a395] | 109 | this.props.message(null);
|
---|
| 110 |
|
---|
[351c43f] | 111 | SkillFetch.fetchAll().then((data) =>{
|
---|
[7944fab] | 112 | var sorted = [];
|
---|
| 113 | data.data.forEach(item => {
|
---|
| 114 | var obj = {
|
---|
| 115 | key: item.id,
|
---|
| 116 | text: item.name,
|
---|
| 117 | value: item.id
|
---|
| 118 | }
|
---|
| 119 | sorted.push(obj);
|
---|
| 120 | });
|
---|
| 121 |
|
---|
[351c43f] | 122 | this.setState({
|
---|
[7944fab] | 123 | sortedOptions: sorted
|
---|
[351c43f] | 124 | })
|
---|
| 125 | })
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | export default RegisterUser; |
---|