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