source: jobvista-frontend/src/views/job_advertisements/AddJobAdModal.js@ 08f82ec

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

Did more refactoring

  • Property mode set to 100644
File size: 7.1 KB
Line 
1import React, {useState} from "react";
2import "../shared_css/Modal.css";
3
4import { Editor } from 'primereact/editor';
5
6import 'react-responsive-modal/styles.css';
7import {Modal} from 'react-responsive-modal';
8import Select from "react-select";
9
10//Validation
11import * as yup from "yup";
12import {yupResolver} from "@hookform/resolvers/yup";
13import {Controller, useForm} from "react-hook-form";
14
15
16import {employmentStatusOptions, industryOptions, jobTypeOptions} from "../selectOptions";
17import {useDispatch, useSelector} from "react-redux";
18import {JobAdvertisementActions} from "../../redux/actions/jobAdvertisementActions";
19import {notifyJobAdPost} from "../../utils/toastUtils";
20
21
22export const AddJobAdModal = () => {
23 const [modal, setModal] = useState(false);
24 const [text, setText] = useState("");
25 const dispatch = useDispatch();
26 const auth = useSelector(state => state.auth.currentUser)
27 const toggleModal = () => {
28 setModal(!modal);
29 };
30
31 const schema = yup.object().shape({
32 title: yup.string().required("Please enter a title"),
33 description: yup.string().required("Please enter a description"),
34 industry: yup.mixed().required("Select industry"),
35 startingSalary: yup.string().required("Please enter the starting salary"),
36 jobType: yup.mixed().required("Select job type"),
37 employmentStatus: yup.mixed().required("Select employment status"),
38 })
39
40 const {register, handleSubmit, control, formState: {errors}} = useForm({
41 resolver: yupResolver(schema),
42 });
43
44 const addJobAdvertisement = async (values) => {
45 try {
46 dispatch(JobAdvertisementActions.addJobAdvertisement(
47 {
48 id: auth.id,
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 }, (success) => {
57 if (success) {
58 toggleModal()
59 notifyJobAdPost()
60 }
61 }
62 ))
63 } catch (err) {
64 console.error(err)
65 }
66 }
67 let minimumDate = new Date();
68 minimumDate.setDate(minimumDate.getDate() + 1);
69
70 return (<div className="modal-wrap">
71 <div className="col">
72 <button onClick={toggleModal} className="add-new-card">
73 <h3>+ Post Advertisement</h3>
74 </button>
75 </div>
76 {/*<button onClick={toggleModal} className="btn-open-modal">POST ADVERTISEMENT</button>*/}
77 <Modal open={modal} onClose={toggleModal} center>
78 <div className="head-modal">
79 <h3>Post 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
88 <label className="label">Job title:</label>
89 <input type="text" {...register("title")}/>
90 <p style={{color: "red"}}>{errors.title?.message}</p>
91
92 <label className="label">Job description:</label>
93 {/*<textarea type="text" placeholder="Describe the job position and all the requirements"*/}
94 {/* className="description-textarea" {...register("description")}/>*/}
95
96
97 <Controller
98 name="description"
99 control={control}
100 render={({ field }) => (
101 <Editor
102 value={field.value}
103 onTextChange={(e) => field.onChange(e.htmlValue)}
104 style={{ height: '300px', fontSize: "16px", fontFamily: "Segoe UI" }}
105 />
106 )}
107 />
108 <p style={{color: "red"}}>{errors.description?.message}</p>
109 </div>
110
111 <div className="col-md-5">
112 <label className="label">Hourly rate:</label>
113 <input type="number" {...register("startingSalary")}/>
114 <p style={{color: "red"}}>{errors.startingSalary?.message}</p>
115
116 <label className="label">Industry:</label>
117 <Controller
118 name="industry"
119 control={control}
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 render={({field}) => (<Select
132 {...field}
133 options={jobTypeOptions}
134 />)}
135 />
136 <p style={{color: "red"}}>{errors.jobType?.message}</p>
137
138 <label className="label">Employment status</label>
139 <Controller
140 name="employmentStatus"
141 control={control}
142 render={({field}) => (<Select
143 {...field}
144 options={employmentStatusOptions}
145 />)}
146 />
147 <p style={{color: "red"}}>{errors.employmentStatus?.message}</p>
148
149 <label htmlFor="start">Active until:</label>
150 <input type="date" defaultValue={minimumDate.toLocaleDateString('en-CA')}
151 min={minimumDate.toLocaleDateString('en-CA')}
152 onChange={(event) => console.log(event.target.value)}
153 {...register("date")}/>
154
155
156 </div>
157 </div>
158
159 <div className="modal-buttons">
160 <div className="cancel-btn" onClick={toggleModal}> Cancel</div>
161 <button className="submit-btn" onClick={handleSubmit(addJobAdvertisement)}>Submit</button>
162 </div>
163
164 </form>
165 </div>
166 </Modal>
167 </div>)
168}
Note: See TracBrowser for help on using the repository browser.