[19398ad] | 1 | import React, {useState} from "react";
|
---|
| 2 | import "./Form.css";
|
---|
| 3 |
|
---|
| 4 | import 'react-responsive-modal/styles.css';
|
---|
| 5 | import {Modal} from 'react-responsive-modal';
|
---|
| 6 | import Select from "react-select";
|
---|
| 7 |
|
---|
| 8 | //Validation
|
---|
| 9 | import * as yup from "yup";
|
---|
| 10 | import {yupResolver} from "@hookform/resolvers/yup";
|
---|
| 11 | import {Controller, useForm} from "react-hook-form";
|
---|
| 12 |
|
---|
| 13 |
|
---|
| 14 | import {employmentStatusOptions, industryOptions, jobTypeOptions} from "../selectOptions";
|
---|
| 15 | import {useDispatch, useSelector} from "react-redux";
|
---|
| 16 | import {JobAdvertisementActions} from "../../redux/actions/jobAdvertisementActions";
|
---|
| 17 |
|
---|
| 18 |
|
---|
| 19 | export const AddJobAdModal = () => {
|
---|
| 20 | const [modal, setModal] = useState(false);
|
---|
| 21 | const dispatch = useDispatch();
|
---|
| 22 | const auth = useSelector(state => state.auth.currentUser)
|
---|
| 23 | const toggleModal = () => {
|
---|
| 24 | setModal(!modal);
|
---|
| 25 | };
|
---|
| 26 |
|
---|
| 27 | const schema = yup.object().shape({
|
---|
| 28 | title: yup.string().required("Please enter a title"),
|
---|
| 29 | description: yup.string().required("Please enter a description"),
|
---|
| 30 | industry: yup.mixed().required("Select industry"),
|
---|
| 31 | startingSalary: yup.number().required("Please enter the starting salary"),
|
---|
| 32 | jobType: yup.mixed().required("Select job type"),
|
---|
| 33 | employmentStatus: yup.mixed().required("Select employment status"),
|
---|
| 34 | })
|
---|
| 35 |
|
---|
| 36 | const {register, handleSubmit, control, formState: {errors}} = useForm({
|
---|
| 37 | resolver: yupResolver(schema),
|
---|
| 38 | });
|
---|
| 39 |
|
---|
| 40 | const addJobAdvertisement = async (values) => {
|
---|
| 41 | //const description = values.description.replace(/\n/g, "\\n");
|
---|
| 42 | try {
|
---|
| 43 | dispatch(JobAdvertisementActions.addJobAdvertisement(
|
---|
| 44 | {
|
---|
| 45 | email: auth.email,
|
---|
| 46 | title: values.title,
|
---|
| 47 | description: values.description,
|
---|
| 48 | industry: values.industry.value,
|
---|
| 49 | startingSalary: values.startingSalary,
|
---|
| 50 | activeUntil: values.date,
|
---|
| 51 | jobType: values.jobType.value,
|
---|
| 52 | employmentStatus: values.employmentStatus.value,
|
---|
| 53 | }, (success, response) => {
|
---|
| 54 | if(success) {
|
---|
| 55 | console.log("Job Advertisement added")
|
---|
| 56 | toggleModal()
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | ))
|
---|
| 60 | } catch (err) {
|
---|
| 61 | console.error(err)
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | let minimumDate = new Date();
|
---|
| 65 | minimumDate.setDate(minimumDate.getDate() + 1);
|
---|
| 66 |
|
---|
| 67 | return (<div className="modal-wrap">
|
---|
| 68 | <button onClick={toggleModal} className="btn-open-modal">POST ADVERTISEMENT</button>
|
---|
| 69 | <Modal open={modal} onClose={toggleModal} center classNames="job-advertisement-modal">
|
---|
| 70 | <div className="head-modal">
|
---|
| 71 | <h3>Post Job Advertisement</h3>
|
---|
| 72 | <i className="fa-solid fa-x btn-close-modal" onClick={toggleModal}></i>
|
---|
| 73 | </div>
|
---|
| 74 |
|
---|
| 75 | <div className="modal-content">
|
---|
| 76 | <form onSubmit={handleSubmit(addJobAdvertisement)}>
|
---|
| 77 | <div className="row">
|
---|
| 78 | <div className="col-md-7">
|
---|
| 79 |
|
---|
| 80 | <label className="label">Job title:</label>
|
---|
| 81 | <input type="text" {...register("title")}/>
|
---|
| 82 | <p style={{color: "red"}}>{errors.title?.message}</p>
|
---|
| 83 |
|
---|
| 84 | <label className="label">Job description:</label>
|
---|
| 85 | <textarea type="text" placeholder="Describe the job position and all the requirements"
|
---|
| 86 | className="description-textarea" {...register("description")}/>
|
---|
| 87 | <p style={{color: "red"}}>{errors.description?.message}</p>
|
---|
| 88 | </div>
|
---|
| 89 |
|
---|
| 90 | <div className="col-md-5">
|
---|
| 91 | <label className="label">Hourly rate:</label>
|
---|
| 92 | <input {...register("startingSalary")}/>
|
---|
| 93 | <p style={{color: "red"}}>{errors.startingSalary?.message}</p>
|
---|
| 94 |
|
---|
| 95 | <label className="label">Industry:</label>
|
---|
| 96 | <Controller
|
---|
| 97 | name="industry"
|
---|
| 98 | control={control}
|
---|
| 99 | render={({ field }) => (<Select
|
---|
| 100 | {...field}
|
---|
| 101 | options={industryOptions}
|
---|
| 102 | />)}
|
---|
| 103 | />
|
---|
| 104 | <p style={{color: "red"}}>{errors.industry?.message}</p>
|
---|
| 105 |
|
---|
| 106 | <label className="label">Job type:</label>
|
---|
| 107 | <Controller
|
---|
| 108 | name="jobType"
|
---|
| 109 | control={control}
|
---|
| 110 | render={({ field }) => (<Select
|
---|
| 111 | {...field}
|
---|
| 112 | options={jobTypeOptions}
|
---|
| 113 | />)}
|
---|
| 114 | />
|
---|
| 115 | <p style={{color: "red"}}>{errors.jobType?.message}</p>
|
---|
| 116 |
|
---|
| 117 | <label className="label">Employment status</label>
|
---|
| 118 | <Controller
|
---|
| 119 | name="employmentStatus"
|
---|
| 120 | control={control}
|
---|
| 121 | render={({ field }) => (<Select
|
---|
| 122 | {...field}
|
---|
| 123 | options={employmentStatusOptions}
|
---|
| 124 | />)}
|
---|
| 125 | />
|
---|
| 126 | <p style={{color: "red"}}>{errors.employmentStatus?.message}</p>
|
---|
| 127 |
|
---|
| 128 | <label htmlFor="start">Active until:</label>
|
---|
| 129 | <input type="date" defaultValue={minimumDate.toLocaleDateString('en-CA')}
|
---|
| 130 | min={minimumDate.toLocaleDateString('en-CA')} onChange={(event) => console.log(event.target.value)}
|
---|
| 131 | {...register("date")}/>
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | </div>
|
---|
| 135 | </div>
|
---|
| 136 |
|
---|
| 137 | <div className="aligned">
|
---|
| 138 | <button className="submit-btn"> Submit</button>
|
---|
| 139 | </div>
|
---|
| 140 |
|
---|
| 141 | </form>
|
---|
| 142 | </div>
|
---|
| 143 | </Modal>
|
---|
| 144 | </div>)
|
---|
| 145 | } |
---|