Skip to content

Commit

Permalink
fix: fix merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickoff committed Nov 22, 2023
2 parents 2618d71 + d02bb54 commit 7fd26a9
Show file tree
Hide file tree
Showing 28 changed files with 747 additions and 170 deletions.
104 changes: 104 additions & 0 deletions src/components/VacancyCard/EditVacancyForm/EditActionButtons.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className='shelter-form__submit-buttons'>
<div className='btn-edit'>
<Button className='btn-edit__buttons' disabled={isSubmitButtonDisabled} onClick={handleSubmit}>
Сохранить изменения
</Button>
<Button className='btn-edit__buttons' theme='transparent' onClick={() => { setIsModalOpen(true); }}>
Удалить вакансию
</Button>
</div>

{isModalOpen && (
<div className='modal-overlay'>
<div className='wrapper wrapper__del'>
<div className='modal modal__del'>
<div className='modal__del-text'>
<div className='modal__title modal__del-title standard-font standard-font_type_h3'>Вы уверены, что хотите удалить вакансию?</div>
<div className='modal__descr modal__del-descr standard-font standard-font_type_h3'>Все данные о вакансии будут удалены</div>
</div>
<div className='modal__btn-del'>
<Button className='modal__btn-del-left' theme='accent' onClick={handleDelete}>
<DeleteIcon />
Да, удалить вакансию
</Button>
<Button theme='transparent' onClick={() => { setIsModalOpen(false); }}>Отменить удаление</Button>
</div>
<button
type='button'
className='modal__esc'
onClick={() => { setIsModalOpen(false); }}
/>
</div>
</div>
</div>
)}
</div>
);
};

export default EditActionButtons;
127 changes: 127 additions & 0 deletions src/components/VacancyCard/EditVacancyForm/EditVacancyForm.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<form className='vacancy-form'>
<div className='vacancy-form__edit' >
<button
type='button'
className='vacancy-form__edit-btn'
onClick={onClose}
/>
<Button theme='tertiary' onClick={onClose}>Отменить редактирование вакансии</Button>
</div>
<DeclarationInput
caption='Название вакансии*'
inputState={{ value: formData.title, onChange: (e) => { handleInputChange('title', e.target.value); } }}
type='text' name='position' required />

<div className='vacancy-form__container'>
<DeclarationInput
caption='Заработная плата*'
inputState={{ value: formData.salary, onChange: (e) => { handleInputChange('salary', e.target.value); } }}
type='number' name='salaryInput' required placeholder='₽'
/>
<Select
label='Тип оплаты*'
onChange={handleInputChange}
options={salaryOptions}
id='is_ndfl'
isMulti={false}
initialValues={formData.is_ndfl}
/>
</div>
<div className='vacancy-form__container'>
<Select
label='График работы*'
id='schedule'
isMulti
onChange={handleInputChange}
options={shiftOptions}
initialValues={formData.schedule}
/>
<Select
label='Образование*'
id='education'
isMulti={false}
onChange={handleInputChange}
options={educationOptions}
initialValues={formData.education}
/>
</div>
<FormTextarea
jobDescriptionInput={jobDescriptionInput}
handleDescriptionChange={handleDescriptionChange1}
initialValues={formData.description}
/>
<EditActionButtons formData={formData} onDelete={onDelete} onSubmitSuccess={onSubmitSuccess} onClose={onClose} isLoading={isLoading} />
</form>
);
};

export default EditVacancyForm;
107 changes: 97 additions & 10 deletions src/components/VacancyCard/VacancyCard.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
import React from 'react';
import React, { useState } from 'react';
import { useOutletContext } from 'react-router-dom';
import './VacancyCard.scss';
import EditIcon from '../../images/EditIcon/EditIcon';
import DeleteIcon from '../../images/DeleteIcon/DeleteIcon';
import { Button } from '../../ui';
import EditVacancyForm from './EditVacancyForm/EditVacancyForm';
import { deleteVacancy } from '../../modules/ShelterVacancies/components/AddVacancyForm/components/vacanciesAPI';

const VacancyCard = ({ title, salary, schedule, description, education, isLoading }) => {
// eslint-disable-next-line
const VacancyCard = ({ id, title, salary, schedule, description, education, is_ndfl, isLoading, onDelete, onSubmitSuccess, openEditForm, isOpenVacancyForm }) => {
const { isOwner } = useOutletContext();
const [isModalOpen, setIsModalOpen] = useState(false);
const [isEditFormOpen, setIsEditFormOpen] = useState(false);
// eslint-disable-next-line
const convertedIsNdfl = is_ndfl === 'ndfl' ? { slug: 'ndfl', name: 'с НДФЛ' } : { slug: 'no_ndfl', name: 'на руки' };

const handleEditFormClose = () => {
setIsEditFormOpen(false);
};

const handleEditFormOpen = () => {
openEditForm({
id,
title,
salary,
schedule,
description,
education,
// eslint-disable-next-line
is_ndfl: convertedIsNdfl,
});
};

const handleDelete = async () => {
try {
const isDeleted = await deleteVacancy(id);
if (isDeleted) {
onDelete(id);
}
} catch (error) {
throw new Error('Ошибка при удалении вакансии');
} finally {
setIsModalOpen(false);
}
};

if (isLoading) {
return null;
Expand All @@ -17,24 +55,73 @@ const VacancyCard = ({ title, salary, schedule, description, education, isLoadin
<h3 className='vacancy-card__title'>{title}</h3>

{isOwner && (
<>
{/* TODO <EditIcon /> функционал реализую в след PR */}
<button type='button' className='vacancy-card__icon-button vacancy-card__icon-button_edit'>
<div className='vacancy-card__icon'>
<button
type='button'
className='vacancy-card__icon-button vacancy-card__icon-button_edit'
onClick={handleEditFormOpen}
disabled={isOpenVacancyForm}
>
<EditIcon />
</button>
{/* TODO DeleteIcon функционал реализую в след PR */}
<button type='button' className='vacancy-card__icon-button vacancy-card__title-button_delete'>
<button
type='button'
className='vacancy-card__icon-button vacancy-card__title-button_delete'
onClick={() => { setIsModalOpen(true); }}
>
<DeleteIcon />
</button>
</>
</div>
)}
</div>
<p className='vacancy-card__text'>{`ЗП: ${salary}`}</p>
{/* eslint-disable-next-line */}
<p className='vacancy-card__text'>{`ЗП: ${salary} рублей ${is_ndfl === 'ndfl' ? 'с НДФЛ' : 'на руки'}`}</p>
<p className='vacancy-card__text'>{`График работы: ${schedule.map((item) => { return item.name; }).join(', ')}`}</p>
<p className='vacancy-card__text'>{`Образование: ${education.name}`}</p>
<p className='vacancy-card__text'>{`Обязанности: ${description}`}</p>

{isEditFormOpen && (
<EditVacancyForm
id={id}
title={title}
salary={salary}
education={education}
schedule={schedule}
is_ndfl={convertedIsNdfl}
description={description}
onDelete={handleDelete}
onClose={handleEditFormClose}
onSubmitSuccess={onSubmitSuccess}
isLoading={isLoading}
/>
)}

{isModalOpen && (
<div className='modal-overlay'>
<div className='wrapper wrapper__del'>
<div className='modal modal__del'>
<div className='modal__del-text'>
<div className='modal__title modal__del-title standard-font standard-font_type_h3'>Вы уверены, что хотите удалить вакансию?</div>
<div className='modal__descr modal__del-descr standard-font standard-font_type_h3'>Все данные о вакансии будут удалены</div>
</div>
<div className='modal__btn-del'>
<Button className='modal__btn-del-left' theme='accent' onClick={handleDelete}>
<DeleteIcon />
Да, удалить вакансию
</Button>
<Button theme='transparent' onClick={() => { setIsModalOpen(false); }}>Отменить удаление</Button>
</div>
<button
type='button'
className='modal__esc'
onClick={() => { setIsModalOpen(false); }}
/>
</div>
</div>
</div>
)}
</li>
);
};

export default VacancyCard;
export default VacancyCard;
Loading

0 comments on commit 7fd26a9

Please sign in to comment.