guild_front/src/components/ReportForm/ReportForm.js

205 lines
7.8 KiB
JavaScript
Raw Normal View History

2023-01-09 20:53:23 +03:00
import React, {useState} from 'react'
import {useSelector} from 'react-redux'
import {Link} from 'react-router-dom'
2023-01-09 20:53:23 +03:00
import {Loader} from '../Loader/Loader'
import {currentMonthAndDay} from '../Calendar/calendarHelper'
import {Footer} from "../Footer/Footer";
import {ProfileHeader} from "../ProfileHeader/ProfileHeader";
import {useRequest} from "../../hooks/useRequest";
2023-01-09 20:53:23 +03:00
import {getReportDate} from '../../redux/reportSlice'
2021-11-30 17:00:58 +03:00
import calendarIcon from '../../images/calendar_icon.png'
import ellipse from '../../images/ellipse.png'
import remove from '../../images/remove.png'
import addIcon from '../../images/addIcon.png'
2022-12-26 15:12:01 +03:00
import arrow from "../../images/right-arrow.png";
import './reportForm.scss'
2021-11-30 17:00:58 +03:00
2022-12-26 15:12:01 +03:00
const getCreatedDate = (day) => {
if (day) {
return `${new Date(day).getFullYear()}-${new Date(day).getMonth() + 1}-${new Date(day).getDate()}`
} else {
const date = new Date();
2023-01-09 20:53:23 +03:00
const dd = String(date.getDate()).padStart(2, '0');
const mm = String(date.getMonth() + 1).padStart(2, '0');
const yyyy = date.getFullYear();
2022-12-26 15:12:01 +03:00
return `${yyyy}-${mm}-${dd}`
}
2023-01-09 20:53:23 +03:00
};
2021-06-21 17:41:26 +03:00
const ReportForm = () => {
2023-01-09 20:53:23 +03:00
const reportDate = useSelector(getReportDate);
2022-10-19 15:41:11 +03:00
const {apiRequest} = useRequest();
2023-01-09 20:53:23 +03:00
const [isFetching, setIsFetching] = useState(false);
const [reportSuccess, setReportSuccess] = useState(false);
2021-06-22 18:00:52 +03:00
2023-01-09 20:53:23 +03:00
const [inputs, setInputs] = useState([{task: '', hours_spent: '', minutes_spent: 0}]);
2021-11-30 17:00:58 +03:00
const [troublesInputValue, setTroublesInputValue] = useState('');
const [scheduledInputValue, setScheduledInputValue] = useState('');
2021-06-22 18:00:52 +03:00
2021-11-30 17:00:58 +03:00
const addInput = () => {
2023-01-09 20:53:23 +03:00
setInputs((prev) => [...prev, {task: '', hours_spent: '', minutes_spent: 0}])
};
2021-06-22 18:00:52 +03:00
2023-01-09 20:53:23 +03:00
const totalHours = inputs.reduce((a, b) => a + b.hours_spent, 0);
2022-10-19 15:41:11 +03:00
2021-11-30 17:00:58 +03:00
const deleteInput = (indexRemove) => {
2022-10-19 15:41:11 +03:00
if (indexRemove !== 0) {
2021-11-30 17:00:58 +03:00
setInputs((prev) => prev.filter((el, index) => index !== indexRemove))
2021-06-29 17:58:15 +03:00
}
2023-01-09 20:53:23 +03:00
};
2021-06-22 18:00:52 +03:00
const handler = () => {
apiRequest('/reports/create', {
method: 'POST',
body: {
tasks: inputs,
difficulties: troublesInputValue,
tomorrow: scheduledInputValue,
created_at: getCreatedDate(reportDate),
status: 1,
},
}).then((res) => {
if (res.status === 200) {
setReportSuccess(true);
setTimeout(() => setReportSuccess(false), 2000)
}
setInputs(() => []);
setTroublesInputValue('');
setScheduledInputValue('');
setIsFetching(false);
setInputs(() => [{task: '', hours_spent: '', minutes_spent: 0}]);
})
};
2021-06-21 17:41:26 +03:00
return (
2023-01-09 20:53:23 +03:00
<section className='report-form'>
2023-01-13 15:53:07 +03:00
<ProfileHeader/>
<div className='container'>
<h2 className='summary__title'>Ваши отчеты - <span>добавить отчет</span></h2>
<div>
<div className='report__head'>
2023-01-16 20:38:33 +03:00
<Link className='calendar__back' to={`/profile/calendar`}>
2023-01-16 16:28:56 +03:00
<img src={arrow} alt=''/><p>Вернуться</p>
2023-01-13 15:53:07 +03:00
</Link>
</div>
</div>
<div className='report-form__content'>
2023-01-09 20:53:23 +03:00
<div className='report-form__block'>
<div className='report-form__block-title'>
2023-01-13 15:53:07 +03:00
<h2>Добавление отчета за день</h2>
2023-01-09 20:53:23 +03:00
<h3>Дата заполнения отчета:</h3>
</div>
<div className='report-form__block-img'>
<img
className='report-form__calendar-icon'
src={calendarIcon}
alt=''
/>
{/*{currentMonthAndDayReportPage()}*/}
{reportDate ? currentMonthAndDay(reportDate) : getCreatedDate()}
</div>
<div className='report-form__task-list'>
<img src={ellipse} alt=''/>
<span>Какие задачи были выполнены?</span>
</div>
2021-06-21 17:41:26 +03:00
</div>
2021-06-22 18:00:52 +03:00
2023-01-13 15:53:07 +03:00
<div className='row'>
<div className='col-8'>
<div className='report-form__task-header'>
<p className='report-form__task-title--description'>
Краткое описание задачи
</p>
<p className='report-form__task-title--hours'>Количество часов</p>
</div>
2021-11-30 17:00:58 +03:00
2023-01-13 15:53:07 +03:00
{inputs.map((input, index) => {
return (
<form id={'input'} key={`input__${index}`} className='report-form__task-form'>
<div className='report-form__task-number'>
{index + 1}.
</div>
<div className='report-form__task-input report-form__task-input--description'>
<input name='text' type='text' onChange={e => setInputs(inputs.map((input, inputIndex) => {
return index === inputIndex
? {
...input,
task: e.target.value
}
: input
}))}/>
</div>
<div className='report-form__task-input report-form__task-input--hours'>
<input name='number' type='number' min='1'
onChange={e => setInputs(inputs.map((input, inputIndex) => {
return index === inputIndex
? {
...input,
hours_spent: Number(e.target.value)
}
: input
}))}/>
</div>
<div className='report-form__task-remove'>
<img onClick={() => deleteInput(index)} src={remove} alt=''/>
</div>
</form>
)
})}
2021-11-30 17:00:58 +03:00
2023-01-13 15:53:07 +03:00
<div className='report-form__form-add'>
<img onClick={addInput} src={addIcon} alt=''/>
<span>Добавить еще </span>
</div>
</div>
<div className='col-4'></div>
2023-01-09 20:53:23 +03:00
</div>
2021-06-22 18:00:52 +03:00
2023-01-09 20:53:23 +03:00
<div className='row'>
<div className='col-12'>
<div className='report-form__input-box'>
<div className='report-form__troubles'>
<img src={ellipse} alt=''/>
<span>Какие сложности возникли?</span>
</div>
<input type='text' value={troublesInputValue} onChange={e => setTroublesInputValue(e.target.value)}/>
<div className='report-form__scheduled'>
<img src={ellipse} alt=''/>
<span>Что планируется сделать завтра?</span>
</div>
<input type='text' value={scheduledInputValue} onChange={e => setScheduledInputValue(e.target.value)}/>
2021-06-22 18:00:52 +03:00
</div>
</div>
</div>
2023-01-09 20:53:23 +03:00
<div className='row'>
<div className='col-12'>
<div className='report-form__footer'>
<button className='report-form__footer-btn' onClick={() => handler()}>
2023-01-09 20:53:23 +03:00
{isFetching ? <Loader/> : 'Отправить'}
</button>
<p className='report-form__footer-text'>
Всего за день : <span>{totalHours} часов</span>
</p>
{reportSuccess &&
2022-10-19 15:41:11 +03:00
<p className='report-form__footer-done'>Отчет отправлен</p>
2023-01-09 20:53:23 +03:00
}
</div>
2021-06-22 18:00:52 +03:00
</div>
2023-01-09 20:53:23 +03:00
</div>
2021-06-22 18:00:52 +03:00
</div>
</div>
2023-01-13 15:53:07 +03:00
<Footer/>
2023-01-09 20:53:23 +03:00
</section>
2021-11-30 17:00:58 +03:00
)
2023-01-09 20:53:23 +03:00
};
2021-06-21 17:41:26 +03:00
2021-11-30 17:00:58 +03:00
export default ReportForm