source: client_app/src/components/register/register_user_form.js@ 7944fab

Last change on this file since 7944fab was 7944fab, checked in by Vzdra <vladko.zdravkovski@…>, 4 years ago

finished user registration

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