source: client_app/src/components/account_edit/user_edit.js@ ed3f5c4

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

edit complete

  • 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
82 this.props.updateUser(res.data);
83 }).catch(err => {
84 this.setState({
85 success: null,
86 error: "User already exists.",
87 })
88 });
89 }
90
91 render() {
92 if(this.state.success!=null){
93 return(
94 <Redirect to={"/profile"}/>
95 );
96 }
97
98 if(this.state.type!=="USER"){
99 return(
100 <Redirect to={"/profile"}/>
101 );
102 }
103
104 return (
105 <Container>
106 <h1 style={{color: "red"}}>{this.state.error}</h1>
107 <Form onSubmit={this.attemptEdit}>
108 <Form.Input id="email" name="email" type='email' required fluid label='E-mail'
109 placeholder='Enter e-mail.' value={this.state.email} onChange={this.handleCheck}/>
110 <Form.Input id="name" name="name" type='text' required fluid label='Name'
111 placeholder='Enter name.' value={this.state.name} onChange={this.handleCheck}/>
112 <Form.Input id="surname" name="surname" type='text' required fluid label='Surname'
113 placeholder='Enter surname.' value={this.state.surname} onChange={this.handleCheck}/>
114 <Label>Select skills you know:</Label>
115 <Dropdown placeholder="Skills you know..." fluid multiple selection value={this.state.retained} options={this.state.sortedOptions} onChange={this.setKnown}/>
116 <br/>
117 <Label>Select skills you want to know:</Label>
118 <Dropdown placeholder="Skills you want to learn..." fluid multiple value={this.state.toLearn} selection options={this.state.sortedOptions} onChange={this.setToKnow}/>
119 <br/>
120 <Button primary type="submit">Edit</Button>
121 </Form>
122 </Container>
123 );
124 }
125
126 componentDidMount(){
127 this.props.message(null);
128
129 SkillFetch.fetchAll().then((data) =>{
130 let sorted = [];
131 data.data.forEach(item => {
132 let obj = {
133 key: item.id,
134 text: item.name,
135 value: item.id
136 }
137 sorted.push(obj);
138 });
139
140 this.setState({
141 sortedOptions: sorted
142 })
143 });
144
145 this.setIds();
146 }
147
148
149}
150
151export default EditUser;
Note: See TracBrowser for help on using the repository browser.