Commit bc35da21 authored by Ermolaev Timur's avatar Ermolaev Timur

#56 Почти реализовал drug and drop

parent 343bd71c
import { Grid } from "@mui/material"; import { Grid } from "@mui/material";
import { memo, useMemo } from "react"; import { memo, useMemo, useState } from "react";
import { useDispatch } from "react-redux";
import CalendarStandartCell from "../CalendarStandartCell.js/CalendarStandartCell"; import CalendarStandartCell from "../CalendarStandartCell.js/CalendarStandartCell";
import CalendarTask from "../CalendarTask/CalendarTask"; import CalendarTask from "../CalendarTask/CalendarTask";
const CalendarRowDay = ({xs, hoursInDay, createTaskInCellHandler, currentTask, handleOpen, modal, setCurrentTask, year, month, tasks, day, hourFormat}) => { const CalendarRowDay = ({xs, hoursInDay, createTaskInCellHandler, currentTask, handleOpen, modal, setCurrentTask, year, month, tasks, day, hourFormat, setCurrentLine, currentLine, dragTaskHandler}) => {
const hours = useMemo(()=>{ const hours = useMemo(()=>{
return hoursInDay.map((hour)=>parseInt(hour.split(':')[0]))}, return hoursInDay.map((hour)=>parseInt(hour.split(':')[0]))},
[hoursInDay]) [hoursInDay])
...@@ -101,11 +101,11 @@ const CalendarRowDay = ({xs, hoursInDay, createTaskInCellHandler, currentTask, h ...@@ -101,11 +101,11 @@ const CalendarRowDay = ({xs, hoursInDay, createTaskInCellHandler, currentTask, h
const boxes = [] const boxes = []
for (let i = 0; i < line.length; i++) { for (let i = 0; i < line.length; i++) {
if (!isNaN(line[i])) { if (!isNaN(line[i])) {
if (boxes[boxes.length -1]?.task === null) { // if (boxes[boxes.length -1]?.task === null) {
boxes[boxes.length -1].xs += xs // boxes[boxes.length -1].xs += xs
} else { // } else {
boxes.push({xs: xs, task: null}) boxes.push({xs: xs, task: null})
} // }
} else { } else {
const task = sortedTasks[line[i].split('-')[1]] const task = sortedTasks[line[i].split('-')[1]]
const taskIsThere = boxes.find((taskFind)=>{ const taskIsThere = boxes.find((taskFind)=>{
...@@ -122,6 +122,18 @@ const CalendarRowDay = ({xs, hoursInDay, createTaskInCellHandler, currentTask, h ...@@ -122,6 +122,18 @@ const CalendarRowDay = ({xs, hoursInDay, createTaskInCellHandler, currentTask, h
} }
} }
const dragOverHandler = (e) => {
e.preventDefault();
}
const dropHandler = (e, currentHour) => {
e.stopPropagation()
e.preventDefault();
dragTaskHandler(day.dayNumber, currentHour)
}
console.log(sortedTasks)
console.log(linesInDay)
return <> return <>
<Grid <Grid
container container
...@@ -129,13 +141,16 @@ const CalendarRowDay = ({xs, hoursInDay, createTaskInCellHandler, currentTask, h ...@@ -129,13 +141,16 @@ const CalendarRowDay = ({xs, hoursInDay, createTaskInCellHandler, currentTask, h
xs={10.8} xs={10.8}
align='center' align='center'
> >
{hoursInDay.map((hour, i)=>{ {hoursInDay.map((hour, i)=>{
return ( return (
<CalendarStandartCell <CalendarStandartCell
linesInDay={linesInDay}
key={i} key={i}
item xs={xs} item xs={xs}
createTaskInCellHandler={createTaskInCellHandler} createTaskInCellHandler={createTaskInCellHandler}
hours={hour} hours={hour}
dragTaskHandler={dragTaskHandler}
dayNumber={day.dayNumber} dayNumber={day.dayNumber}
currentTask={currentTask} currentTask={currentTask}
handleOpen={handleOpen} handleOpen={handleOpen}
...@@ -148,18 +163,32 @@ const CalendarRowDay = ({xs, hoursInDay, createTaskInCellHandler, currentTask, h ...@@ -148,18 +163,32 @@ const CalendarRowDay = ({xs, hoursInDay, createTaskInCellHandler, currentTask, h
{linesInDay?.map((line, i)=>{ {linesInDay?.map((line, i)=>{
const boxes = getBoxesInLine(line) const boxes = getBoxesInLine(line)
return( return(
<Grid key={i} container sx={{height: '35px', backgroundColor: 'rgb(0,0,0,0)', marginTop: i === 0 ? '-35px' : 0, marginBottom: '5px'}}> <Grid key={i} container sx={{height: '35px', backgroundColor: 'rgb(0,0,0,0)', marginBottom: '5px'}}>
{boxes.map((box)=>{ {boxes.map((box, index)=>{
if (box.task) { if (box.task) {
return (<Grid item xs={box.xs} sx={{height: '35px', marginBottom: '5px'}}> return (<Grid
item xs={box.xs}
sx={{height: '35px', marginBottom: '5px'}}
onDragOver={(e)=>{dragOverHandler(e)}}
onDrop={(e)=>{dropHandler(e, parseInt(hours[index]))}
}>
<CalendarTask <CalendarTask
dragTaskHandler={dragTaskHandler}
setCurrentLine={()=>{setCurrentLine(day.dayNumber)}}
currentTask={currentTask}
currentLine={currentLine}
hour={parseInt(hours[index])}
line={day.dayNumber}
task={box.task} task={box.task}
setCurrentTask={setCurrentTask} setCurrentTask={setCurrentTask}
handleOpen={handleOpen} handleOpen={handleOpen}
/> />
</Grid>) </Grid>)
} else { } else {
return (<Grid item xs={box.xs} sx={{height: '35px', backgroundColor: 'rgb(0,0,0,0)'}}> return (<Grid
onDragOver={(e)=>{dragOverHandler(e)}}
onDrop={(e)=>{dropHandler(e, parseInt(hours[index]))}}
item xs={box.xs} sx={{height: '35px', backgroundColor: 'rgb(0,0,0,0)'}}>
</Grid>) </Grid>)
} }
...@@ -171,3 +200,4 @@ const CalendarRowDay = ({xs, hoursInDay, createTaskInCellHandler, currentTask, h ...@@ -171,3 +200,4 @@ const CalendarRowDay = ({xs, hoursInDay, createTaskInCellHandler, currentTask, h
}; };
export default memo(CalendarRowDay); export default memo(CalendarRowDay);
import { Grid} from "@mui/material"; import { Grid} from "@mui/material";
import { memo, useEffect, useState } from "react"; import { memo, useEffect, useState } from "react";
const CalendarStandartCell = ({children, xs, hours, dayNumber, createTaskInCellHandler, handleOpen, modal, divRef }) => { const CalendarStandartCell = ({children, xs, hours, dayNumber, createTaskInCellHandler, handleOpen, modal, divRef, dragTaskHandler, linesInDay}) => {
const [isThisCell, setIsThisCell] = useState(false) const [isThisCell, setIsThisCell] = useState(false)
useEffect(()=>{ useEffect(()=>{
if(!modal) { if(!modal) {
...@@ -9,11 +9,23 @@ const CalendarStandartCell = ({children, xs, hours, dayNumber, createTaskInCell ...@@ -9,11 +9,23 @@ const CalendarStandartCell = ({children, xs, hours, dayNumber, createTaskInCell
} }
}, [modal]) }, [modal])
const dragOverHandler = (e) => {
e.preventDefault();
}
const dropHandler = (e) => {
e.stopPropagation()
e.preventDefault();
dragTaskHandler(dayNumber, parseInt(hours.split(':')[0]))
}
return <> return <>
<Grid <Grid
item xs={xs} item xs={xs}
sx={{position: 'relative', height: '35px'}} sx={{position: 'relative', height: linesInDay?.length ? `${35*linesInDay}px` : '35px'}}
onClick={(e)=>{createTaskInCellHandler(dayNumber, hours); setIsThisCell(true); handleOpen(e)}} onClick={(e)=>{createTaskInCellHandler(dayNumber, hours); setIsThisCell(true); handleOpen(e)}}
onDragOver={(e)=>{dragOverHandler(e)}}
onDrop={(e)=>{dropHandler(e)}}
> >
{children} {children}
{isThisCell ? {isThisCell ?
......
import { Grid} from "@mui/material"; import { Grid} from "@mui/material";
import React, { memo} from "react"; import React, { memo, useEffect, useState} from "react";
const CalendarTask = ({setCurrentTask, handleOpen, task}) => { const CalendarTask = ({setCurrentTask, handleOpen, task, line, setCurrentLine, currentLine, currentTask, dragTaskHandler, hour}) => {
const [color, setColor] = useState('')
useEffect(() => {
if (task.priority) {
if (task.priority === 'A') setColor('rgb(32, 138, 250)')
if (task.priority === 'B') setColor('lightgreen')
if (task.priority === 'C') setColor('yellow')
} else {
setColor('rgb(171, 157, 157);')
}
}, [task])
const onClickTaskHandler = (e, task) => { const onClickTaskHandler = (e, task) => {
...@@ -12,9 +23,36 @@ const CalendarTask = ({setCurrentTask, handleOpen, task}) => { ...@@ -12,9 +23,36 @@ const CalendarTask = ({setCurrentTask, handleOpen, task}) => {
handleOpen(e) handleOpen(e)
} }
const dragOverHandler = (e) => {
e.preventDefault();
e.target.style.boxShadow = '0 4px 3px grey'
}
const dragLeaveHandler = (e) => {
e.target.style.boxShadow = 'none'
}
const dragStartHandler = (e, line, task) => {
setCurrentLine()
setCurrentTask(task);
}
const dragEndHandler = (e) => {
e.target.style.boxShadow = 'none'
}
const dropHandler = (e, line, task) => {
e.stopPropagation()
e.preventDefault();
console.log(hour)
dragTaskHandler(line, hour)
}
return (<> return (<>
<Grid <Grid
sx={{ position: 'relative', height: '30px', backgroundColor: 'lightgreen', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis', borderRadius: '10px', margin: '5px 10px', display: 'flex', justifyContent: 'flex-start', alignItems: 'center', paddingLeft: '5px', zIndex: '5'}} draggable={true}
onDragOver={(e)=>{dragOverHandler(e)}}
onDragLeave={(e)=>{dragLeaveHandler(e)}}
onDragStart={(e)=>{dragStartHandler(e, line, task)}}
onDragEnd={(e)=>{dragEndHandler(e)}}
onDrop={(e)=>{dropHandler(e, line, task)}}
sx={{ position: 'relative', height: '30px', backgroundColor: color, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis', borderRadius: '10px', margin: '5px 10px', display: 'flex', justifyContent: 'flex-start', alignItems: 'center', paddingLeft: '5px', zIndex: '5'}}
onClick={(e)=>{onClickTaskHandler(e, task)}} onClick={(e)=>{onClickTaskHandler(e, task)}}
> >
<span> <span>
......
import { FormControlLabel, Switch} from "@mui/material"; import { FormControlLabel, Switch} from "@mui/material";
import { useEffect, useRef, useState } from "react"; import { useCallback, useEffect, useMemo, useRef, 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,8 +7,10 @@ import ModalTask from "../UI/ModalTask/ModalTask"; ...@@ -7,8 +7,10 @@ import ModalTask from "../UI/ModalTask/ModalTask";
import MonthCalendarModalContent from "../MonthCalendarModalContent/MonthCalendarModalContent"; import MonthCalendarModalContent from "../MonthCalendarModalContent/MonthCalendarModalContent";
import CalendarRowDay from "./CalendarRowDay/CalendarRowDay"; import CalendarRowDay from "./CalendarRowDay/CalendarRowDay";
function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, currentTask, setCurrentTask, hourFormat, setHourFormat, onChangeCurrentTaskHandler, sendNewTaskHandler, deleteTaskHandler, cellSizes, hoursInDay, daysInMonth}) {
function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, currentTask, setCurrentTask, hourFormat, setHourFormat, onChangeCurrentTaskHandler, sendNewTaskHandler, deleteTaskHandler, cellSizes, hoursInDay, daysInMonth, dragTaskHandler}) {
const [currentLine, setCurrentLine] = 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( {
...@@ -21,19 +23,22 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, current ...@@ -21,19 +23,22 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, current
xDiv: e.target.offsetWidth, xDiv: e.target.offsetWidth,
}) })
}; };
const handleClose = () => { const handleClose = () => {
setModal({...modal, open: false}) setModal({...modal, open: false})
setCurrentTask({}) setCurrentTask({})
}; };
const divRef = useRef(null) const divRef = useRef(null)
const [divHeight, setDivHeight] = useState('') const [divHeight, setDivHeight] = useState('')
useEffect(() => { useEffect(() => {
if (divRef) { if (divRef) {
setDivHeight(()=>{ setDivHeight(()=>{
return divRef.current?.offsetHeight return divRef.current?.offsetHeight
}) })
} }
}, [divRef.current?.offsetHeight, hourFormat, month, tasks]); }, [divRef.current?.offsetHeight, hourFormat, month, tasks]);
return ( return (
<div ref={divRef} style={{marginBottom: '30px'}}> <div ref={divRef} style={{marginBottom: '30px'}}>
...@@ -62,11 +67,14 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, current ...@@ -62,11 +67,14 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, current
<CalendarSmallCell xs={cellSizes.smallCell}>{day.dayNumber}</CalendarSmallCell> <CalendarSmallCell xs={cellSizes.smallCell}>{day.dayNumber}</CalendarSmallCell>
<CalendarSmallCell xs={cellSizes.smallCell}>{day.dayOfWeek}</CalendarSmallCell> <CalendarSmallCell xs={cellSizes.smallCell}>{day.dayOfWeek}</CalendarSmallCell>
<CalendarRowDay <CalendarRowDay
dragTaskHandler={dragTaskHandler}
xs={cellSizes.dayCell} xs={cellSizes.dayCell}
createTaskInCellHandler={createTaskInCellHandler} createTaskInCellHandler={createTaskInCellHandler}
hoursInDay={hoursInDay} hoursInDay={hoursInDay}
currentTask={currentTask} currentTask={currentTask}
handleOpen={handleOpen} handleOpen={handleOpen}
currentLine={currentLine}
setCurrentLine={setCurrentLine}
modal={modal.open} modal={modal.open}
setCurrentTask={setCurrentTask} setCurrentTask={setCurrentTask}
year={year} year={year}
...@@ -74,7 +82,8 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, current ...@@ -74,7 +82,8 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, current
tasks={tasks} tasks={tasks}
day={day} day={day}
hourFormat={hourFormat} hourFormat={hourFormat}
/> >
</CalendarRowDay>
</CalendarRow> </CalendarRow>
) )
})} })}
......
...@@ -101,6 +101,7 @@ function MonthCalendar() { ...@@ -101,6 +101,7 @@ function MonthCalendar() {
const iso = dateLocal.toISOString(); const iso = dateLocal.toISOString();
return iso; return iso;
} }
const createTaskInCellHandler = (dayNumber, dayHour) => { const createTaskInCellHandler = (dayNumber, dayHour) => {
const hour = parseInt(dayHour.split(':')[0]) const hour = parseInt(dayHour.split(':')[0])
let hourDue let hourDue
...@@ -119,10 +120,25 @@ function MonthCalendar() { ...@@ -119,10 +120,25 @@ function MonthCalendar() {
setCurrentTask((newTask)) setCurrentTask((newTask))
} }
const dragTaskHandler = async (dayNumber, hour) => {
console.log(dayNumber, hour)
const timeEnd = currentTask.dateTimeDue.split('T')[1]
const timeEndHour = parseInt(timeEnd.split(':')[0])
const timeStart = currentTask.dateTimeStart.split('T')[1]
const timeStartHour = parseInt(timeStart.split(':')[0])
const hourDiff = timeEndHour - timeStartHour
const due = dateToISOLikeButLocal(new Date(dateNow.year, dateNow.month, dayNumber, hour + hourDiff, 59))
const start = dateToISOLikeButLocal(new Date(dateNow.year, dateNow.month, dayNumber, hour, 0))
await dispatch(editTask({
...currentTask,
dateTimeStart: start,
dateTimeDue: due
}))
}
const sendNewTaskHandler = async () => { const sendNewTaskHandler = async () => {
if (currentTask.id) { if (currentTask.id) {
delete currentTask.infoForCell delete currentTask.infoForCell
console.log(currentTask)
setCurrentTask(() => { setCurrentTask(() => {
return{ return{
...currentTask, ...currentTask,
...@@ -171,6 +187,7 @@ function MonthCalendar() { ...@@ -171,6 +187,7 @@ function MonthCalendar() {
cellSizes={cellSizes} cellSizes={cellSizes}
hoursInDay={hoursInDay} hoursInDay={hoursInDay}
daysInMonth={daysInMonth} daysInMonth={daysInMonth}
dragTaskHandler={dragTaskHandler}
/> />
</> </>
......
...@@ -95,11 +95,8 @@ const editTaskFailure = (error) => { ...@@ -95,11 +95,8 @@ const editTaskFailure = (error) => {
export const editTask = (task) => { export const editTask = (task) => {
return async (dispatch, getState) => { return async (dispatch, getState) => {
dispatch(editTaskRequest()); dispatch(editTaskRequest());
// const token = getState().users?.user?.token;
try { try {
console.log('task' , task) await axios.put("/tasks", task);
const r=await axios.put("/tasks/", task);
console.log(r)
dispatch(editTaskSuccess()) dispatch(editTaskSuccess())
dispatch(fetchAllTasks()) dispatch(fetchAllTasks())
dispatch(fetchCalendarTasks()) dispatch(fetchCalendarTasks())
......
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