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