2022-11-19 14:21:26 +00:00
|
|
|
import React, { useCallback, useEffect, useState } from "react";
|
|
|
|
// next
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
// swr
|
|
|
|
import useSWR from "swr";
|
|
|
|
// react beautiful dnd
|
|
|
|
import type { DropResult } from "react-beautiful-dnd";
|
|
|
|
import { DragDropContext } from "react-beautiful-dnd";
|
|
|
|
// services
|
2022-12-12 04:49:52 +00:00
|
|
|
import stateServices from "lib/services/state.service";
|
|
|
|
import issuesServices from "lib/services/issues.service";
|
2022-11-19 14:21:26 +00:00
|
|
|
// hooks
|
|
|
|
import useUser from "lib/hooks/useUser";
|
|
|
|
// fetching keys
|
|
|
|
import { STATE_LIST } from "constants/fetch-keys";
|
|
|
|
// components
|
2022-12-13 15:52:44 +00:00
|
|
|
import SingleBoard from "components/project/issues/BoardView/single-board";
|
2022-11-19 14:21:26 +00:00
|
|
|
import StrictModeDroppable from "components/dnd/StrictModeDroppable";
|
|
|
|
import CreateUpdateIssuesModal from "components/project/issues/CreateUpdateIssueModal";
|
|
|
|
// ui
|
2022-11-24 13:48:18 +00:00
|
|
|
import { Spinner } from "ui";
|
2022-11-19 14:21:26 +00:00
|
|
|
// types
|
2022-12-12 04:49:52 +00:00
|
|
|
import type { IState, IIssue, Properties, NestedKeyOf, IProjectMember } from "types";
|
2022-12-13 15:52:44 +00:00
|
|
|
import ConfirmIssueDeletion from "../confirm-issue-deletion";
|
2022-11-19 14:21:26 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
properties: Properties;
|
|
|
|
selectedGroup: NestedKeyOf<IIssue> | null;
|
|
|
|
groupedByIssues: {
|
|
|
|
[key: string]: IIssue[];
|
|
|
|
};
|
2022-12-12 04:49:52 +00:00
|
|
|
members: IProjectMember[] | undefined;
|
2022-12-13 15:52:44 +00:00
|
|
|
handleDeleteIssue: React.Dispatch<React.SetStateAction<string | undefined>>;
|
|
|
|
partialUpdateIssue: (formData: Partial<IIssue>, issueId: string) => void;
|
2022-11-19 14:21:26 +00:00
|
|
|
};
|
|
|
|
|
2022-12-13 15:52:44 +00:00
|
|
|
const BoardView: React.FC<Props> = ({
|
|
|
|
properties,
|
|
|
|
selectedGroup,
|
|
|
|
groupedByIssues,
|
|
|
|
members,
|
|
|
|
handleDeleteIssue,
|
|
|
|
partialUpdateIssue,
|
|
|
|
}) => {
|
2022-11-19 14:21:26 +00:00
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
|
|
|
|
|
|
const [isIssueOpen, setIsIssueOpen] = useState(false);
|
2022-11-29 14:19:39 +00:00
|
|
|
const [isIssueDeletionOpen, setIsIssueDeletionOpen] = useState(false);
|
|
|
|
const [issueDeletionData, setIssueDeletionData] = useState<IIssue | undefined>();
|
2022-11-19 14:21:26 +00:00
|
|
|
|
|
|
|
const [preloadedData, setPreloadedData] = useState<
|
|
|
|
(Partial<IIssue> & { actionType: "createIssue" | "edit" | "delete" }) | undefined
|
|
|
|
>(undefined);
|
|
|
|
|
|
|
|
const { activeWorkspace } = useUser();
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const { projectId } = router.query;
|
|
|
|
|
|
|
|
const { data: states, mutate: mutateState } = useSWR<IState[]>(
|
|
|
|
projectId && activeWorkspace ? STATE_LIST(projectId as string) : null,
|
|
|
|
activeWorkspace
|
|
|
|
? () => stateServices.getStates(activeWorkspace.slug, projectId as string)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleOnDragEnd = useCallback(
|
|
|
|
(result: DropResult) => {
|
|
|
|
if (!result.destination) return;
|
|
|
|
const { source, destination, type } = result;
|
|
|
|
|
2022-11-29 14:19:39 +00:00
|
|
|
if (destination.droppableId === "trashBox") {
|
|
|
|
const removedItem = groupedByIssues[source.droppableId][source.index];
|
|
|
|
|
|
|
|
setIssueDeletionData(removedItem);
|
|
|
|
setIsIssueDeletionOpen(true);
|
2022-11-19 14:21:26 +00:00
|
|
|
} else {
|
2022-11-29 14:19:39 +00:00
|
|
|
if (type === "state") {
|
|
|
|
const newStates = Array.from(states ?? []);
|
|
|
|
const [reorderedState] = newStates.splice(source.index, 1);
|
|
|
|
newStates.splice(destination.index, 0, reorderedState);
|
|
|
|
const prevSequenceNumber = newStates[destination.index - 1]?.sequence;
|
|
|
|
const nextSequenceNumber = newStates[destination.index + 1]?.sequence;
|
2022-11-19 14:21:26 +00:00
|
|
|
|
2022-11-29 14:19:39 +00:00
|
|
|
const sequenceNumber =
|
|
|
|
prevSequenceNumber && nextSequenceNumber
|
|
|
|
? (prevSequenceNumber + nextSequenceNumber) / 2
|
|
|
|
: nextSequenceNumber
|
|
|
|
? nextSequenceNumber - 15000 / 2
|
|
|
|
: prevSequenceNumber
|
|
|
|
? prevSequenceNumber + 15000 / 2
|
|
|
|
: 15000;
|
2022-11-19 14:21:26 +00:00
|
|
|
|
2022-11-29 14:19:39 +00:00
|
|
|
newStates[destination.index].sequence = sequenceNumber;
|
2022-11-19 14:21:26 +00:00
|
|
|
|
2022-11-29 14:19:39 +00:00
|
|
|
mutateState(newStates, false);
|
|
|
|
if (!activeWorkspace) return;
|
|
|
|
stateServices
|
|
|
|
.patchState(
|
|
|
|
activeWorkspace.slug,
|
|
|
|
projectId as string,
|
|
|
|
newStates[destination.index].id,
|
|
|
|
{
|
|
|
|
sequence: sequenceNumber,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.then((response) => {
|
|
|
|
console.log(response);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
2022-11-19 14:21:26 +00:00
|
|
|
});
|
2022-11-29 14:19:39 +00:00
|
|
|
} else {
|
|
|
|
if (source.droppableId !== destination.droppableId) {
|
|
|
|
const sourceGroup = source.droppableId; // source group id
|
|
|
|
const destinationGroup = destination.droppableId; // destination group id
|
|
|
|
if (!sourceGroup || !destinationGroup) return;
|
|
|
|
|
|
|
|
// removed/dragged item
|
|
|
|
const removedItem = groupedByIssues[source.droppableId][source.index];
|
|
|
|
|
|
|
|
if (selectedGroup === "priority") {
|
|
|
|
// update the removed item for mutation
|
|
|
|
removedItem.priority = destinationGroup;
|
2022-11-19 14:21:26 +00:00
|
|
|
|
2022-11-29 14:19:39 +00:00
|
|
|
// patch request
|
|
|
|
issuesServices.patchIssue(
|
|
|
|
activeWorkspace!.slug,
|
|
|
|
projectId as string,
|
|
|
|
removedItem.id,
|
|
|
|
{
|
|
|
|
priority: destinationGroup,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else if (selectedGroup === "state_detail.name") {
|
|
|
|
const destinationState = states?.find((s) => s.name === destinationGroup);
|
|
|
|
const destinationStateId = destinationState?.id;
|
|
|
|
|
|
|
|
// update the removed item for mutation
|
|
|
|
if (!destinationStateId || !destinationState) return;
|
|
|
|
removedItem.state = destinationStateId;
|
|
|
|
removedItem.state_detail = destinationState;
|
|
|
|
|
|
|
|
// patch request
|
|
|
|
issuesServices.patchIssue(
|
|
|
|
activeWorkspace!.slug,
|
|
|
|
projectId as string,
|
|
|
|
removedItem.id,
|
|
|
|
{
|
|
|
|
state: destinationStateId,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove item from the source group
|
|
|
|
groupedByIssues[source.droppableId].splice(source.index, 1);
|
|
|
|
// add item to the destination group
|
|
|
|
groupedByIssues[destination.droppableId].splice(destination.index, 0, removedItem);
|
|
|
|
}
|
2022-11-19 14:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[activeWorkspace, mutateState, groupedByIssues, projectId, selectedGroup, states]
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isOpen) return;
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
setPreloadedData(undefined);
|
|
|
|
clearTimeout(timer);
|
|
|
|
}, 500);
|
|
|
|
}, [isOpen]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-11-29 14:19:39 +00:00
|
|
|
<ConfirmIssueDeletion
|
|
|
|
isOpen={isIssueDeletionOpen}
|
|
|
|
handleClose={() => setIsIssueDeletionOpen(false)}
|
|
|
|
data={issueDeletionData}
|
|
|
|
/>
|
2022-11-19 14:21:26 +00:00
|
|
|
<CreateUpdateIssuesModal
|
|
|
|
isOpen={isIssueOpen && preloadedData?.actionType === "createIssue"}
|
|
|
|
setIsOpen={setIsIssueOpen}
|
|
|
|
prePopulateData={{
|
|
|
|
...preloadedData,
|
|
|
|
}}
|
|
|
|
projectId={projectId as string}
|
|
|
|
/>
|
|
|
|
{groupedByIssues ? (
|
2022-11-29 14:19:39 +00:00
|
|
|
<div className="h-full w-full">
|
|
|
|
<DragDropContext onDragEnd={handleOnDragEnd}>
|
|
|
|
<div className="h-full w-full overflow-hidden">
|
|
|
|
<StrictModeDroppable droppableId="state" type="state" direction="horizontal">
|
|
|
|
{(provided) => (
|
|
|
|
<div
|
|
|
|
className="h-full w-full"
|
|
|
|
{...provided.droppableProps}
|
|
|
|
ref={provided.innerRef}
|
|
|
|
>
|
|
|
|
<div className="flex gap-x-4 h-full overflow-x-auto overflow-y-hidden pb-3">
|
|
|
|
{Object.keys(groupedByIssues).map((singleGroup, index) => (
|
|
|
|
<SingleBoard
|
|
|
|
key={singleGroup}
|
|
|
|
selectedGroup={selectedGroup}
|
|
|
|
groupTitle={singleGroup}
|
|
|
|
createdBy={
|
2022-12-07 14:35:12 +00:00
|
|
|
selectedGroup === "created_by"
|
|
|
|
? members?.find((m) => m.member.id === singleGroup)?.member
|
|
|
|
.first_name ?? "loading..."
|
|
|
|
: null
|
2022-11-29 14:19:39 +00:00
|
|
|
}
|
|
|
|
groupedByIssues={groupedByIssues}
|
|
|
|
index={index}
|
|
|
|
setIsIssueOpen={setIsIssueOpen}
|
|
|
|
properties={properties}
|
|
|
|
setPreloadedData={setPreloadedData}
|
|
|
|
stateId={
|
|
|
|
selectedGroup === "state_detail.name"
|
2022-12-07 14:35:12 +00:00
|
|
|
? states?.find((s) => s.name === singleGroup)?.id ?? null
|
|
|
|
: null
|
2022-11-29 14:19:39 +00:00
|
|
|
}
|
|
|
|
bgColor={
|
|
|
|
selectedGroup === "state_detail.name"
|
|
|
|
? states?.find((s) => s.name === singleGroup)?.color
|
|
|
|
: undefined
|
|
|
|
}
|
2022-12-13 15:52:44 +00:00
|
|
|
handleDeleteIssue={handleDeleteIssue}
|
|
|
|
partialUpdateIssue={partialUpdateIssue}
|
2022-11-29 14:19:39 +00:00
|
|
|
/>
|
|
|
|
))}
|
2022-11-19 14:21:26 +00:00
|
|
|
</div>
|
2022-11-29 14:19:39 +00:00
|
|
|
{provided.placeholder}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</StrictModeDroppable>
|
|
|
|
</div>
|
|
|
|
</DragDropContext>
|
|
|
|
</div>
|
2022-11-19 14:21:26 +00:00
|
|
|
) : (
|
2022-11-29 14:19:39 +00:00
|
|
|
<div className="h-full w-full flex justify-center items-center">
|
2022-11-19 14:21:26 +00:00
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default BoardView;
|