[580ba1a] | 1 | import React from 'react';
|
---|
| 2 | import 'semantic-ui-react';
|
---|
| 3 | import {Button, Container, Dropdown, Form, Label} from "semantic-ui-react";
|
---|
| 4 | import { Redirect } from 'react-router-dom';
|
---|
| 5 | import SkillFetch from "../../repository/skill_repo";
|
---|
| 6 | import {Component} from "react/cjs/react.production.min";
|
---|
| 7 | import AccountEdit from "../../repository/edit_account_repo";
|
---|
| 8 |
|
---|
| 9 | class 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 | })
|
---|
[be373a8] | 81 |
|
---|
[580ba1a] | 82 | this.props.updateUser(res.data);
|
---|
| 83 | }).catch(err => {
|
---|
| 84 | this.setState({
|
---|
| 85 | success: null,
|
---|
[be373a8] | 86 | error: "User already exists.",
|
---|
[580ba1a] | 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 |
|
---|
| 151 | export default EditUser; |
---|