[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";
|
---|
| 17 |
|
---|
| 18 |
|
---|
| 19 | export const EditJobAdModal = (jobAd) => {
|
---|
| 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 | const editJobAdvertisement = async (values) => {
|
---|
| 40 | try {
|
---|
| 41 | dispatch(JobAdvertisementActions.editJobAdvertisement(
|
---|
| 42 | {
|
---|
| 43 | email: auth.email,
|
---|
| 44 | title: values.title,
|
---|
| 45 | description: values.description,
|
---|
| 46 | industry: values.industry.value,
|
---|
| 47 | startingSalary: values.startingSalary,
|
---|
| 48 | activeUntil: values.date,
|
---|
| 49 | jobType: values.jobType.value,
|
---|
| 50 | employmentStatus: values.employmentStatus.value,
|
---|
| 51 | }, jobAd.props.id, (success, response) => {
|
---|
| 52 | if(success) {
|
---|
| 53 | console.log("Job Advertisement edited")
|
---|
| 54 | toggleModal()
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | ))
|
---|
| 58 | } catch (err) {
|
---|
| 59 | console.error(err)
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | let minimumDate = new Date();
|
---|
| 64 | minimumDate.setDate(minimumDate.getDate() + 1);
|
---|
| 65 |
|
---|
| 66 | return (<div className="modal-wrap">
|
---|
| 67 |
|
---|
| 68 | <i className="fa-solid fa-pen-to-square pen-edit-btn" onClick={() => {
|
---|
| 69 | // setSelectedRecord(record);
|
---|
| 70 | toggleModal()
|
---|
| 71 | }}></i>
|
---|
| 72 | <Modal open={modal} onClose={toggleModal} center classNames="job-advertisement-modal">
|
---|
| 73 | <div className="head-modal">
|
---|
| 74 | <h3>Edit Job Advertisement</h3>
|
---|
| 75 | <i className="fa-solid fa-x btn-close-modal" onClick={toggleModal}></i>
|
---|
| 76 | </div>
|
---|
| 77 |
|
---|
| 78 | <div className="modal-content">
|
---|
| 79 | <form onSubmit={handleSubmit(editJobAdvertisement)}>
|
---|
| 80 | <div className="row">
|
---|
| 81 | <div className="col-md-7">
|
---|
| 82 | <label className="label">Job title:</label>
|
---|
| 83 | <input type="text" defaultValue={jobAd.props.title} {...register("title")}/>
|
---|
| 84 | <p style={{color: "red"}}>{errors.title?.message}</p>
|
---|
| 85 |
|
---|
| 86 | <label className="label">Job description:</label>
|
---|
| 87 | <textarea type="text" defaultValue={jobAd.props.description} placeholder="Describe the job position and all the requirements"
|
---|
| 88 | className="description-textarea" {...register("description")}/>
|
---|
| 89 | <p style={{color: "red"}}>{errors.description?.message}</p>
|
---|
| 90 | </div>
|
---|
| 91 |
|
---|
| 92 | <div className="col-md-5">
|
---|
| 93 | <label className="label">Hourly rate:</label>
|
---|
| 94 | <input defaultValue={jobAd.props.startingSalary} {...register("startingSalary")}/>
|
---|
| 95 | <p style={{color: "red"}}>{errors.startingSalary?.message}</p>
|
---|
| 96 |
|
---|
| 97 | <label className="label">Industry:</label>
|
---|
| 98 | <Controller
|
---|
| 99 | name="industry"
|
---|
| 100 | control={control}
|
---|
| 101 | defaultValue={{
|
---|
| 102 | label: jobAd.props.industry,
|
---|
| 103 | value: jobAd.props.industry
|
---|
| 104 | }}
|
---|
| 105 | render={({ field }) => (<Select
|
---|
| 106 | {...field}
|
---|
| 107 | options={industryOptions}
|
---|
| 108 | />)}
|
---|
| 109 | />
|
---|
| 110 | <p style={{color: "red"}}>{errors.industry?.message}</p>
|
---|
| 111 |
|
---|
| 112 | <label className="label">Job type:</label>
|
---|
| 113 | <Controller
|
---|
| 114 | name="jobType"
|
---|
| 115 | control={control}
|
---|
| 116 | defaultValue={{
|
---|
| 117 | value: jobAd.props.jobType,
|
---|
| 118 | label: jobAd.props.jobType==="JOB" ? "Job" : "Internship"
|
---|
| 119 | }}
|
---|
| 120 | render={({ field }) => (
|
---|
| 121 | <Select
|
---|
| 122 | {...field}
|
---|
| 123 | options={jobTypeOptions}
|
---|
| 124 | />)}
|
---|
| 125 | />
|
---|
| 126 | <p style={{color: "red"}}>{errors.jobType?.message}</p>
|
---|
| 127 |
|
---|
| 128 | <label className="label">Employment status</label>
|
---|
| 129 | <Controller
|
---|
| 130 | name="employmentStatus"
|
---|
| 131 | control={control}
|
---|
| 132 | defaultValue={{
|
---|
| 133 | value: jobAd.props.employmentStatus,
|
---|
| 134 | label: jobAd.props.employmentStatus==="FULL_TIME" ? "Full time" : "Part time"
|
---|
| 135 | }}
|
---|
| 136 | render={({ field }) => (
|
---|
| 137 | <Select
|
---|
| 138 | {...field}
|
---|
| 139 | options={employmentStatusOptions}
|
---|
| 140 | />)}
|
---|
| 141 | />
|
---|
| 142 | <p style={{color: "red"}}>{errors.employmentStatus?.message}</p>
|
---|
| 143 |
|
---|
| 144 | <label htmlFor="start">Active until:</label>
|
---|
| 145 | <input type="date" defaultValue={new Date(jobAd.props.activeUntil).toLocaleDateString('en-CA')}
|
---|
| 146 | min={minimumDate.toLocaleDateString('en-CA')} onChange={(event) => console.log(event.target.value)}
|
---|
| 147 | {...register("date")}/>
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 | </div>
|
---|
| 151 | </div>
|
---|
| 152 |
|
---|
| 153 | <div className="aligned">
|
---|
| 154 | <button className="submit-btn"> Submit</button>
|
---|
| 155 | </div>
|
---|
| 156 |
|
---|
| 157 | </form>
|
---|
| 158 | </div>
|
---|
| 159 | </Modal>
|
---|
| 160 | </div>)
|
---|
| 161 | } |
---|