Commit cce9a6c8 authored by Ermolaev Timur's avatar Ermolaev Timur

#28 реализовал создание таска через бекенд

parent 6a7323c0
...@@ -36,9 +36,9 @@ import { ...@@ -36,9 +36,9 @@ import {
description!: string description!: string
@CreateDateColumn({ name: 'created_at', type: Date, default: new Date() }) @CreateDateColumn({ name: 'created_at', type: Date, default: new Date() })
createdAt!: Date; createdAt!: Date;
@Column({ name: 'dateTimeStart', type: Date,nullable: true }) @Column({ name: 'dateTimeStart', type: 'varchar',nullable: true })
dateTimeStart!: Date | null; dateTimeStart!: Date | null;
@Column({ name: 'dateTimeDue', type: Date,nullable: true }) @Column({ name: 'dateTimeDue', type: 'varchar',nullable: true })
dateTimeDue!: Date | null; dateTimeDue!: Date | null;
@Column({ @Column({
type: "enum", type: "enum",
......
import { Grid, TextField } from "@mui/material"; import { Grid, TextField } from "@mui/material";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useDispatch } from "react-redux";
import { setNewTasksForCells } from "../../store/actions/tasksActions";
function MonthCalendarBody({month, year, tasks, createTaskInCellHandler}) { function MonthCalendarBody({month, year, tasks, createTaskInCellHandler, onChangeCellTaskTitle, setCurrentTask}) {
const [hoursInDay, setHoursInDay] = useState(['8:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00', '6:00']) const [hoursInDay, setHoursInDay] = useState(['8:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00', '6:00'])
const [daysInMonth, setDaysInMonth] = useState([]) const [daysInMonth, setDaysInMonth] = useState([])
...@@ -38,7 +36,7 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler}) { ...@@ -38,7 +36,7 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler}) {
if (year === task.infoForCell.startYear) { if (year === task.infoForCell.startYear) {
if (month + 1 === task.infoForCell.startMonth) { if (month + 1 === task.infoForCell.startMonth) {
if (day.dayNumber === task.infoForCell.startDay && task.infoForCell.startDayOfWeek === day.dayOfWeek ) { if (day.dayNumber === task.infoForCell.startDay && task.infoForCell.startDayOfWeek === day.dayOfWeek ) {
if ((task.infoForCell.endHour <= parseInt(hours.split(':')[0]) || task.infoForCell.startHour <= parseInt(hours.split(':')[0])) && (task.infoForCell.endHour >= parseInt(hours.split(':')[0]))) { if ((task.infoForCell.endHour <= parseInt(hours.split(':')[0]) || task.infoForCell.startHour <= parseInt(hours.split(':')[0])) && (task.infoForCell.endHour > parseInt(hours.split(':')[0]))) {
return task return task
} }
} }
...@@ -47,19 +45,6 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler}) { ...@@ -47,19 +45,6 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler}) {
}) })
return task return task
} }
const onChangeCellTaskTitle = (e, task) => {
const value = e.target.value;
const name = e.target.name;
const { id } = task;
const newTasks = tasks.map(task => {
if (task.id === id) {
return { ...task, [name]: value };
}
return task;
});
setNewTasksForCells(newTasks)
}
return ( return (
<> <>
...@@ -103,7 +88,7 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler}) { ...@@ -103,7 +88,7 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler}) {
key={i} key={i}
item xs={1.2222} item xs={1.2222}
sx={{borderRight: '1px solid black'}} sx={{borderRight: '1px solid black'}}
onClick={()=>{createTaskInCellHandler(day.dayOfWeek, day.dayNumber, hours)}}> onClick={()=>{createTaskInCellHandler(day.dayNumber, hours)}}>
{ task ? { task ?
<Grid key={i} sx={{backgroundColor: 'lightgreen'}}> <Grid key={i} sx={{backgroundColor: 'lightgreen'}}>
<TextField <TextField
...@@ -111,7 +96,8 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler}) { ...@@ -111,7 +96,8 @@ function MonthCalendarBody({month, year, tasks, createTaskInCellHandler}) {
variant="standard" variant="standard"
value={task.title} value={task.title}
name='title' name='title'
onChange={(e)=>{onChangeCellTaskTitle(e, task)}}></TextField> onClick={(e)=>{e.stopPropagation(); setCurrentTask(task)}}
onChange={(e)=>{onChangeCellTaskTitle(e)}}></TextField>
</Grid> : null} </Grid> : null}
</Grid> </Grid>
) )
......
import { Container } from '@mui/material'; import { Container } from '@mui/material';
import moment from 'moment';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import MonthCalendarBody from '../../components/MonthCalendarBody/MonthCalendarBody'; import MonthCalendarBody from '../../components/MonthCalendarBody/MonthCalendarBody';
import MonthCalendarHeader from '../../components/MonthCalendarHeader/MonthCalendarHeader'; import MonthCalendarHeader from '../../components/MonthCalendarHeader/MonthCalendarHeader';
import { fetchTasks} from '../../store/actions/tasksActions'; import { addTask, fetchTasks} from '../../store/actions/tasksActions';
function MonthCalendar() { function MonthCalendar() {
const dispatch = useDispatch(); const dispatch = useDispatch();
...@@ -13,13 +14,14 @@ function MonthCalendar() { ...@@ -13,13 +14,14 @@ function MonthCalendar() {
const [year, setYear] = useState('') const [year, setYear] = useState('')
const [worker, setWorker] = useState(''); const [worker, setWorker] = useState('');
const [calendarType, setCalendarType] = useState('Месяц'); const [calendarType, setCalendarType] = useState('Месяц');
const [currentTask, setCurrentTask] = useState({})
useEffect(()=>{ useEffect(()=>{
setMonth(new Date().getMonth()) setMonth(new Date().getMonth())
setYear(new Date().getFullYear()) setYear(new Date().getFullYear())
dispatch(fetchTasks()) dispatch(fetchTasks())
}, [dispatch]) }, [dispatch])
console.log(tasks)
const onChangeWorkerHandler = (event) => { const onChangeWorkerHandler = (event) => {
setWorker(event.target.value); setWorker(event.target.value);
}; };
...@@ -52,24 +54,29 @@ function MonthCalendar() { ...@@ -52,24 +54,29 @@ function MonthCalendar() {
}) })
} }
const createTaskInCellHandler = (dayOfWeek, dayNumber, dayHour) => { function dateToISOButLocal(date) {
// const newTasks = [...tasksForCell] return date.toLocaleString('sv').replace(' ', 'T');
// const newTask = { }
// id: Date.now(),
// user:"first", const createTaskInCellHandler = (dayNumber, dayHour) => {
// title:"Новая", const hour = parseInt(dayHour.split(':')[0])
// description:"описание задачи11111", const newTask = {
// priority:"A", title:"Задача",
// author:"Ivan", description:"описание",
// executor:"Arman", dateTimeStart: dateToISOButLocal(new Date(year, month, dayNumber, hour)),
// dateTimeStart:`${dayNumber}.${month+1}.${year} ${parseInt(dayHour.split(':')[0])}:00:00`, dateTimeDue: dateToISOButLocal(new Date(year, month, dayNumber, hour + 2)),
// dateTimeDue:`${dayNumber}.${month+1}.${year} ${parseInt(dayHour.split(':')[0]) + 1}:00:00`, }
// } dispatch(addTask(newTask))
// newTasks.push(newTask) setCurrentTask((newTask))
// exampleTasks.push(newTask)
// setNewTasksWithInfoForCell(newTasks)
} }
const onChangeCellTaskTitle = (e) => {
e.stopPropagation()
const value = e.target.value;
const name = e.target.name;
setCurrentTask({ ...currentTask, [name]: value })
}
console.log(tasks)
return ( return (
<> <>
<Container> <Container>
...@@ -89,6 +96,8 @@ function MonthCalendar() { ...@@ -89,6 +96,8 @@ function MonthCalendar() {
year={year} year={year}
tasks={tasks} tasks={tasks}
createTaskInCellHandler={createTaskInCellHandler} createTaskInCellHandler={createTaskInCellHandler}
onChangeCellTaskTitle={onChangeCellTaskTitle}
setCurrentTask={setCurrentTask}
/> />
</Container> </Container>
</> </>
......
...@@ -2,4 +2,6 @@ export const FETCH_TASKS_REQUEST = "FETCH_TASKS_REQUEST"; ...@@ -2,4 +2,6 @@ export const FETCH_TASKS_REQUEST = "FETCH_TASKS_REQUEST";
export const FETCH_TASKS_SUCCESS = "FETCH_TASKS_SUCCESS"; export const FETCH_TASKS_SUCCESS = "FETCH_TASKS_SUCCESS";
export const FETCH_TASKS_FAILURE = "FETCH_TASKS_FAILURE"; export const FETCH_TASKS_FAILURE = "FETCH_TASKS_FAILURE";
export const SET_NEW_TASKS_FOR_CELLS = "SET_NEW_TASKS_FOR_CELLS"; export const ADD_NEW_TASK_REQUEST = "ADD_NEW_TASK_REQUEST";
\ No newline at end of file export const ADD_NEW_TASK_SUCCESS = "ADD_NEW_TASK_SUCCESS";
export const ADD_NEW_TASK_FAILURE = "ADD_NEW_TASK_FAILURE";
\ No newline at end of file
import { FETCH_TASKS_FAILURE, FETCH_TASKS_REQUEST, FETCH_TASKS_SUCCESS, SET_NEW_TASKS_FOR_CELLS } from "../actionTypes/tasksTypes"; import { ADD_NEW_TASK_FAILURE, ADD_NEW_TASK_REQUEST, ADD_NEW_TASK_SUCCESS, FETCH_TASKS_FAILURE, FETCH_TASKS_REQUEST, FETCH_TASKS_SUCCESS} from "../actionTypes/tasksTypes";
import axios from '../../axiosPlanner' import axios from '../../axiosPlanner'
const fetchTasksRequest = () => { const fetchTasksRequest = () => {
...@@ -12,40 +12,45 @@ const fetchTasksSuccess = (tasks) => { ...@@ -12,40 +12,45 @@ const fetchTasksSuccess = (tasks) => {
const fetchTasksFailure = (error) => { const fetchTasksFailure = (error) => {
return {type: FETCH_TASKS_FAILURE, error} return {type: FETCH_TASKS_FAILURE, error}
}; };
export const setNewTasksForCells = (tasks) => {
return {type: SET_NEW_TASKS_FOR_CELLS, tasks}
}
export const fetchTasks = () => { export const fetchTasks = () => {
return async (dispatch) => { return async (dispatch) => {
dispatch(fetchTasksRequest()); dispatch(fetchTasksRequest());
try { try {
const response = await axios.get("/tasks"); const response = await axios.get("/tasks");
const arr = [ dispatch(fetchTasksSuccess(response.data.tasks))
{
accomplish: "opened",
author: {id: 'f0e4a3bf-78fb-465b-9f28-620790750418', name: 'timur', surname: 'ermolaev', displayName: 'ermolaev t.', email: 'loool@mail.ru'},
createdAt: "2022-11-07T11:35:30.937Z",
dateTimeDue: "2022-11-11T14:35:30.937Z",
dateTimeStart: "2022-11-11T11:35:30.937Z",
description: "lol",
id: "2fd828a2-3778-45fd-a11a-00d799db9142",
title : "task1",
}, {
accomplish: "opened",
author: {id: 'f0e4a3bf-78fb-465b-9f28-620790750418', name: 'timur', surname: 'ermolaev', displayName: 'ermolaev t.', email: 'loool@mail.ru'},
createdAt: "2022-11-07T11:35:30.937Z",
dateTimeDue: "2022-11-25T16:00:00.937Z",
dateTimeStart: "2022-11-25T15:35:30.937Z",
description: "lol",
id: "2fd828a2-3778-45fd-a11a-00d799db9142",
title : "task2",
},
]
dispatch(fetchTasksSuccess(arr))
setNewTasksForCells()
} catch (error) { } catch (error) {
dispatch(fetchTasksFailure(error.response.data)); dispatch(fetchTasksFailure(error.response.data));
} }
} }
}
const addTaskRequest = () => {
return {type: ADD_NEW_TASK_REQUEST}
};
const addTaskSuccess = () => {
return {type: ADD_NEW_TASK_SUCCESS}
};
const addTaskFailure = (error) => {
return {type: ADD_NEW_TASK_FAILURE, error}
};
export const addTask = (task) => {
return async (dispatch, getState) => {
dispatch(addTaskRequest());
const token = getState().users?.user?.token;
try {
await axios.post("/tasks", task, {
headers: {
'Authorization': 'aBhHYW8kXUUzjXlxOwGmg'
}
});
dispatch(addTaskSuccess())
dispatch(fetchTasks())
} catch (error) {
dispatch(addTaskFailure(error.response.data));
}
}
} }
\ No newline at end of file
import { FETCH_TASKS_FAILURE, FETCH_TASKS_REQUEST, FETCH_TASKS_SUCCESS, SET_NEW_TASKS_FOR_CELLS } from "../actionTypes/tasksTypes"; import { FETCH_TASKS_FAILURE, FETCH_TASKS_REQUEST, FETCH_TASKS_SUCCESS} from "../actionTypes/tasksTypes";
const initialState = { const initialState = {
tasks: [], tasks: [],
...@@ -11,7 +11,8 @@ const tasksReduсer = (state = initialState, action) => { ...@@ -11,7 +11,8 @@ const tasksReduсer = (state = initialState, action) => {
case FETCH_TASKS_REQUEST: case FETCH_TASKS_REQUEST:
return {...state, loading: true}; return {...state, loading: true};
case FETCH_TASKS_SUCCESS: case FETCH_TASKS_SUCCESS:
const newArr = action.tasks.map((task)=>{ const newArr = []
action.tasks.forEach((task)=>{
if (task.dateTimeStart && task.dateTimeDue) { if (task.dateTimeStart && task.dateTimeDue) {
const dateStart = task.dateTimeStart.split('T')[0] const dateStart = task.dateTimeStart.split('T')[0]
const timeStart = task.dateTimeStart.split('T')[1] const timeStart = task.dateTimeStart.split('T')[1]
...@@ -23,7 +24,7 @@ const tasksReduсer = (state = initialState, action) => { ...@@ -23,7 +24,7 @@ const tasksReduсer = (state = initialState, action) => {
const yearStartNumber = parseInt(dateStart.split('-')[0]) const yearStartNumber = parseInt(dateStart.split('-')[0])
const timeStartHour = parseInt(timeStart.split(':')[0]) const timeStartHour = parseInt(timeStart.split(':')[0])
const timeEndHour = parseInt(timeEnd.split(':')[0]) const timeEndHour = parseInt(timeEnd.split(':')[0])
return {...task, infoForCell: { newArr.push({...task, infoForCell: {
startDay: dayStart, startDay: dayStart,
startDayOfWeek: dayOfWeekStartString, startDayOfWeek: dayOfWeekStartString,
startHour: timeStartHour, startHour: timeStartHour,
...@@ -31,16 +32,12 @@ const tasksReduсer = (state = initialState, action) => { ...@@ -31,16 +32,12 @@ const tasksReduсer = (state = initialState, action) => {
startYear: yearStartNumber, startYear: yearStartNumber,
endHour: timeEndHour, endHour: timeEndHour,
} }
} } )
} else {
return null
} }
}) })
return {...state, loading: false, tasks: newArr}; return {...state, loading: false, tasks: newArr};
case FETCH_TASKS_FAILURE: case FETCH_TASKS_FAILURE:
return {...state, loading: false, error: action.error}; return {...state, loading: false, error: action.error};
case SET_NEW_TASKS_FOR_CELLS:
return {...state, tasks: action.tasks}
default: default:
return state; return state;
} }
......
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