[19398ad] | 1 | import React, {useEffect, 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";
|
---|
[28b3398] | 17 | import {Editor} from "primereact/editor";
|
---|
[19398ad] | 18 |
|
---|
| 19 |
|
---|
| 20 | export const EditJobAdModal = (jobAd) => {
|
---|
| 21 | const [modal, setModal] = useState(false);
|
---|
| 22 | const dispatch = useDispatch();
|
---|
| 23 | const auth = useSelector(state => state.auth.currentUser)
|
---|
| 24 | const toggleModal = () => {
|
---|
| 25 | setModal(!modal);
|
---|
| 26 | };
|
---|
| 27 |
|
---|
| 28 | const schema = yup.object().shape({
|
---|
| 29 | title: yup.string().required("Please enter a title"),
|
---|
| 30 | description: yup.string().required("Please enter a description"),
|
---|
| 31 | industry: yup.mixed().required("Select industry"),
|
---|
| 32 | startingSalary: yup.number().required("Please enter the starting salary"),
|
---|
| 33 | jobType: yup.mixed().required("Select job type"),
|
---|
| 34 | employmentStatus: yup.mixed().required("Select employment status"),
|
---|
| 35 | })
|
---|
| 36 |
|
---|
| 37 | const {register, handleSubmit, control, formState: {errors}} = useForm({
|
---|
| 38 | resolver: yupResolver(schema),
|
---|
| 39 | });
|
---|
| 40 | const editJobAdvertisement = async (values) => {
|
---|
| 41 | try {
|
---|
| 42 | dispatch(JobAdvertisementActions.editJobAdvertisement(
|
---|
| 43 | {
|
---|
| 44 | email: auth.email,
|
---|
| 45 | title: values.title,
|
---|
| 46 | description: values.description,
|
---|
| 47 | industry: values.industry.value,
|
---|
| 48 | startingSalary: values.startingSalary,
|
---|
| 49 | activeUntil: values.date,
|
---|
| 50 | jobType: values.jobType.value,
|
---|
| 51 | employmentStatus: values.employmentStatus.value,
|
---|
| 52 | }, jobAd.props.id, (success, response) => {
|
---|
| 53 | if(success) {
|
---|
| 54 | console.log("Job Advertisement edited")
|
---|
| 55 | toggleModal()
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | ))
|
---|
| 59 | } catch (err) {
|
---|
| 60 | console.error(err)
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | let minimumDate = new Date();
|
---|
| 65 | minimumDate.setDate(minimumDate.getDate() + 1);
|
---|
| 66 |
|
---|
| 67 | return (<div className="modal-wrap">
|
---|
| 68 |
|
---|
| 69 | <i className="fa-solid fa-pen-to-square pen-edit-btn" onClick={() => {
|
---|
| 70 | // setSelectedRecord(record);
|
---|
| 71 | toggleModal()
|
---|
| 72 | }}></i>
|
---|
| 73 | <Modal open={modal} onClose={toggleModal} center classNames="job-advertisement-modal">
|
---|
| 74 | <div className="head-modal">
|
---|
| 75 | <h3>Edit Job Advertisement</h3>
|
---|
| 76 | <i className="fa-solid fa-x btn-close-modal" onClick={toggleModal}></i>
|
---|
| 77 | </div>
|
---|
| 78 |
|
---|
| 79 | <div className="modal-content">
|
---|
| 80 | <form onSubmit={handleSubmit(editJobAdvertisement)}>
|
---|
| 81 | <div className="row">
|
---|
| 82 | <div className="col-md-7">
|
---|
| 83 | <label className="label">Job title:</label>
|
---|
| 84 | <input type="text" defaultValue={jobAd.props.title} {...register("title")}/>
|
---|
| 85 | <p style={{color: "red"}}>{errors.title?.message}</p>
|
---|
| 86 |
|
---|
| 87 | <label className="label">Job description:</label>
|
---|
[28b3398] | 88 | {/*<textarea type="text" defaultValue={jobAd.props.description} placeholder="Describe the job position and all the requirements"*/}
|
---|
| 89 | {/* className="description-textarea" {...register("description")}/>*/}
|
---|
| 90 |
|
---|
| 91 | <Controller
|
---|
| 92 | name="description"
|
---|
| 93 | control={control}
|
---|
| 94 | render={({ field }) => (
|
---|
| 95 | <Editor
|
---|
| 96 | defaultValue={jobAd.props.description}
|
---|
| 97 | value={jobAd.props.description}
|
---|
| 98 | onTextChange={(e) => field.onChange(e.htmlValue)}
|
---|
| 99 | style={{ height: '300px', fontSize: "16px", fontFamily: "Segoe UI" }}
|
---|
| 100 | />
|
---|
| 101 | )}
|
---|
| 102 | />
|
---|
| 103 |
|
---|
[19398ad] | 104 | <p style={{color: "red"}}>{errors.description?.message}</p>
|
---|
| 105 | </div>
|
---|
| 106 |
|
---|
| 107 | <div className="col-md-5">
|
---|
| 108 | <label className="label">Hourly rate:</label>
|
---|
| 109 | <input defaultValue={jobAd.props.startingSalary} {...register("startingSalary")}/>
|
---|
| 110 | <p style={{color: "red"}}>{errors.startingSalary?.message}</p>
|
---|
| 111 |
|
---|
| 112 | <label className="label">Industry:</label>
|
---|
| 113 | <Controller
|
---|
| 114 | name="industry"
|
---|
| 115 | control={control}
|
---|
| 116 | defaultValue={{
|
---|
| 117 | label: jobAd.props.industry,
|
---|
| 118 | value: jobAd.props.industry
|
---|
| 119 | }}
|
---|
| 120 | render={({ field }) => (<Select
|
---|
| 121 | {...field}
|
---|
| 122 | options={industryOptions}
|
---|
| 123 | />)}
|
---|
| 124 | />
|
---|
| 125 | <p style={{color: "red"}}>{errors.industry?.message}</p>
|
---|
| 126 |
|
---|
| 127 | <label className="label">Job type:</label>
|
---|
| 128 | <Controller
|
---|
| 129 | name="jobType"
|
---|
| 130 | control={control}
|
---|
| 131 | defaultValue={{
|
---|
| 132 | value: jobAd.props.jobType,
|
---|
| 133 | label: jobAd.props.jobType==="JOB" ? "Job" : "Internship"
|
---|
| 134 | }}
|
---|
| 135 | render={({ field }) => (
|
---|
| 136 | <Select
|
---|
| 137 | {...field}
|
---|
| 138 | options={jobTypeOptions}
|
---|
| 139 | />)}
|
---|
| 140 | />
|
---|
| 141 | <p style={{color: "red"}}>{errors.jobType?.message}</p>
|
---|
| 142 |
|
---|
| 143 | <label className="label">Employment status</label>
|
---|
| 144 | <Controller
|
---|
| 145 | name="employmentStatus"
|
---|
| 146 | control={control}
|
---|
| 147 | defaultValue={{
|
---|
| 148 | value: jobAd.props.employmentStatus,
|
---|
| 149 | label: jobAd.props.employmentStatus==="FULL_TIME" ? "Full time" : "Part time"
|
---|
| 150 | }}
|
---|
| 151 | render={({ field }) => (
|
---|
| 152 | <Select
|
---|
| 153 | {...field}
|
---|
| 154 | options={employmentStatusOptions}
|
---|
| 155 | />)}
|
---|
| 156 | />
|
---|
| 157 | <p style={{color: "red"}}>{errors.employmentStatus?.message}</p>
|
---|
| 158 |
|
---|
| 159 | <label htmlFor="start">Active until:</label>
|
---|
| 160 | <input type="date" defaultValue={new Date(jobAd.props.activeUntil).toLocaleDateString('en-CA')}
|
---|
| 161 | min={minimumDate.toLocaleDateString('en-CA')} onChange={(event) => console.log(event.target.value)}
|
---|
| 162 | {...register("date")}/>
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | </div>
|
---|
| 166 | </div>
|
---|
| 167 |
|
---|
| 168 | <div className="aligned">
|
---|
| 169 | <button className="submit-btn"> Submit</button>
|
---|
| 170 | </div>
|
---|
| 171 |
|
---|
| 172 | </form>
|
---|
| 173 | </div>
|
---|
| 174 | </Modal>
|
---|
| 175 | </div>)
|
---|
| 176 | } |
---|