diff --git a/src/components/VacancyCard/EditVacancyForm/EditActionButtons.jsx b/src/components/VacancyCard/EditVacancyForm/EditActionButtons.jsx new file mode 100644 index 00000000..d436ed1b --- /dev/null +++ b/src/components/VacancyCard/EditVacancyForm/EditActionButtons.jsx @@ -0,0 +1,104 @@ +import React, { useState } from 'react'; +import { Button } from '../../../ui'; +import DeleteIcon from '../../../images/DeleteIcon/DeleteIcon'; +import { updateVacancy, deleteVacancy } from '../../../modules/ShelterVacancies/components/AddVacancyForm/components/vacanciesAPI'; + +const EditActionButtons = ({ formData, isSubmitButtonDisabled, onDelete, onClose, onSubmitSuccess, isLoading }) => { + const [isModalOpen, setIsModalOpen] = useState(false); + + const handleSubmit = async () => { + try { + const token = localStorage.getItem('access'); + + const getFormattedValue = (value) => { + if (Array.isArray(value)) { + return value.join(''); + } + if (typeof value === 'object' && value !== null) { + return value.slug || ''; + } + return value || ''; + }; + + const formattedSchedule = formData.schedule.map((item) => { + if (typeof item === 'object' && item !== null && item.slug) { + return item.slug; + } + return item; + }); + + const updateFormData = { + id: formData.id, + position: formData.title, + salary: formData.salary, + is_ndfl: getFormattedValue(formData.is_ndfl), + schedule: formattedSchedule, + education: getFormattedValue(formData.education), + description: formData.description, + }; + await updateVacancy(token, updateFormData, updateFormData.id); + onSubmitSuccess(); + onClose(); + } catch (error) { + throw new Error('Network response was not ok'); + } + }; + + const handleDelete = async () => { + try { + const isDeleted = await deleteVacancy(formData.id); + if (isDeleted) { + onDelete(formData.id); + } + } catch (error) { + throw new Error('Ошибка при удалении вакансии'); + } finally { + setIsModalOpen(false); + onClose(); + } + }; + + if (isLoading) { + return null; + } + + return ( +
+
+ + +
+ + {isModalOpen && ( +
+
+
+
+
Вы уверены, что хотите удалить вакансию?
+
Все данные о вакансии будут удалены
+
+
+ + +
+
+
+
+ )} +
+ ); +}; + +export default EditActionButtons; \ No newline at end of file diff --git a/src/components/VacancyCard/EditVacancyForm/EditVacancyForm.jsx b/src/components/VacancyCard/EditVacancyForm/EditVacancyForm.jsx new file mode 100644 index 00000000..c8fc59be --- /dev/null +++ b/src/components/VacancyCard/EditVacancyForm/EditVacancyForm.jsx @@ -0,0 +1,127 @@ +import React, { useState, useEffect } from 'react'; +import '../../../modules/ShelterVacancies/components/AddVacancyForm/AddVacancyForm.scss'; +import { Button } from '../../../ui'; +import DeclarationInput from '../../../ui/DeclarationInput/DeclarationInput'; +import Select from '../../../ui/Select/Select'; +import FormTextarea from '../../../modules/ShelterVacancies/components/AddVacancyForm/components/FormTextarea'; +import useInput from '../../../hooks/useInput'; +import EditActionButtons from './EditActionButtons'; +import * as regex from '../../../utils/regex'; +import * as errorMessage from '../../../utils/errorMessage'; +import { getEducationOptions, getShiftOptions, getSalaryOptions } from '../../../modules/ShelterVacancies/components/AddVacancyForm/constants'; + +// eslint-disable-next-line +const EditVacancyForm = ({ id, title, salary, education, schedule, is_ndfl, description, onDelete, onClose, onSubmitSuccess, isLoading }) => { + const [formData, setFormData] = useState({ + id: id || '', + title: title || '', + salary: salary || '', + education: education || '', + schedule: schedule || '', + // eslint-disable-next-line + is_ndfl: is_ndfl || '', + description: description || '', + }); + + const [educationOptions, setEducationOptions] = useState([]); + const [shiftOptions, setShiftOptions] = useState([]); + const [salaryOptions, setSalaryOptions] = useState([]); + + const jobDescriptionInput = useInput('', { notEmpty: true, maxLength: 3000, regex: regex.TEXT }, errorMessage.VACANCY_DESCRIPTION); + + const handleInputChange = (field, value) => { + // eslint-disable-next-line + setFormData((prevData) => ({ + ...prevData, + [field]: value, + })); + }; + + const handleDescriptionChange1 = (evt) => { + jobDescriptionInput.onChange(evt); + + setFormData((prevData) => { + return { + ...prevData, + description: evt.target.value, + }; + }); + }; + + useEffect(() => { + const fetchOptions = async () => { + const shiftOpts = await getShiftOptions(); + const educationOpts = await getEducationOptions(); + const salaryOpts = getSalaryOptions(); + if (shiftOpts) { + setShiftOptions(shiftOpts); + } + if (educationOpts) { + setEducationOptions(educationOpts); + } + if (salaryOpts) { + setSalaryOptions(salaryOpts); + } + }; + fetchOptions(); + }, []); + + return ( +
+
+ +
+ { handleInputChange('title', e.target.value); } }} + type='text' name='position' required /> + +
+ { handleInputChange('salary', e.target.value); } }} + type='number' name='salaryInput' required placeholder='₽' + /> + +
-
+
@@ -126,4 +126,4 @@ const AddVacancyForm = ({ onChange, onSubmitSuccess }) => { ); }; -export default AddVacancyForm; +export default AddVacancyForm; \ No newline at end of file diff --git a/src/modules/ShelterVacancies/components/AddVacancyForm/AddVacancyForm.scss b/src/modules/ShelterVacancies/components/AddVacancyForm/AddVacancyForm.scss index 92fe583e..3b6917e8 100644 --- a/src/modules/ShelterVacancies/components/AddVacancyForm/AddVacancyForm.scss +++ b/src/modules/ShelterVacancies/components/AddVacancyForm/AddVacancyForm.scss @@ -1,21 +1,41 @@ -.add-vacancy-form__flex-container { - display: flex; - gap: 16px; - justify-content: space-between; -} +$images-path: '../../../../images'; -.add-vacancy-form__container { +.vacancy-form { margin-top: 36px; width: 576px; + + &__container { + display: flex; + gap: 16px; + justify-content: space-between; + } + + &__edit { + display: flex; + gap: 12px; + margin-bottom: 36px; + + &-btn { + background-image: url('#{$images-path}/icons/ic_button_close_editForm.svg'); + background-size: contain; + border: none; + width: 20px; + height: 20px; + background-color: rgba(0, 0, 0, 0); + padding: 0; + margin: 0; + cursor: pointer; + } + } } -.add-shelter-form__submit-buttons { +.shelter-form__submit-buttons { display: flex; gap: 32px; margin-top: 40px; } -.add-vacancy-form__description { +.vacancy-form__description { width: 100%; height: 120px; margin: 0; @@ -34,6 +54,15 @@ } } -.add-vacancy-form__desctioption-title { +.vacancy-form__desctioption-title { margin-bottom: 10px; } + +.btn-edit { + display: flex; + gap: 76px; + + &__buttons { + font-size: 16px; + } +} diff --git a/src/modules/ShelterVacancies/components/AddVacancyForm/components/FormActionButtons.jsx b/src/modules/ShelterVacancies/components/AddVacancyForm/components/FormActionButtons.jsx index b1ebf820..bc06e576 100644 --- a/src/modules/ShelterVacancies/components/AddVacancyForm/components/FormActionButtons.jsx +++ b/src/modules/ShelterVacancies/components/AddVacancyForm/components/FormActionButtons.jsx @@ -1,6 +1,6 @@ import React from 'react'; import Button from '../../../../../ui/Button/Button'; -import sendDataWithToken from './api'; +import { sendVacancy } from './vacanciesAPI'; const FormActionButtons = ({ isSubmitButtonDisabled, onClick, onSubmitSuccess, formValues }) => { const handleSubmit = async () => { @@ -21,7 +21,7 @@ const FormActionButtons = ({ isSubmitButtonDisabled, onClick, onSubmitSuccess, f description: formattedDescription, }; - await sendDataWithToken(token, formData); + await sendVacancy(token, formData); onSubmitSuccess(); } catch (error) { throw new Error('Network response was not ok'); @@ -29,7 +29,7 @@ const FormActionButtons = ({ isSubmitButtonDisabled, onClick, onSubmitSuccess, f }; return ( -
+
@@ -38,4 +38,4 @@ const FormActionButtons = ({ isSubmitButtonDisabled, onClick, onSubmitSuccess, f ); }; -export default FormActionButtons; +export default FormActionButtons; \ No newline at end of file diff --git a/src/modules/ShelterVacancies/components/AddVacancyForm/components/FormTextarea.jsx b/src/modules/ShelterVacancies/components/AddVacancyForm/components/FormTextarea.jsx index f6999ad1..2f90af29 100644 --- a/src/modules/ShelterVacancies/components/AddVacancyForm/components/FormTextarea.jsx +++ b/src/modules/ShelterVacancies/components/AddVacancyForm/components/FormTextarea.jsx @@ -1,11 +1,16 @@ -import React from 'react'; +import React, { useEffect } from 'react'; -const FormTextarea = ({ jobDescriptionInput, handleDescriptionChange }) => { +const FormTextarea = ({ jobDescriptionInput, handleDescriptionChange, initialValues }) => { + useEffect(() => { + if (initialValues) { + jobDescriptionInput.onChange({ target: { value: initialValues } }); + } + }, [initialValues]); return ( <> -

Обязанности*

+

Обязанности*