source: jobvista-frontend/src/views/job_advertisements/EditJobAdModal.js@ 4d97b63

main
Last change on this file since 4d97b63 was 08f82ec, checked in by 223021 <daniel.ilievski.2@…>, 5 months ago

Did more refactoring

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