diff --git a/src/KBBoard.js b/src/KBBoard.js index b59b347..c581fb4 100644 --- a/src/KBBoard.js +++ b/src/KBBoard.js @@ -1,29 +1,355 @@ -import { useState, useEffect } from 'react'; -import { useParams } from 'react-router-dom'; +import { useState, useEffect, useCallback } from 'react'; +import { useParams, useNavigate } from 'react-router-dom'; import axios from 'axios'; import Header from './Header'; +import './css/Board.css'; const KBBoard = () => { + const navigate = useNavigate(); const { id } = useParams(); - const [user, setUser] = useState(null); const [error, setError] = useState(''); + const [info, setInfo] = useState({}); + const [categories, setCategories] = useState([]); + + const [crTask, setCrTask] = useState(false); + const [crCateg, setCrCateg] = useState(false); + const [edTask, setEdTask] = useState(false); + const [edCateg, setEdCateg] = useState(false); + const [delTask, setDelTask] = useState(false); + const [delCateg, setDelCateg] = useState(false); + const [categoryTitle, setCategoryTitle] = useState(''); + const [categoryPosition, setCategoryPosition] = useState(null); + const [taskTitle, setTaskTitle] = useState(''); + const [taskDescription, setTaskDescription] = useState(''); + const [taskPosition, setTaskPosition] = useState(null); + const [taskCategory, setTaskCategory] = useState(null); + const [taskCategori, setTaskCategori] = useState(null); + const [editedTask, setEditedTask] = useState({}); + const [editedCateg, setEditedCateg] = useState({}); + + + + const loadBoardData = useCallback(async () => { + try { + setError('') + const response = await axios.post('/api/boards/load', { id }); + setInfo(response.data); + setCategories(response.data.categories); + } catch (err) { + if (err.response?.data?.message === 'Token Error' || err.response?.data?.message === 'Invalid Token') { + setError('Вы не авторизованы'); + setTimeout(() => { + navigate('/login'); + }, 1000); + } else { + setError('Ошибка загрузки доски'); + } + } + }, [id, navigate]); useEffect(() => { - const checkSession = async () => { - try { - const response = await axios.post('/api/boards/load', { id }); - } catch {} - }; - checkSession(); - }, [id]); + if (id) loadBoardData(); + }, [id, loadBoardData]); + + + const modalCrTask = (categori) => () => { + setCrTask(!crTask); + setTaskCategori(categori); + setTaskTitle(''); + setTaskDescription(''); + } + const modalCrCateg = () => { + setCrCateg(!crCateg); + setCategoryTitle(''); + } + const modalEditTask = (task, id_categ) => () => { + setEdTask(!edTask); + setTaskCategori(id_categ); + setEditedTask(task); + setTaskTitle(task.title); + setTaskDescription(task.description); + setTaskPosition(task.position); + setTaskCategory(task.category_id); + } + const modalEditCateg = (categ) => () => { + setEdCateg(!edCateg); + setEditedCateg(categ); + setCategoryTitle(categ.title); + setCategoryPosition(categ.position); + } + const modalDelTask = () => { + setDelTask(!delTask); + } + const modalDelCateg = () => { + setDelCateg(!delCateg); + } + + + + const createTask = async (e) => { + e.preventDefault(); + const newTask = { category_id: taskCategori, title: taskTitle, description: taskDescription }; + axios.post('/api/boards/categories/tasks/create', newTask); + await loadBoardData(); + modalCrTask(null)(); + }; + const createCategory = async (e) => { + e.preventDefault(); + try { + const newCategory = { board_id: id, title: categoryTitle}; + await axios.post('/api/boards/categories/create', newCategory); + await loadBoardData(); + } catch (err) { + setError(err.response.data.message) + } finally { + modalCrCateg(); + } + }; + + const editTask = async (e) => { + e.preventDefault(); + var newTask = { id: editedTask.id, update_method: "title", value: taskTitle }; + await axios.put('/api/boards/categories/tasks/update', newTask); + newTask = { id: editedTask.id, update_method: "description", value: taskDescription }; + await axios.put('/api/boards/categories/tasks/update', newTask); + newTask = { id: editedTask.id, update_method: "category", value: Number(taskCategory) }; + await axios.put('/api/boards/categories/tasks/update', newTask); + await loadBoardData(); + modalEditTask({}, null)(); + }; + const editCategory = async (e) => { + e.preventDefault(); + try { + const newCategory = { id: editedCateg.id, update_method: "title", value: categoryTitle}; + await axios.put('/api/boards/categories/update', newCategory); + await loadBoardData(); + } catch (err) { + setError(err.response.data.message) + } finally { + modalEditCateg({})(); + } + }; + + + const deleteCategory = async (e) => { + e.preventDefault(); + await axios.delete('/api/boards/categories/delete', { data: {id: editedCateg.id} }); + await loadBoardData(); + modalDelCateg(); + modalEditCateg({})(); + } + const deleteTask = async (e) => { + e.preventDefault(); + await axios.delete('/api/boards/categories/tasks/delete', { data: { id: editedTask.id } }); + await loadBoardData(); + modalDelTask(); + modalEditTask({},null)(); + } + return ( - <> +
+ Участники: В разработке +
+
+ Владелец: {" "+info.owner?.display_name}
+
+
Описание: {info.description ? info.description : 'Отсутствует'}
+SetingPanel:В разработке
+Позиция: {category.position}
+Задач: {category.tasks.length}
+Нет задач
+ )} +