source: client_app/src/components/register/register_user_form.js

Last change on this file was df3a395, checked in by Vzdra <vladko.zdravkovski@…>, 3 years ago

registration complete for all users

  • Property mode set to 100644
File size: 4.1 KB
Line 
1import React from 'react';
2import 'semantic-ui-react';
3import {Button, Container, Dropdown, Form, Label} from "semantic-ui-react";
4import { Redirect } from 'react-router-dom';
5import SkillFetch from "../../repository/skill_repo";
6import UserRegister from "../../repository/register_repo";
7import {Component} from "react/cjs/react.production.min";
8import Login from "../login/login";
9
10class 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: [],
21 error: null,
22 success: null,
23 sortedOptions:[]
24 }
25 this.attemptRegister = this.attemptRegister.bind(this);
26 }
27
28 handleCheck = (e, {value}) => {
29 this.setState({
30 ...this.state,
31 [e.target.name]: value
32 })
33 }
34
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 })
61
62 this.props.message(this.state.success);
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
77 render() {
78 if(this.state.success!=null){
79 return(
80 <Redirect to={"/login"}/>
81 );
82 }
83
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'
89 placeholder='Enter e-mail.' onChange={this.handleCheck}/>
90 <Form.Input id="password" name="password" type='password' required fluid label='Password'
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}/>
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/>
102 <Button type="submit">Register</Button>
103 </Form>
104 </Container>
105 );
106 }
107
108 componentDidMount(){
109 this.props.message(null);
110
111 SkillFetch.fetchAll().then((data) =>{
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
122 this.setState({
123 sortedOptions: sorted
124 })
125 })
126 }
127}
128
129export default RegisterUser;
Note: See TracBrowser for help on using the repository browser.