refactor: изменена структура передаваемой информации между файлами "Logic" и "API"
This commit is contained in:
@@ -81,3 +81,7 @@ export const deleteBoardsAPI = async (boardId) => {
|
|||||||
data: {id: boardId}
|
data: {id: boardId}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const meAPI = () => {
|
||||||
|
return axios.get('/api/users/me');
|
||||||
|
};
|
||||||
@@ -1,19 +1,11 @@
|
|||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import {
|
import {
|
||||||
//me,
|
meAPI,
|
||||||
loadBoardDataAPI,
|
loadBoardDataAPI, updateBoardsAPI, deleteBoardsAPI,
|
||||||
createTaskAPI,
|
createTaskAPI, updateTaskAPI, deleteTaskAPI,
|
||||||
createCategoryAPI,
|
createCategoryAPI, updateCategoryAPI, deleteCategoryAPI,
|
||||||
updateTaskAPI,
|
addMemberAPI, assignMemberAPI, unassignMemberAPI,
|
||||||
updateCategoryAPI,
|
|
||||||
deleteCategoryAPI,
|
|
||||||
deleteTaskAPI,
|
|
||||||
assignMemberAPI,
|
|
||||||
unassignMemberAPI,
|
|
||||||
addMemberAPI,
|
|
||||||
deleteBoardsAPI,
|
|
||||||
updateBoardsAPI
|
|
||||||
} from './BoardAPI';
|
} from './BoardAPI';
|
||||||
|
|
||||||
export const useBoardLogic = (id, setError, setInfo, setCategories, setLoading) => {
|
export const useBoardLogic = (id, setError, setInfo, setCategories, setLoading) => {
|
||||||
@@ -42,9 +34,91 @@ export const useBoardLogic = (id, setError, setInfo, setCategories, setLoading)
|
|||||||
}
|
}
|
||||||
}, [id, setError, setInfo, setCategories, setLoading, navigate]);
|
}, [id, setError, setInfo, setCategories, setLoading, navigate]);
|
||||||
|
|
||||||
const checkOwner = useCallback((ownerId) => {
|
const checkOwner = useCallback(async (ownerId, setIsOwner) => {
|
||||||
return /*me()*/22 === ownerId
|
setLoading(true);
|
||||||
}, []);
|
const result = await meAPI(ownerId);
|
||||||
|
const res = (result?.data?.id === ownerId);
|
||||||
|
setLoading(false);
|
||||||
|
setIsOwner(res);
|
||||||
|
}, [setLoading]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const createCategory = useCallback(async (categoryTitle, modalCrCateg) => {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
await createCategoryAPI(id, categoryTitle);
|
||||||
|
await loadBoardData();
|
||||||
|
} catch (err) {
|
||||||
|
setError(err.response?.data?.message || 'Ошибка создания категории');
|
||||||
|
} finally {
|
||||||
|
modalCrCateg();
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}, [id, loadBoardData, setLoading, setError]);
|
||||||
|
|
||||||
|
const createTask = useCallback(async (taskCategori, taskTitle, taskDescription, modalCrTask) => {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
await createTaskAPI(taskCategori, taskTitle, taskDescription);
|
||||||
|
await loadBoardData();
|
||||||
|
modalCrTask(null)();
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Ошибка создания задачи:', err);
|
||||||
|
setError('Ошибка создания задачи');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}, [loadBoardData, setLoading, setError]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const editBoard = useCallback(async (editedBoardId, boardTitle, boardDescription, modalEditBoard) => {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
await updateBoardsAPI( editedBoardId, 'title', boardTitle );
|
||||||
|
await updateBoardsAPI( editedBoardId, 'description', boardDescription );
|
||||||
|
await modalEditBoard();
|
||||||
|
await loadBoardData();
|
||||||
|
} catch (err) {
|
||||||
|
setError(err.response?.data?.message || 'Ошибка редактирования доски');
|
||||||
|
} finally {
|
||||||
|
modalEditBoard();
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}, [loadBoardData, setLoading, setError]);
|
||||||
|
|
||||||
|
const editCategory = useCallback(async (editedCategId, categoryTitle, modalEditCateg) => {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
await updateCategoryAPI( editedCategId, 'title', categoryTitle );
|
||||||
|
await loadBoardData();
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Ошибка редактирования категории:', err);
|
||||||
|
setError(err.response?.data?.message || 'Ошибка редактирования категории');
|
||||||
|
} finally {
|
||||||
|
modalEditCateg({})();
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}, [loadBoardData, setLoading, setError]);
|
||||||
|
|
||||||
|
const editTask = useCallback(async (editedTaskId, taskTitle, taskDescription, taskCategory, modalEditTask) => {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
await updateTaskAPI( editedTaskId, 'title', taskTitle );
|
||||||
|
await updateTaskAPI( editedTaskId, 'description', taskDescription );
|
||||||
|
await updateTaskAPI( editedTaskId, 'category', Number(taskCategory) );
|
||||||
|
await loadBoardData();
|
||||||
|
modalEditTask({}, null)();
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Ошибка редактирования задачи:', err);
|
||||||
|
setError('Ошибка редактирования задачи');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}, [loadBoardData, setLoading, setError]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const deleteBoards = useCallback(async (boardId, modalDelBoard) => {
|
const deleteBoards = useCallback(async (boardId, modalDelBoard) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
@@ -59,113 +133,6 @@ export const useBoardLogic = (id, setError, setInfo, setCategories, setLoading)
|
|||||||
}
|
}
|
||||||
}, [setLoading, setError]);
|
}, [setLoading, setError]);
|
||||||
|
|
||||||
const createTask = useCallback(async (taskCategori, taskTitle, taskDescription, modalCrTask) => {
|
|
||||||
setLoading(true);
|
|
||||||
try {
|
|
||||||
const newTask = {
|
|
||||||
category_id: taskCategori,
|
|
||||||
title: taskTitle,
|
|
||||||
description: taskDescription
|
|
||||||
};
|
|
||||||
await createTaskAPI(newTask);
|
|
||||||
await loadBoardData();
|
|
||||||
modalCrTask(null)();
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Ошибка создания задачи:', err);
|
|
||||||
setError('Ошибка создания задачи');
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
}, [loadBoardData, setLoading, setError]);
|
|
||||||
|
|
||||||
const createCategory = useCallback(async (categoryTitle, modalCrCateg) => {
|
|
||||||
setLoading(true);
|
|
||||||
try {
|
|
||||||
const newCategory = {
|
|
||||||
board_id: id,
|
|
||||||
title: categoryTitle
|
|
||||||
};
|
|
||||||
await createCategoryAPI(newCategory);
|
|
||||||
await loadBoardData();
|
|
||||||
} catch (err) {
|
|
||||||
setError(err.response?.data?.message || 'Ошибка создания категории');
|
|
||||||
} finally {
|
|
||||||
modalCrCateg();
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
}, [id, loadBoardData, setLoading, setError]);
|
|
||||||
|
|
||||||
const editBoard = useCallback(async (editedBoardId, boardTitle, boardDescription, modalEditBoard) => {
|
|
||||||
setLoading(true);
|
|
||||||
try {
|
|
||||||
await updateBoardsAPI({
|
|
||||||
id: editedBoardId,
|
|
||||||
update_method: 'title',
|
|
||||||
value: boardTitle
|
|
||||||
});
|
|
||||||
await updateBoardsAPI({
|
|
||||||
id: editedBoardId,
|
|
||||||
update_method: 'description',
|
|
||||||
value: boardDescription
|
|
||||||
});
|
|
||||||
await modalEditBoard();
|
|
||||||
await loadBoardData();
|
|
||||||
} catch (err) {
|
|
||||||
setError(err.response?.data?.message || 'Ошибка редактирования доски');
|
|
||||||
} finally {
|
|
||||||
modalEditBoard();
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
}, [loadBoardData, setLoading, setError]);
|
|
||||||
|
|
||||||
const editTask = useCallback(async (editedTaskId, taskTitle, taskDescription, taskCategory, modalEditTask) => {
|
|
||||||
setLoading(true);
|
|
||||||
try {
|
|
||||||
await updateTaskAPI({
|
|
||||||
id: editedTaskId,
|
|
||||||
update_method: 'title',
|
|
||||||
value: taskTitle
|
|
||||||
});
|
|
||||||
await updateTaskAPI({
|
|
||||||
id: editedTaskId,
|
|
||||||
update_method: 'description',
|
|
||||||
value: taskDescription
|
|
||||||
});
|
|
||||||
await updateTaskAPI({
|
|
||||||
id: editedTaskId,
|
|
||||||
update_method: 'category',
|
|
||||||
value: Number(taskCategory)
|
|
||||||
});
|
|
||||||
|
|
||||||
await loadBoardData();
|
|
||||||
modalEditTask({}, null)();
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Ошибка редактирования задачи:', err);
|
|
||||||
setError('Ошибка редактирования задачи');
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
}, [loadBoardData, setLoading, setError]);
|
|
||||||
|
|
||||||
const editCategory = useCallback(async (editedCategId, categoryTitle, modalEditCateg) => {
|
|
||||||
setLoading(true);
|
|
||||||
try {
|
|
||||||
const updateData = {
|
|
||||||
id: editedCategId,
|
|
||||||
update_method: 'title',
|
|
||||||
value: categoryTitle
|
|
||||||
};
|
|
||||||
await updateCategoryAPI(updateData);
|
|
||||||
await loadBoardData();
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Ошибка редактирования категории:', err);
|
|
||||||
setError(err.response?.data?.message || 'Ошибка редактирования категории');
|
|
||||||
} finally {
|
|
||||||
modalEditCateg({})();
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
}, [loadBoardData, setLoading, setError]);
|
|
||||||
|
|
||||||
const deleteCategory = useCallback(async (categoryId, modalDelCateg, modalEditCateg) => {
|
const deleteCategory = useCallback(async (categoryId, modalDelCateg, modalEditCateg) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
@@ -189,26 +156,34 @@ export const useBoardLogic = (id, setError, setInfo, setCategories, setLoading)
|
|||||||
modalDelTask();
|
modalDelTask();
|
||||||
modalEditTask({}, null)();
|
modalEditTask({}, null)();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Ошибка удаления задачи:', err);
|
|
||||||
setError('Ошибка удаления задачи');
|
setError('Ошибка удаления задачи');
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
}, [loadBoardData, setLoading, setError]);
|
}, [loadBoardData, setLoading, setError]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const addMember = useCallback(async (username, boardId, modalAddMember) => {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
await addMemberAPI( username, boardId );
|
||||||
|
await loadBoardData();
|
||||||
|
modalAddMember();
|
||||||
|
} catch {
|
||||||
|
setError('Ошибка');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}, [loadBoardData, setLoading, setError]);
|
||||||
|
|
||||||
const assignMember = useCallback(async (editedTaskId, memberId, act, modalAssignMember) => {
|
const assignMember = useCallback(async (editedTaskId, memberId, act, modalAssignMember) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
if (act) {
|
if (act) {
|
||||||
await assignMemberAPI({
|
await assignMemberAPI( editedTaskId, memberId );
|
||||||
id: editedTaskId,
|
|
||||||
member_id: memberId
|
|
||||||
});
|
|
||||||
} else if (!act) {
|
} else if (!act) {
|
||||||
await unassignMemberAPI({
|
await unassignMemberAPI( editedTaskId, memberId );
|
||||||
id: editedTaskId,
|
|
||||||
member_id: memberId
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
await loadBoardData();
|
await loadBoardData();
|
||||||
modalAssignMember();
|
modalAssignMember();
|
||||||
@@ -219,34 +194,12 @@ export const useBoardLogic = (id, setError, setInfo, setCategories, setLoading)
|
|||||||
}
|
}
|
||||||
}, [loadBoardData, setLoading, setError]);
|
}, [loadBoardData, setLoading, setError]);
|
||||||
|
|
||||||
const addMember = useCallback(async (username, boardId, modalAddMember) => {
|
|
||||||
setLoading(true);
|
|
||||||
try {
|
|
||||||
await addMemberAPI({
|
|
||||||
username: username,
|
|
||||||
board_id: boardId
|
|
||||||
});
|
|
||||||
await loadBoardData();
|
|
||||||
modalAddMember();
|
|
||||||
} catch {
|
|
||||||
setError('Ошибка');
|
|
||||||
} finally{
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
}, [loadBoardData, setLoading, setError]);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
loadBoardData,
|
loadBoardData, checkOwner, editBoard, deleteBoards,
|
||||||
createTask,
|
createTask, editTask, deleteTask,
|
||||||
checkOwner,
|
createCategory, editCategory, deleteCategory,
|
||||||
createCategory,
|
addMember, assignMember,
|
||||||
editBoard,
|
|
||||||
editTask,
|
|
||||||
editCategory,
|
|
||||||
deleteCategory,
|
|
||||||
deleteTask,
|
|
||||||
assignMember,
|
|
||||||
addMember,
|
|
||||||
deleteBoards
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -10,6 +10,7 @@ const KBBoard = () => {
|
|||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [info, setInfo] = useState({});
|
const [info, setInfo] = useState({});
|
||||||
const [categories, setCategories] = useState([]);
|
const [categories, setCategories] = useState([]);
|
||||||
|
const [isOwner, setIsOwner] = useState(null);
|
||||||
|
|
||||||
const [crTask, setCrTask] = useState(false);
|
const [crTask, setCrTask] = useState(false);
|
||||||
const [crCateg, setCrCateg] = useState(false);
|
const [crCateg, setCrCateg] = useState(false);
|
||||||
@@ -56,6 +57,10 @@ const KBBoard = () => {
|
|||||||
if (id) loadBoardData();
|
if (id) loadBoardData();
|
||||||
}, [id, loadBoardData]);
|
}, [id, loadBoardData]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (info?.owner?.id !== undefined) checkOwner(info?.owner?.id, setIsOwner);
|
||||||
|
}, [info?.owner?.id, checkOwner, setIsOwner]);
|
||||||
|
|
||||||
const modalCrTask = (categori) => () => {
|
const modalCrTask = (categori) => () => {
|
||||||
setCrTask(!crTask);
|
setCrTask(!crTask);
|
||||||
setTaskCategori(categori);
|
setTaskCategori(categori);
|
||||||
@@ -149,6 +154,7 @@ const KBBoard = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="app-container">
|
<div className="app-container">
|
||||||
<Header />
|
<Header />
|
||||||
@@ -172,7 +178,11 @@ const KBBoard = () => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="set-panel" >
|
<div className="set-panel" >
|
||||||
{checkOwner(info?.owner?.id) ? (
|
{loading ? (
|
||||||
|
<>
|
||||||
|
|
||||||
|
</>
|
||||||
|
) : isOwner ? (
|
||||||
<>
|
<>
|
||||||
<button onClick={modalAddMember}>
|
<button onClick={modalAddMember}>
|
||||||
Добавить участника
|
Добавить участника
|
||||||
|
|||||||
Reference in New Issue
Block a user