Commit 7b89ded7 authored by Ermolaev Timur's avatar Ermolaev Timur

#35 Оптимизировал немного код, убрал пролаги при вводе в инпуте

parent e23db0d3
...@@ -34,6 +34,7 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{ ...@@ -34,6 +34,7 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{
newTask.author= user; newTask.author= user;
newTask.accomplish = accomplish; newTask.accomplish = accomplish;
newTask.priority = priority; newTask.priority = priority;
console.log(newTask)
await newTask.save(); await newTask.save();
return res.send({newTask}) return res.send({newTask})
}) })
......
import { Grid } from "@mui/material"; import { Grid } from "@mui/material";
import { memo } from "react";
const CalendarRow = ({children}) => { const CalendarRow = ({children}) => {
return <> return <>
...@@ -12,6 +13,6 @@ const CalendarRow = ({children}) => { ...@@ -12,6 +13,6 @@ const CalendarRow = ({children}) => {
</> </>
}; };
export default CalendarRow; export default memo(CalendarRow);
import { Grid } from "@mui/material"; import { Grid } from "@mui/material";
import { memo } from "react";
const CalendarSmallCell = ({children, xs}) => { const CalendarSmallCell = ({children, xs}) => {
return <> return <>
...@@ -8,4 +9,4 @@ const CalendarSmallCell = ({children, xs}) => { ...@@ -8,4 +9,4 @@ const CalendarSmallCell = ({children, xs}) => {
</> </>
}; };
export default CalendarSmallCell; export default memo(CalendarSmallCell);
\ No newline at end of file \ No newline at end of file
import { Grid, TextField, Typography } from "@mui/material"; import { Grid, TextField, Typography } from "@mui/material";
import { useEffect, useState } from "react"; import { memo, useEffect, useState } from "react";
const CalendarStandartCell = ({children, xs, currentTask, hours, dayNumber, createTaskInCellHandler, handleOpen, modal}) => { const CalendarStandartCell = ({children, xs, hours, dayNumber, createTaskInCellHandler, handleOpen, modal}) => {
const [isThisCell, setIsThisCell] = useState(false) const [isThisCell, setIsThisCell] = useState(false)
useEffect(()=>{ useEffect(()=>{
if(!modal) { if(!modal) {
...@@ -19,11 +19,14 @@ const CalendarStandartCell = ({children, xs, currentTask, hours, dayNumber, cre ...@@ -19,11 +19,14 @@ const CalendarStandartCell = ({children, xs, currentTask, hours, dayNumber, cre
sx={{backgroundColor: 'lightgreen', padding: '10px', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis'}} sx={{backgroundColor: 'lightgreen', padding: '10px', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis'}}
> >
<span> <span>
{currentTask.title} Задача
</span> </span>
</Grid> : null} </Grid> : null}
</Grid> </Grid>
</> </>
}; };
export default CalendarStandartCell; export default memo(CalendarStandartCell, (prevProps, nextProps)=>{
\ No newline at end of file if(!prevProps.modal) return false
if(nextProps.modal) return true
});
\ No newline at end of file
import { Grid, TextField, Typography } from "@mui/material"; import { Grid, TextField, Typography } from "@mui/material";
import React, { useEffect, useState } from "react"; import React, { memo, useState, useEffect} from "react";
const CalendarTask = ({year, month, tasks, day, hours, setCurrentTask, hourFormat, handleOpen, currentTask}) => { const CalendarTask = ({year, month, tasks, day, hours, setCurrentTask, hourFormat, handleOpen, currentTask}) => {
const [thisCellCurrentTask, setThisCellCurrentTask] = useState({}) const [thisCellCurrentTask, setThisCellCurrentTask] = useState({})
...@@ -27,20 +27,13 @@ const CalendarTask = ({year, month, tasks, day, hours, setCurrentTask, hourForma ...@@ -27,20 +27,13 @@ const CalendarTask = ({year, month, tasks, day, hours, setCurrentTask, hourForma
return tasksCell return tasksCell
} }
const tasksCell = getTaskInDayCell(tasks, day, hours) const tasksCell = getTaskInDayCell(tasks, day, hours)
useEffect(()=>{ useEffect(()=>{
if (!currentTask.title) { if (!currentTask.title) {
setThisCellCurrentTask({}) setThisCellCurrentTask({})
} }
}, [currentTask]) }, [currentTask])
const renderText = (i, task) => {
if (thisCellCurrentTask && i === thisCellCurrentTask.i) {
return (<>{currentTask.title}</>)
} else {
return (<>{task.title}</>)
}
}
return (<> return (<>
{tasksCell.length ? tasksCell.map((task, i)=> {tasksCell.length ? tasksCell.map((task, i)=>
...@@ -49,9 +42,9 @@ const CalendarTask = ({year, month, tasks, day, hours, setCurrentTask, hourForma ...@@ -49,9 +42,9 @@ const CalendarTask = ({year, month, tasks, day, hours, setCurrentTask, hourForma
<Grid <Grid
key={task.id} key={task.id}
sx={{backgroundColor: 'lightgreen', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis', padding: '10px', borderBottom: '1px solid rgb(29, 161, 51);;'}} sx={{backgroundColor: 'lightgreen', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis', padding: '10px', borderBottom: '1px solid rgb(29, 161, 51);;'}}
onClick={(e)=>{e.stopPropagation(); setCurrentTask(task); handleOpen(e); setThisCellCurrentTask({...task, i: i})}} onClick={(e)=>{e.stopPropagation(); setCurrentTask(task); handleOpen(e)}}
> >
{renderText(i, task)} {task.title}
</Grid> </Grid>
)} )}
) )
...@@ -59,4 +52,4 @@ const CalendarTask = ({year, month, tasks, day, hours, setCurrentTask, hourForma ...@@ -59,4 +52,4 @@ const CalendarTask = ({year, month, tasks, day, hours, setCurrentTask, hourForma
</>) </>)
}; };
export default CalendarTask; export default memo(CalendarTask);
\ No newline at end of file \ No newline at end of file
import { FormControlLabel, Switch} from "@mui/material"; import { FormControlLabel, Switch} from "@mui/material";
import { useEffect, useState } from "react"; import { memo, useEffect, useState } from "react";
import CalendarRow from "./CalendarRow/CalendarRow"; import CalendarRow from "./CalendarRow/CalendarRow";
import CalendarSmallCell from "./CalendarSmallCell/CalendarSmallCell"; import CalendarSmallCell from "./CalendarSmallCell/CalendarSmallCell";
import CalendarStandartCell from "./CalendarStandartCell.js/CalendarStandartCell"; import CalendarStandartCell from "./CalendarStandartCell.js/CalendarStandartCell";
...@@ -7,11 +7,8 @@ import CalendarTask from "./CalendarTask/CalendarTask"; ...@@ -7,11 +7,8 @@ import CalendarTask from "./CalendarTask/CalendarTask";
import ModalTask from "../UI/ModalTask/ModalTask"; import ModalTask from "../UI/ModalTask/ModalTask";
import MonthCalendarModalContent from "../MonthCalendarModalContent/MonthCalendarModalContent"; import MonthCalendarModalContent from "../MonthCalendarModalContent/MonthCalendarModalContent";
function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, currentTask, setCurrentTask, hourFormat, setHourFormat, onChangeCurrentTaskHandler, sendNewTaskHandler, deleteTaskHandler}) { function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, currentTask, setCurrentTask, hourFormat, setHourFormat, onChangeCurrentTaskHandler, sendNewTaskHandler, deleteTaskHandler, cellSizes, hoursInDay, daysInMonth}) {
const [hoursInDay, setHoursInDay] = useState(['8:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00'])
const [daysInMonth, setDaysInMonth] = useState([])
const [cellSizes, setCellSizes] = useState({})
const [modal, setModal] = useState({open:false, y: 0, x: 0,}); const [modal, setModal] = useState({open:false, y: 0, x: 0,});
const handleOpen = (e) => { const handleOpen = (e) => {
setModal( { setModal( {
...@@ -30,58 +27,6 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, current ...@@ -30,58 +27,6 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, current
}; };
useEffect(()=>{
const cells = hoursInDay.length
const xs = 10.8/cells
setCellSizes(()=>{
return {smallCell: 0.6, standarCell: xs}
})
}, [])
useEffect(()=>{
if (hourFormat) {
const arr = ['8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00','21:00','22:00']
const cells = arr.length
const xs = 10.8/cells
setCellSizes(()=>{
return {smallCell: 0.6, standarCell: xs}
})
setHoursInDay(()=>arr)
} else {
const arr = ['8:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00']
const cells = arr.length
const xs = 10.8/cells
setCellSizes(()=>{
return {smallCell: 0.6, standarCell: xs}
})
setHoursInDay(()=>arr)
}
}, [hourFormat])
useEffect(()=>{
setNewMonthDays()
}, [month])
const getDaysInMonth = () => {
return new Date(year, month + 1, 0).getDate();
}
const getDayOfWeekString = (day) => {
return ["ВС","ПН","ВТ","СР","ЧТ","ПТ","СБ"][day];
}
const getDayOfWeekNumber = (day) => {
return new Date(year, month, day).getDay()
}
const setNewMonthDays = () => {
const newDaysInMonth = []
for (let i = 1; i <= getDaysInMonth(); i++) {
const dayOfWeekNumber = getDayOfWeekNumber(i)
newDaysInMonth.push({dayNumber: i, dayOfWeek: getDayOfWeekString(dayOfWeekNumber)})
}
setDaysInMonth(prevState=>newDaysInMonth)
}
return ( return (
<> <>
...@@ -94,7 +39,7 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, current ...@@ -94,7 +39,7 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, current
labelPlacement="end" labelPlacement="end"
/> />
</CalendarSmallCell> </CalendarSmallCell>
{hoursInDay.map((hours, i)=>{ {hoursInDay?.map((hours, i)=>{
return ( return (
<CalendarStandartCell key={i} xs={cellSizes.standarCell}> <CalendarStandartCell key={i} xs={cellSizes.standarCell}>
{hours} {hours}
...@@ -102,7 +47,7 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, current ...@@ -102,7 +47,7 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, current
) )
})} })}
</CalendarRow> </CalendarRow>
{daysInMonth.map((day, i)=>{ {daysInMonth?.map((day, i)=>{
return ( return (
<CalendarRow <CalendarRow
key={i} key={i}
......
...@@ -4,14 +4,6 @@ import MonthCalendarBody from '../../components/MonthCalendarBody/MonthCalendarB ...@@ -4,14 +4,6 @@ import MonthCalendarBody from '../../components/MonthCalendarBody/MonthCalendarB
import MonthCalendarHeader from '../../components/MonthCalendarHeader/MonthCalendarHeader'; import MonthCalendarHeader from '../../components/MonthCalendarHeader/MonthCalendarHeader';
import { addTask, deleteTask, editTask, fetchCalendarTasks} from '../../store/actions/tasksActions'; import { addTask, deleteTask, editTask, fetchCalendarTasks} from '../../store/actions/tasksActions';
function dateToISOLikeButLocal(date) {
const offsetMs = date.getTimezoneOffset() * 60 * 1000;
const msLocal = date.getTime() - offsetMs;
const dateLocal = new Date(msLocal);
const iso = dateLocal.toISOString();
return iso;
}
function MonthCalendar() { function MonthCalendar() {
const dispatch = useDispatch(); const dispatch = useDispatch();
const { calendarTasks } = useSelector(state => state.tasks); const { calendarTasks } = useSelector(state => state.tasks);
...@@ -20,7 +12,9 @@ function MonthCalendar() { ...@@ -20,7 +12,9 @@ function MonthCalendar() {
const [dateNow, setDateNow] = useState({month: '', year: ''}) const [dateNow, setDateNow] = useState({month: '', year: ''})
const [worker, setWorker] = useState(''); const [worker, setWorker] = useState('');
const [calendarType, setCalendarType] = useState('Месяц'); const [calendarType, setCalendarType] = useState('Месяц');
const [currentTask, setCurrentTask] = useState({}) const [currentTask, setCurrentTask] = useState({title: '', description: '', priority: ''})
const [cellSizes, setCellSizes] = useState({})
useEffect(()=>{ useEffect(()=>{
setDateNow({ setDateNow({
...@@ -30,6 +24,38 @@ function MonthCalendar() { ...@@ -30,6 +24,38 @@ function MonthCalendar() {
dispatch(fetchCalendarTasks()) dispatch(fetchCalendarTasks())
}, [dispatch]) }, [dispatch])
const hoursInDay = useMemo(()=>{
if (hourFormat) {
const arr = ['8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00','21:00','22:00']
const cells = arr.length
const xs = 10.8/cells
setCellSizes(()=>{
return {smallCell: 0.6, standarCell: xs}
})
return arr
} else {
const arr = ['8:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00']
const cells = arr.length
const xs = 10.8/cells
setCellSizes(()=>{
return {smallCell: 0.6, standarCell: xs}
})
return arr
}
}, [hourFormat])
const daysInMonth = useMemo(() => {
const newDaysInMonth = []
const daysInMonthNumber = new Date(dateNow.year, dateNow.month + 1, 0).getDate()
for (let i = 1; i <= daysInMonthNumber; i++) {
const dayOfWeekNumber = new Date(dateNow.year, dateNow.month, i).getDay()
const getDayOfWeekString = ["ВС","ПН","ВТ","СР","ЧТ","ПТ","СБ"][dayOfWeekNumber]
newDaysInMonth.push({dayNumber: i, dayOfWeek: getDayOfWeekString})
}
return newDaysInMonth
}, [dateNow.month, dateNow.year])
const onChangeWorkerHandler = useCallback((event) => { const onChangeWorkerHandler = useCallback((event) => {
setWorker(event.target.value); setWorker(event.target.value);
}, []); }, []);
...@@ -69,8 +95,14 @@ function MonthCalendar() { ...@@ -69,8 +95,14 @@ function MonthCalendar() {
} }
}); });
}, []); }, []);
function dateToISOLikeButLocal(date) {
const createTaskInCellHandler = useCallback((dayNumber, dayHour) => { const offsetMs = date.getTimezoneOffset() * 60 * 1000;
const msLocal = date.getTime() - offsetMs;
const dateLocal = new Date(msLocal);
const iso = dateLocal.toISOString();
return iso;
}
const createTaskInCellHandler = (dayNumber, dayHour) => {
const hour = parseInt(dayHour.split(':')[0]) const hour = parseInt(dayHour.split(':')[0])
let hourDue let hourDue
if (hourFormat) { if (hourFormat) {
...@@ -86,10 +118,9 @@ function MonthCalendar() { ...@@ -86,10 +118,9 @@ function MonthCalendar() {
dateTimeDue: dateToISOLikeButLocal(new Date(dateNow.year, dateNow.month, dayNumber, hourDue, 59)), dateTimeDue: dateToISOLikeButLocal(new Date(dateNow.year, dateNow.month, dayNumber, hourDue, 59)),
} }
setCurrentTask((newTask)) setCurrentTask((newTask))
}, []) }
const sendNewTaskHandler = async () => { const sendNewTaskHandler = async () => {
if (currentTask.id) { if (currentTask.id) {
setCurrentTask(() => { setCurrentTask(() => {
return{ return{
...@@ -104,6 +135,7 @@ function MonthCalendar() { ...@@ -104,6 +135,7 @@ function MonthCalendar() {
...currentTask, ...currentTask,
}} }}
) )
console.log(currentTask)
delete currentTask.infoForCell delete currentTask.infoForCell
await dispatch(addTask(currentTask)) await dispatch(addTask(currentTask))
} }
...@@ -137,7 +169,11 @@ function MonthCalendar() { ...@@ -137,7 +169,11 @@ function MonthCalendar() {
onChangeCurrentTaskHandler={onChangeCurrentTaskHandler} onChangeCurrentTaskHandler={onChangeCurrentTaskHandler}
sendNewTaskHandler={sendNewTaskHandler} sendNewTaskHandler={sendNewTaskHandler}
deleteTaskHandler={deleteTaskHandler} deleteTaskHandler={deleteTaskHandler}
cellSizes={cellSizes}
hoursInDay={hoursInDay}
daysInMonth={daysInMonth}
/> />
</> </>
); );
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment