1 | import React from 'react';
|
---|
2 | import 'semantic-ui-react';
|
---|
3 | import SemanticDatePicker from 'react-semantic-ui-datepickers';
|
---|
4 | import {Button, Container, Dropdown, Form, Label} from "semantic-ui-react";
|
---|
5 | import { Redirect } from 'react-router-dom';
|
---|
6 | import SkillFetch from "../../repository/skill_repo";
|
---|
7 | import {Component} from "react/cjs/react.production.min";
|
---|
8 | import WorkRegister from "../../repository/work_register_repo";
|
---|
9 |
|
---|
10 | class ProjectRegister extends Component {
|
---|
11 | constructor(props) {
|
---|
12 | super(props);
|
---|
13 | this.state = {
|
---|
14 | title: "",
|
---|
15 | description: "",
|
---|
16 | accountId: props.accountId,
|
---|
17 | salary: 0,
|
---|
18 | type: props.type,
|
---|
19 | validUntil: "",
|
---|
20 | skillsRequired: [],
|
---|
21 | error: null,
|
---|
22 | success: null,
|
---|
23 | sortedOptions: []
|
---|
24 | }
|
---|
25 | this.attemptProjectRegister = this.attemptProjectRegister.bind(this);
|
---|
26 | }
|
---|
27 |
|
---|
28 | handleCheck = (e, {value}) => {
|
---|
29 | this.setState({
|
---|
30 | ...this.state,
|
---|
31 | [e.target.name]: value
|
---|
32 | })
|
---|
33 | }
|
---|
34 | editValid = (e, {value}) => {
|
---|
35 | this.setState({
|
---|
36 | validUntil: value
|
---|
37 | })
|
---|
38 | }
|
---|
39 |
|
---|
40 | setSkills = (e, {value}) =>{
|
---|
41 | this.setState({
|
---|
42 | skillsRequired: value
|
---|
43 | })
|
---|
44 | }
|
---|
45 |
|
---|
46 | attemptProjectRegister(){
|
---|
47 | WorkRegister.projectRegister(
|
---|
48 | this.state.title,
|
---|
49 | this.state.description,
|
---|
50 | this.state.accountId,
|
---|
51 | this.state.salary,
|
---|
52 | this.state.type,
|
---|
53 | this.state.validUntil,
|
---|
54 | this.state.skillsRequired
|
---|
55 | ).then(res => {
|
---|
56 | this.props.updateProjects(res.data);
|
---|
57 | this.setState({
|
---|
58 | success: true,
|
---|
59 | error: null
|
---|
60 | })
|
---|
61 | }).catch(err => {
|
---|
62 | console.log(err);
|
---|
63 | this.setState({
|
---|
64 | error: "Error registering job!",
|
---|
65 | success: null
|
---|
66 | })
|
---|
67 | });
|
---|
68 | }
|
---|
69 |
|
---|
70 | render() {
|
---|
71 | if(this.state.accountId==null || this.state.type==="USER"){
|
---|
72 | return(
|
---|
73 | <Redirect to={"/login"}/>
|
---|
74 | );
|
---|
75 | }
|
---|
76 |
|
---|
77 | if(this.state.success===true){
|
---|
78 | return (
|
---|
79 | <Redirect to={"/team/projects"}/>
|
---|
80 | );
|
---|
81 | }
|
---|
82 |
|
---|
83 | return (
|
---|
84 | <Container>
|
---|
85 | <h1 style={{color: "red"}}>{this.state.error}</h1>
|
---|
86 | <Form onSubmit={this.attemptProjectRegister}>
|
---|
87 | <Form.Input id="title" name="title" type='text' required fluid label='Title'
|
---|
88 | placeholder='Enter title...' onChange={this.handleCheck}/>
|
---|
89 | <Form.Input id="description" name="description" type='text' required fluid label='Description'
|
---|
90 | placeholder='Enter description...' onChange={this.handleCheck}/>
|
---|
91 | <Form.Input id="salary" name="salary" type='number' required fluid label='Salary'
|
---|
92 | placeholder='Enter salary...' onChange={this.handleCheck}/>
|
---|
93 | <SemanticDatePicker required local="en_US" format='YYYY-MM-DD' onChange={this.editValid} type="basic"/>
|
---|
94 | <br />
|
---|
95 | <Label>Select required skills:</Label>
|
---|
96 | <Dropdown placeholder="Skills needed for the project..." fluid multiple selection options={this.state.sortedOptions} onChange={this.setSkills}/>
|
---|
97 | <br/>
|
---|
98 | <Button type="submit">Add</Button>
|
---|
99 | </Form>
|
---|
100 | </Container>
|
---|
101 | );
|
---|
102 | }
|
---|
103 |
|
---|
104 | componentDidMount(){
|
---|
105 | SkillFetch.fetchAll().then((data) =>{
|
---|
106 | var sorted = [];
|
---|
107 | data.data.forEach(item => {
|
---|
108 | var obj = {
|
---|
109 | key: item.id,
|
---|
110 | text: item.name,
|
---|
111 | value: item.id
|
---|
112 | }
|
---|
113 | sorted.push(obj);
|
---|
114 | });
|
---|
115 |
|
---|
116 | this.setState({
|
---|
117 | sortedOptions: sorted
|
---|
118 | })
|
---|
119 | })
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | export default ProjectRegister; |
---|