2020-04-10 04:40:22 +02:00
|
|
|
import React, { useState } from 'react';
|
2020-04-16 22:05:12 +02:00
|
|
|
import * as BoardStateUtils from 'shared/utils/boardState';
|
2020-04-10 04:40:22 +02:00
|
|
|
import styled from 'styled-components/macro';
|
2020-04-16 22:05:12 +02:00
|
|
|
import { useParams, Route, useRouteMatch, useHistory, RouteComponentProps } from 'react-router-dom';
|
2020-04-10 05:27:57 +02:00
|
|
|
import {
|
|
|
|
useFindProjectQuery,
|
|
|
|
useUpdateTaskNameMutation,
|
|
|
|
useCreateTaskMutation,
|
|
|
|
useDeleteTaskMutation,
|
|
|
|
useUpdateTaskLocationMutation,
|
2020-04-11 04:53:03 +02:00
|
|
|
useUpdateTaskGroupLocationMutation,
|
2020-04-11 04:22:58 +02:00
|
|
|
useCreateTaskGroupMutation,
|
2020-04-11 21:24:45 +02:00
|
|
|
useDeleteTaskGroupMutation,
|
2020-04-20 05:02:55 +02:00
|
|
|
useUpdateTaskDescriptionMutation,
|
|
|
|
useAssignTaskMutation,
|
2020-04-10 05:27:57 +02:00
|
|
|
} from 'shared/generated/graphql';
|
2020-04-10 04:40:22 +02:00
|
|
|
|
|
|
|
import QuickCardEditor from 'shared/components/QuickCardEditor';
|
2020-04-11 04:22:58 +02:00
|
|
|
import PopupMenu from 'shared/components/PopupMenu';
|
|
|
|
import ListActions from 'shared/components/ListActions';
|
2020-04-13 00:45:51 +02:00
|
|
|
import MemberManager from 'shared/components/MemberManager';
|
|
|
|
import { LabelsPopup } from 'shared/components/PopupMenu/PopupMenu.stories';
|
2020-04-16 22:05:12 +02:00
|
|
|
import KanbanBoard from 'Projects/Project/KanbanBoard';
|
2020-04-20 05:02:55 +02:00
|
|
|
import Details from './Details';
|
2020-04-10 04:40:22 +02:00
|
|
|
|
2020-04-16 22:05:12 +02:00
|
|
|
type TaskRouteProps = {
|
|
|
|
taskID: string;
|
|
|
|
};
|
2020-04-10 04:40:22 +02:00
|
|
|
|
|
|
|
interface QuickCardEditorState {
|
|
|
|
isOpen: boolean;
|
|
|
|
left: number;
|
|
|
|
top: number;
|
2020-04-10 05:27:57 +02:00
|
|
|
task?: Task;
|
2020-04-10 04:40:22 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 18:31:29 +02:00
|
|
|
const TitleWrapper = styled.div`
|
|
|
|
margin-left: 38px;
|
|
|
|
margin-bottom: 15px;
|
|
|
|
`;
|
|
|
|
|
2020-04-10 04:40:22 +02:00
|
|
|
const Title = styled.span`
|
|
|
|
text-align: center;
|
|
|
|
font-size: 24px;
|
|
|
|
color: #fff;
|
|
|
|
`;
|
|
|
|
|
|
|
|
interface ProjectParams {
|
|
|
|
projectId: string;
|
|
|
|
}
|
|
|
|
|
2020-04-16 22:05:12 +02:00
|
|
|
const initialState: BoardState = { tasks: {}, columns: {} };
|
2020-04-11 04:22:58 +02:00
|
|
|
const initialPopupState = { left: 0, top: 0, isOpen: false, taskGroupID: '' };
|
2020-04-10 04:40:22 +02:00
|
|
|
const initialQuickCardEditorState: QuickCardEditorState = { isOpen: false, top: 0, left: 0 };
|
2020-04-13 00:45:51 +02:00
|
|
|
const initialLabelsPopupState = { taskID: '', isOpen: false, top: 0, left: 0 };
|
|
|
|
const initialTaskDetailsState = { isOpen: false, taskID: '' };
|
2020-04-10 04:40:22 +02:00
|
|
|
|
|
|
|
const Project = () => {
|
|
|
|
const { projectId } = useParams<ProjectParams>();
|
2020-04-16 22:05:12 +02:00
|
|
|
const match = useRouteMatch();
|
|
|
|
const history = useHistory();
|
|
|
|
|
2020-04-20 05:02:55 +02:00
|
|
|
const [updateTaskDescription] = useUpdateTaskDescriptionMutation();
|
2020-04-10 04:40:22 +02:00
|
|
|
const [listsData, setListsData] = useState(initialState);
|
2020-04-11 04:22:58 +02:00
|
|
|
const [popupData, setPopupData] = useState(initialPopupState);
|
2020-04-13 00:45:51 +02:00
|
|
|
const [taskDetails, setTaskDetails] = useState(initialTaskDetailsState);
|
2020-04-10 04:40:22 +02:00
|
|
|
const [quickCardEditor, setQuickCardEditor] = useState(initialQuickCardEditorState);
|
2020-04-10 05:27:57 +02:00
|
|
|
const [updateTaskLocation] = useUpdateTaskLocationMutation();
|
2020-04-11 04:53:03 +02:00
|
|
|
const [updateTaskGroupLocation] = useUpdateTaskGroupLocationMutation();
|
2020-04-16 22:05:12 +02:00
|
|
|
|
2020-04-11 21:24:45 +02:00
|
|
|
const [deleteTaskGroup] = useDeleteTaskGroupMutation({
|
|
|
|
onCompleted: deletedTaskGroupData => {
|
2020-04-16 22:05:12 +02:00
|
|
|
setListsData(
|
|
|
|
BoardStateUtils.deleteTaskGroup(listsData, deletedTaskGroupData.deleteTaskGroup.taskGroup.taskGroupID),
|
|
|
|
);
|
2020-04-11 21:24:45 +02:00
|
|
|
},
|
|
|
|
});
|
2020-04-16 22:05:12 +02:00
|
|
|
|
2020-04-11 04:22:58 +02:00
|
|
|
const [createTaskGroup] = useCreateTaskGroupMutation({
|
|
|
|
onCompleted: newTaskGroupData => {
|
2020-04-16 22:05:12 +02:00
|
|
|
const newTaskGroup = {
|
|
|
|
...newTaskGroupData.createTaskGroup,
|
|
|
|
tasks: [],
|
2020-04-11 04:22:58 +02:00
|
|
|
};
|
2020-04-16 22:05:12 +02:00
|
|
|
setListsData(BoardStateUtils.addTaskGroup(listsData, newTaskGroup));
|
2020-04-11 04:22:58 +02:00
|
|
|
},
|
|
|
|
});
|
2020-04-16 22:05:12 +02:00
|
|
|
|
2020-04-10 05:27:57 +02:00
|
|
|
const [createTask] = useCreateTaskMutation({
|
2020-04-10 04:40:22 +02:00
|
|
|
onCompleted: newTaskData => {
|
2020-04-16 22:05:12 +02:00
|
|
|
const newTask = {
|
|
|
|
...newTaskData.createTask,
|
|
|
|
labels: [],
|
2020-04-10 04:40:22 +02:00
|
|
|
};
|
2020-04-16 22:05:12 +02:00
|
|
|
setListsData(BoardStateUtils.addTask(listsData, newTask));
|
2020-04-10 04:40:22 +02:00
|
|
|
},
|
|
|
|
});
|
2020-04-16 22:05:12 +02:00
|
|
|
|
2020-04-10 05:27:57 +02:00
|
|
|
const [deleteTask] = useDeleteTaskMutation({
|
2020-04-10 04:40:22 +02:00
|
|
|
onCompleted: deletedTask => {
|
2020-04-16 22:05:12 +02:00
|
|
|
setListsData(BoardStateUtils.deleteTask(listsData, deletedTask.deleteTask.taskID));
|
2020-04-10 04:40:22 +02:00
|
|
|
},
|
|
|
|
});
|
2020-04-16 22:05:12 +02:00
|
|
|
|
2020-04-10 05:27:57 +02:00
|
|
|
const [updateTaskName] = useUpdateTaskNameMutation({
|
2020-04-10 04:40:22 +02:00
|
|
|
onCompleted: newTaskData => {
|
2020-04-16 22:05:12 +02:00
|
|
|
setListsData(
|
|
|
|
BoardStateUtils.updateTaskName(listsData, newTaskData.updateTaskName.taskID, newTaskData.updateTaskName.name),
|
|
|
|
);
|
2020-04-10 04:40:22 +02:00
|
|
|
},
|
|
|
|
});
|
2020-04-21 01:04:27 +02:00
|
|
|
const { loading, data, refetch } = useFindProjectQuery({
|
2020-04-10 04:40:22 +02:00
|
|
|
variables: { projectId },
|
|
|
|
onCompleted: newData => {
|
2020-04-21 01:04:27 +02:00
|
|
|
console.log('beep!');
|
2020-04-16 22:05:12 +02:00
|
|
|
const newListsData: BoardState = { tasks: {}, columns: {} };
|
2020-04-11 04:22:58 +02:00
|
|
|
newData.findProject.taskGroups.forEach(taskGroup => {
|
2020-04-10 04:40:22 +02:00
|
|
|
newListsData.columns[taskGroup.taskGroupID] = {
|
|
|
|
taskGroupID: taskGroup.taskGroupID,
|
|
|
|
name: taskGroup.name,
|
|
|
|
position: taskGroup.position,
|
|
|
|
tasks: [],
|
|
|
|
};
|
2020-04-11 04:22:58 +02:00
|
|
|
taskGroup.tasks.forEach(task => {
|
2020-04-21 01:04:27 +02:00
|
|
|
const taskMembers = task.assigned.map(assigned => {
|
|
|
|
return {
|
|
|
|
userID: assigned.userID,
|
|
|
|
displayName: `${assigned.firstName} ${assigned.lastName}`,
|
|
|
|
profileIcon: {
|
|
|
|
url: null,
|
|
|
|
initials: assigned.profileIcon.initials ?? '',
|
|
|
|
bgColor: assigned.profileIcon.bgColor ?? '#7367F0',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2020-04-10 04:40:22 +02:00
|
|
|
newListsData.tasks[task.taskID] = {
|
|
|
|
taskID: task.taskID,
|
2020-04-11 04:22:58 +02:00
|
|
|
taskGroup: {
|
|
|
|
taskGroupID: taskGroup.taskGroupID,
|
|
|
|
},
|
2020-04-10 04:40:22 +02:00
|
|
|
name: task.name,
|
|
|
|
labels: [],
|
2020-04-21 01:04:27 +02:00
|
|
|
position: task.position,
|
2020-04-20 05:02:55 +02:00
|
|
|
description: task.description ?? undefined,
|
2020-04-21 01:04:27 +02:00
|
|
|
members: taskMembers,
|
2020-04-10 04:40:22 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
setListsData(newListsData);
|
|
|
|
},
|
|
|
|
});
|
2020-04-16 22:05:12 +02:00
|
|
|
|
2020-04-10 04:40:22 +02:00
|
|
|
const onCardCreate = (taskGroupID: string, name: string) => {
|
2020-04-11 04:22:58 +02:00
|
|
|
const taskGroupTasks = Object.values(listsData.tasks).filter(
|
|
|
|
(task: Task) => task.taskGroup.taskGroupID === taskGroupID,
|
|
|
|
);
|
2020-04-10 22:31:12 +02:00
|
|
|
let position = 65535;
|
2020-04-10 04:40:22 +02:00
|
|
|
if (taskGroupTasks.length !== 0) {
|
|
|
|
const [lastTask] = taskGroupTasks.sort((a: any, b: any) => a.position - b.position).slice(-1);
|
|
|
|
position = Math.ceil(lastTask.position) * 2 + 1;
|
|
|
|
}
|
|
|
|
|
2020-04-10 22:31:12 +02:00
|
|
|
createTask({ variables: { taskGroupID, name, position } });
|
2020-04-10 04:40:22 +02:00
|
|
|
};
|
2020-04-16 22:05:12 +02:00
|
|
|
|
2020-04-10 04:40:22 +02:00
|
|
|
const onQuickEditorOpen = (e: ContextMenuEvent) => {
|
2020-04-10 05:27:57 +02:00
|
|
|
const currentTask = Object.values(listsData.tasks).find(task => task.taskID === e.taskID);
|
2020-04-10 04:40:22 +02:00
|
|
|
setQuickCardEditor({
|
|
|
|
top: e.top,
|
|
|
|
left: e.left,
|
|
|
|
isOpen: true,
|
2020-04-10 05:27:57 +02:00
|
|
|
task: currentTask,
|
2020-04-10 04:40:22 +02:00
|
|
|
});
|
|
|
|
};
|
2020-04-16 22:05:12 +02:00
|
|
|
const onCardDrop = (droppedTask: Task) => {
|
|
|
|
updateTaskLocation({
|
|
|
|
variables: {
|
|
|
|
taskID: droppedTask.taskID,
|
|
|
|
taskGroupID: droppedTask.taskGroup.taskGroupID,
|
|
|
|
position: droppedTask.position,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
setListsData(BoardStateUtils.updateTask(listsData, droppedTask));
|
|
|
|
};
|
|
|
|
const onListDrop = (droppedColumn: TaskGroup) => {
|
|
|
|
updateTaskGroupLocation({
|
|
|
|
variables: { taskGroupID: droppedColumn.taskGroupID, position: droppedColumn.position },
|
|
|
|
});
|
|
|
|
setListsData(BoardStateUtils.updateTaskGroup(listsData, droppedColumn));
|
|
|
|
};
|
|
|
|
|
|
|
|
const onCreateList = (listName: string) => {
|
|
|
|
const [lastColumn] = Object.values(listsData.columns)
|
|
|
|
.sort((a, b) => a.position - b.position)
|
|
|
|
.slice(-1);
|
|
|
|
let position = 65535;
|
|
|
|
if (lastColumn) {
|
|
|
|
position = lastColumn.position * 2 + 1;
|
|
|
|
}
|
|
|
|
createTaskGroup({ variables: { projectID: projectId, name: listName, position } });
|
|
|
|
};
|
2020-04-10 04:40:22 +02:00
|
|
|
|
2020-04-20 05:02:55 +02:00
|
|
|
const [assignTask] = useAssignTaskMutation();
|
|
|
|
|
2020-04-10 04:40:22 +02:00
|
|
|
if (loading) {
|
2020-04-20 05:02:55 +02:00
|
|
|
return <Title>Error Loading</Title>;
|
2020-04-10 04:40:22 +02:00
|
|
|
}
|
|
|
|
if (data) {
|
2020-04-20 05:02:55 +02:00
|
|
|
const availableMembers = data.findProject.members.map(member => {
|
|
|
|
return {
|
|
|
|
displayName: `${member.firstName} ${member.lastName}`,
|
2020-04-21 01:04:27 +02:00
|
|
|
profileIcon: {
|
|
|
|
url: null,
|
|
|
|
initials: member.profileIcon.initials ?? null,
|
|
|
|
bgColor: member.profileIcon.bgColor ?? null,
|
|
|
|
},
|
2020-04-20 05:02:55 +02:00
|
|
|
userID: member.userID,
|
|
|
|
};
|
|
|
|
});
|
2020-04-10 04:40:22 +02:00
|
|
|
return (
|
|
|
|
<>
|
2020-04-20 05:02:55 +02:00
|
|
|
<KanbanBoard
|
|
|
|
listsData={listsData}
|
|
|
|
onCardDrop={onCardDrop}
|
|
|
|
onListDrop={onListDrop}
|
|
|
|
onCardCreate={onCardCreate}
|
|
|
|
onCreateList={onCreateList}
|
|
|
|
onQuickEditorOpen={onQuickEditorOpen}
|
|
|
|
onOpenListActionsPopup={(isOpen, left, top, taskGroupID) => {
|
|
|
|
setPopupData({ isOpen, top, left, taskGroupID });
|
|
|
|
}}
|
|
|
|
/>
|
2020-04-11 04:22:58 +02:00
|
|
|
{popupData.isOpen && (
|
|
|
|
<PopupMenu
|
|
|
|
title="List Actions"
|
|
|
|
top={popupData.top}
|
|
|
|
onClose={() => setPopupData(initialPopupState)}
|
|
|
|
left={popupData.left}
|
|
|
|
>
|
2020-04-11 21:24:45 +02:00
|
|
|
<ListActions
|
|
|
|
taskGroupID={popupData.taskGroupID}
|
|
|
|
onArchiveTaskGroup={taskGroupID => {
|
|
|
|
deleteTaskGroup({ variables: { taskGroupID } });
|
|
|
|
setPopupData(initialPopupState);
|
|
|
|
}}
|
|
|
|
/>
|
2020-04-11 04:22:58 +02:00
|
|
|
</PopupMenu>
|
|
|
|
)}
|
2020-04-16 22:05:12 +02:00
|
|
|
{quickCardEditor.isOpen && (
|
|
|
|
<QuickCardEditor
|
|
|
|
isOpen
|
|
|
|
taskID={quickCardEditor.task ? quickCardEditor.task.taskID : ''}
|
|
|
|
taskGroupID={quickCardEditor.task ? quickCardEditor.task.taskGroup.taskGroupID : ''}
|
|
|
|
cardTitle={quickCardEditor.task ? quickCardEditor.task.name : ''}
|
|
|
|
onCloseEditor={() => setQuickCardEditor(initialQuickCardEditorState)}
|
|
|
|
onEditCard={(_listId: string, cardId: string, cardName: string) => {
|
|
|
|
updateTaskName({ variables: { taskID: cardId, name: cardName } });
|
2020-04-13 00:45:51 +02:00
|
|
|
}}
|
2020-04-16 22:05:12 +02:00
|
|
|
onOpenPopup={() => console.log()}
|
|
|
|
onArchiveCard={(_listId: string, cardId: string) => deleteTask({ variables: { taskID: cardId } })}
|
|
|
|
labels={[]}
|
|
|
|
top={quickCardEditor.top}
|
|
|
|
left={quickCardEditor.left}
|
2020-04-13 00:45:51 +02:00
|
|
|
/>
|
|
|
|
)}
|
2020-04-16 22:05:12 +02:00
|
|
|
<Route
|
|
|
|
path={`${match.path}/c/:taskID`}
|
|
|
|
render={(routeProps: RouteComponentProps<TaskRouteProps>) => (
|
2020-04-20 05:02:55 +02:00
|
|
|
<Details
|
2020-04-21 01:04:27 +02:00
|
|
|
refreshCache={() => {
|
|
|
|
console.log('beep 2!');
|
|
|
|
refetch();
|
|
|
|
}}
|
2020-04-20 05:02:55 +02:00
|
|
|
availableMembers={availableMembers}
|
|
|
|
projectURL={match.url}
|
|
|
|
taskID={routeProps.match.params.taskID}
|
|
|
|
onTaskNameChange={(updatedTask, newName) => {
|
|
|
|
updateTaskName({ variables: { taskID: updatedTask.taskID, name: newName } });
|
|
|
|
}}
|
|
|
|
onTaskDescriptionChange={(updatedTask, newDescription) => {
|
|
|
|
updateTaskDescription({ variables: { taskID: updatedTask.taskID, description: newDescription } });
|
2020-04-16 22:05:12 +02:00
|
|
|
}}
|
2020-04-20 05:02:55 +02:00
|
|
|
onDeleteTask={deletedTask => {
|
|
|
|
setTaskDetails(initialTaskDetailsState);
|
|
|
|
deleteTask({ variables: { taskID: deletedTask.taskID } });
|
2020-04-16 22:05:12 +02:00
|
|
|
}}
|
2020-04-20 05:02:55 +02:00
|
|
|
onOpenAddLabelPopup={(task, bounds) => {}}
|
2020-04-16 22:05:12 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2020-04-10 04:40:22 +02:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2020-04-20 05:02:55 +02:00
|
|
|
return <div>Error</div>;
|
2020-04-10 04:40:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Project;
|