source: client_app/src/components/account_edit/user_edit.js@ 580ba1a

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

register and edit

  • Property mode set to 100644
File size: 4.4 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 {Component} from "react/cjs/react.production.min";
7import AccountEdit from "../../repository/edit_account_repo";
8
9class EditUser extends Component {
10 constructor(props) {
11 super(props);
12 this.state = {
13 email: props.oldUser.email,
14 name: props.oldUser.name,
15 surname: props.oldUser.surname,
16 retainedSkills: props.oldUser.retained,
17 skillsToLearn: props.oldUser.toLearn,
18 type: props.oldUser.type,
19 error: null,
20 success: null,
21 sortedOptions:[],
22 id: props.oldUser.id,
23 oldEmail: props.oldUser.email,
24 toLearn: [],
25 retained: []
26 }
27 this.attemptEdit = this.attemptEdit.bind(this);
28 }
29
30 handleCheck = (e, {value}) => {
31 this.setState({
32 ...this.state,
33 [e.target.name]: value
34 })
35 }
36
37 setKnown = (e, {value}) =>{
38 this.setState({
39 retained: value
40 })
41 }
42
43 setToKnow = (e, {value}) =>{
44 this.setState({
45 toLearn: value
46 })
47 }
48
49 setIds = () => {
50 let k = [];
51 let m = [];
52
53 this.state.skillsToLearn.forEach(item => {
54 k.push(item.id);
55 })
56
57 this.state.retainedSkills.forEach(item=>{
58 m.push(item.id);
59 })
60
61 this.setState({
62 toLearn: k,
63 retained: m
64 })
65 }
66
67 attemptEdit(){
68 AccountEdit.userEdit(
69 this.state.id,
70 this.state.oldEmail,
71 this.state.email,
72 this.state.name,
73 this.state.surname,
74 this.state.retained,
75 this.state.toLearn
76 ).then(res => {
77 this.setState({
78 success: true,
79 error: null
80 })
81 this.props.updateUser(res.data);
82 }).catch(err => {
83 this.setState({
84 success: null,
85 error: "Error editing user.",
86 })
87 });
88 }
89
90 render() {
91 if(this.state.success!=null){
92 return(
93 <Redirect to={"/profile"}/>
94 );
95 }
96
97 if(this.state.type!=="USER"){
98 return(
99 <Redirect to={"/profile"}/>
100 );
101 }
102
103 return (
104 <Container>
105 <h1 style={{color: "red"}}>{this.state.error}</h1>
106 <Form onSubmit={this.attemptEdit}>
107 <Form.Input id="email" name="email" type='email' required fluid label='E-mail'
108 placeholder='Enter e-mail.' value={this.state.email} onChange={this.handleCheck}/>
109 <Form.Input id="name" name="name" type='text' required fluid label='Name'
110 placeholder='Enter name.' value={this.state.name} onChange={this.handleCheck}/>
111 <Form.Input id="surname" name="surname" type='text' required fluid label='Surname'
112 placeholder='Enter surname.' value={this.state.surname} onChange={this.handleCheck}/>
113 <Label>Select skills you know:</Label>
114 <Dropdown placeholder="Skills you know..." fluid multiple selection value={this.state.retained} options={this.state.sortedOptions} onChange={this.setKnown}/>
115 <br/>
116 <Label>Select skills you want to know:</Label>
117 <Dropdown placeholder="Skills you want to learn..." fluid multiple value={this.state.toLearn} selection options={this.state.sortedOptions} onChange={this.setToKnow}/>
118 <br/>
119 <Button primary type="submit">Edit</Button>
120 </Form>
121 </Container>
122 );
123 }
124
125 componentDidMount(){
126 this.props.message(null);
127
128 SkillFetch.fetchAll().then((data) =>{
129 let sorted = [];
130 data.data.forEach(item => {
131 let obj = {
132 key: item.id,
133 text: item.name,
134 value: item.id
135 }
136 sorted.push(obj);
137 });
138
139 this.setState({
140 sortedOptions: sorted
141 })
142 });
143
144 this.setIds();
145 }
146
147
148}
149
150export default EditUser;
Note: See TracBrowser for help on using the repository browser.