2023-02-05 11:27:37 +00:00
|
|
|
import { useCallback, useState } from "react";
|
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import useSWR, { mutate } from "swr";
|
|
|
|
|
|
|
|
// react-beautiful-dnd
|
2023-02-13 05:02:02 +00:00
|
|
|
import { DragDropContext, DropResult } from "react-beautiful-dnd";
|
2023-02-05 11:27:37 +00:00
|
|
|
// services
|
|
|
|
import issuesService from "services/issues.service";
|
|
|
|
import stateService from "services/state.service";
|
|
|
|
import projectService from "services/project.service";
|
2023-02-05 16:31:23 +00:00
|
|
|
import modulesService from "services/modules.service";
|
2023-02-05 11:27:37 +00:00
|
|
|
// hooks
|
|
|
|
import useIssueView from "hooks/use-issue-view";
|
|
|
|
// components
|
2023-02-08 13:21:03 +00:00
|
|
|
import { AllLists, AllBoards } from "components/core";
|
2023-02-05 11:27:37 +00:00
|
|
|
import { CreateUpdateIssueModal, DeleteIssueModal } from "components/issues";
|
2023-02-13 05:02:02 +00:00
|
|
|
import StrictModeDroppable from "components/dnd/StrictModeDroppable";
|
|
|
|
// icons
|
|
|
|
import { TrashIcon } from "@heroicons/react/24/outline";
|
2023-02-08 13:21:03 +00:00
|
|
|
// helpers
|
|
|
|
import { getStatesList } from "helpers/state.helper";
|
2023-02-05 11:27:37 +00:00
|
|
|
// types
|
2023-02-08 13:21:03 +00:00
|
|
|
import { CycleIssueResponse, IIssue, IssueResponse, ModuleIssueResponse, UserAuth } from "types";
|
2023-02-05 11:27:37 +00:00
|
|
|
// fetch-keys
|
|
|
|
import {
|
|
|
|
CYCLE_ISSUES,
|
|
|
|
MODULE_ISSUES,
|
|
|
|
PROJECT_ISSUES_LIST,
|
|
|
|
PROJECT_MEMBERS,
|
|
|
|
STATE_LIST,
|
|
|
|
} from "constants/fetch-keys";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
type?: "issue" | "cycle" | "module";
|
|
|
|
issues: IIssue[];
|
|
|
|
openIssuesListModal?: () => void;
|
|
|
|
userAuth: UserAuth;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const IssuesView: React.FC<Props> = ({
|
|
|
|
type = "issue",
|
|
|
|
issues,
|
|
|
|
openIssuesListModal,
|
|
|
|
userAuth,
|
|
|
|
}) => {
|
|
|
|
// create issue modal
|
|
|
|
const [createIssueModal, setCreateIssueModal] = useState(false);
|
|
|
|
const [preloadedData, setPreloadedData] = useState<
|
|
|
|
(Partial<IIssue> & { actionType: "createIssue" | "edit" | "delete" }) | undefined
|
|
|
|
>(undefined);
|
|
|
|
|
|
|
|
// updates issue modal
|
|
|
|
const [editIssueModal, setEditIssueModal] = useState(false);
|
|
|
|
const [issueToEdit, setIssueToEdit] = useState<
|
|
|
|
(IIssue & { actionType: "edit" | "delete" }) | undefined
|
|
|
|
>(undefined);
|
|
|
|
|
|
|
|
// delete issue modal
|
|
|
|
const [deleteIssueModal, setDeleteIssueModal] = useState(false);
|
|
|
|
const [issueToDelete, setIssueToDelete] = useState<IIssue | null>(null);
|
|
|
|
|
2023-02-13 05:02:02 +00:00
|
|
|
// trash box
|
|
|
|
const [trashBox, setTrashBox] = useState(false);
|
|
|
|
|
2023-02-05 11:27:37 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId, cycleId, moduleId } = router.query;
|
|
|
|
|
|
|
|
const { issueView, groupedByIssues, groupByProperty: selectedGroup } = useIssueView(issues);
|
|
|
|
|
2023-02-08 13:21:03 +00:00
|
|
|
const { data: stateGroups } = useSWR(
|
2023-02-05 11:27:37 +00:00
|
|
|
workspaceSlug && projectId ? STATE_LIST(projectId as string) : null,
|
|
|
|
workspaceSlug
|
|
|
|
? () => stateService.getStates(workspaceSlug as string, projectId as string)
|
|
|
|
: null
|
|
|
|
);
|
2023-02-08 13:21:03 +00:00
|
|
|
const states = getStatesList(stateGroups ?? {});
|
2023-02-05 11:27:37 +00:00
|
|
|
|
|
|
|
const { data: members } = useSWR(
|
|
|
|
projectId ? PROJECT_MEMBERS(projectId as string) : null,
|
|
|
|
workspaceSlug && projectId
|
|
|
|
? () => projectService.projectMembers(workspaceSlug as string, projectId as string)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
2023-02-13 05:02:02 +00:00
|
|
|
const handleDeleteIssue = useCallback(
|
|
|
|
(issue: IIssue) => {
|
|
|
|
setDeleteIssueModal(true);
|
|
|
|
setIssueToDelete(issue);
|
|
|
|
},
|
|
|
|
[setDeleteIssueModal, setIssueToDelete]
|
|
|
|
);
|
|
|
|
|
2023-02-05 11:27:37 +00:00
|
|
|
const handleOnDragEnd = useCallback(
|
|
|
|
(result: DropResult) => {
|
2023-02-13 05:02:02 +00:00
|
|
|
setTrashBox(false);
|
|
|
|
|
2023-02-05 11:27:37 +00:00
|
|
|
if (!result.destination || !workspaceSlug || !projectId) return;
|
|
|
|
|
2023-02-08 04:43:07 +00:00
|
|
|
const { source, destination } = result;
|
|
|
|
|
|
|
|
const draggedItem = groupedByIssues[source.droppableId][source.index];
|
|
|
|
|
2023-02-13 05:02:02 +00:00
|
|
|
if (destination.droppableId === "trashBox") {
|
|
|
|
handleDeleteIssue(draggedItem);
|
|
|
|
} else {
|
|
|
|
if (source.droppableId !== destination.droppableId) {
|
|
|
|
const sourceGroup = source.droppableId; // source group id
|
|
|
|
const destinationGroup = destination.droppableId; // destination group id
|
|
|
|
|
|
|
|
if (!sourceGroup || !destinationGroup) return;
|
|
|
|
|
|
|
|
if (selectedGroup === "priority") {
|
|
|
|
// update the removed item for mutation
|
|
|
|
draggedItem.priority = destinationGroup;
|
|
|
|
|
|
|
|
if (cycleId)
|
|
|
|
mutate<CycleIssueResponse[]>(
|
|
|
|
CYCLE_ISSUES(cycleId as string),
|
|
|
|
(prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
const updatedIssues = prevData.map((issue) => {
|
|
|
|
if (issue.issue_detail.id === draggedItem.id) {
|
|
|
|
return {
|
|
|
|
...issue,
|
|
|
|
issue_detail: {
|
|
|
|
...draggedItem,
|
|
|
|
priority: destinationGroup,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return issue;
|
|
|
|
});
|
|
|
|
return [...updatedIssues];
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
if (moduleId)
|
|
|
|
mutate<ModuleIssueResponse[]>(
|
|
|
|
MODULE_ISSUES(moduleId as string),
|
|
|
|
(prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
const updatedIssues = prevData.map((issue) => {
|
|
|
|
if (issue.issue_detail.id === draggedItem.id) {
|
|
|
|
return {
|
|
|
|
...issue,
|
|
|
|
issue_detail: {
|
|
|
|
...draggedItem,
|
|
|
|
priority: destinationGroup,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return issue;
|
|
|
|
});
|
|
|
|
return [...updatedIssues];
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
mutate<IssueResponse>(
|
|
|
|
PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string),
|
2023-02-05 11:27:37 +00:00
|
|
|
(prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
|
2023-02-13 05:02:02 +00:00
|
|
|
const updatedIssues = prevData.results.map((issue) => {
|
|
|
|
if (issue.id === draggedItem.id)
|
2023-02-08 04:43:07 +00:00
|
|
|
return {
|
2023-02-13 05:02:02 +00:00
|
|
|
...draggedItem,
|
|
|
|
priority: destinationGroup,
|
2023-02-08 04:43:07 +00:00
|
|
|
};
|
2023-02-13 05:02:02 +00:00
|
|
|
|
2023-02-08 04:43:07 +00:00
|
|
|
return issue;
|
|
|
|
});
|
2023-02-13 05:02:02 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...prevData,
|
|
|
|
results: updatedIssues,
|
|
|
|
};
|
2023-02-05 11:27:37 +00:00
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2023-02-13 05:02:02 +00:00
|
|
|
// patch request
|
|
|
|
issuesService
|
|
|
|
.patchIssue(workspaceSlug as string, projectId as string, draggedItem.id, {
|
|
|
|
priority: destinationGroup,
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (cycleId) mutate(CYCLE_ISSUES(cycleId as string));
|
|
|
|
if (moduleId) mutate(MODULE_ISSUES(moduleId as string));
|
2023-02-08 04:43:07 +00:00
|
|
|
|
2023-02-13 05:02:02 +00:00
|
|
|
mutate(PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string));
|
2023-02-05 11:27:37 +00:00
|
|
|
});
|
2023-02-13 05:02:02 +00:00
|
|
|
} 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;
|
|
|
|
draggedItem.state = destinationStateId;
|
|
|
|
draggedItem.state_detail = destinationState;
|
|
|
|
|
|
|
|
if (cycleId)
|
|
|
|
mutate<CycleIssueResponse[]>(
|
|
|
|
CYCLE_ISSUES(cycleId as string),
|
|
|
|
(prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
const updatedIssues = prevData.map((issue) => {
|
|
|
|
if (issue.issue_detail.id === draggedItem.id) {
|
|
|
|
return {
|
|
|
|
...issue,
|
|
|
|
issue_detail: {
|
|
|
|
...draggedItem,
|
|
|
|
state_detail: destinationState,
|
|
|
|
state: destinationStateId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return issue;
|
|
|
|
});
|
|
|
|
return [...updatedIssues];
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
if (moduleId)
|
|
|
|
mutate<ModuleIssueResponse[]>(
|
|
|
|
MODULE_ISSUES(moduleId as string),
|
|
|
|
(prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
const updatedIssues = prevData.map((issue) => {
|
|
|
|
if (issue.issue_detail.id === draggedItem.id) {
|
|
|
|
return {
|
|
|
|
...issue,
|
|
|
|
issue_detail: {
|
|
|
|
...draggedItem,
|
|
|
|
state_detail: destinationState,
|
|
|
|
state: destinationStateId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return issue;
|
|
|
|
});
|
|
|
|
return [...updatedIssues];
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
mutate<IssueResponse>(
|
|
|
|
PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string),
|
2023-02-05 11:27:37 +00:00
|
|
|
(prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
|
2023-02-13 05:02:02 +00:00
|
|
|
const updatedIssues = prevData.results.map((issue) => {
|
|
|
|
if (issue.id === draggedItem.id)
|
2023-02-08 04:43:07 +00:00
|
|
|
return {
|
2023-02-13 05:02:02 +00:00
|
|
|
...draggedItem,
|
|
|
|
state_detail: destinationState,
|
|
|
|
state: destinationStateId,
|
2023-02-08 04:43:07 +00:00
|
|
|
};
|
2023-02-13 05:02:02 +00:00
|
|
|
|
2023-02-08 04:43:07 +00:00
|
|
|
return issue;
|
|
|
|
});
|
2023-02-13 05:02:02 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...prevData,
|
|
|
|
results: updatedIssues,
|
|
|
|
};
|
2023-02-05 11:27:37 +00:00
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2023-02-13 05:02:02 +00:00
|
|
|
// patch request
|
|
|
|
issuesService
|
|
|
|
.patchIssue(workspaceSlug as string, projectId as string, draggedItem.id, {
|
|
|
|
state: destinationStateId,
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (cycleId) mutate(CYCLE_ISSUES(cycleId as string));
|
|
|
|
if (moduleId) mutate(MODULE_ISSUES(moduleId as string));
|
2023-02-08 04:43:07 +00:00
|
|
|
|
2023-02-13 05:02:02 +00:00
|
|
|
mutate(PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string));
|
2023-02-05 11:27:37 +00:00
|
|
|
});
|
2023-02-13 05:02:02 +00:00
|
|
|
}
|
2023-02-05 11:27:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-02-13 05:02:02 +00:00
|
|
|
[
|
|
|
|
workspaceSlug,
|
|
|
|
cycleId,
|
|
|
|
moduleId,
|
|
|
|
groupedByIssues,
|
|
|
|
projectId,
|
|
|
|
selectedGroup,
|
|
|
|
states,
|
|
|
|
handleDeleteIssue,
|
|
|
|
]
|
2023-02-05 11:27:37 +00:00
|
|
|
);
|
|
|
|
|
2023-02-13 05:02:02 +00:00
|
|
|
const addIssueToState = useCallback(
|
|
|
|
(groupTitle: string, stateId: string | null) => {
|
|
|
|
setCreateIssueModal(true);
|
|
|
|
if (selectedGroup)
|
|
|
|
setPreloadedData({
|
|
|
|
state: stateId ?? undefined,
|
|
|
|
[selectedGroup]: groupTitle,
|
|
|
|
actionType: "createIssue",
|
|
|
|
});
|
|
|
|
else setPreloadedData({ actionType: "createIssue" });
|
|
|
|
},
|
|
|
|
[setCreateIssueModal, setPreloadedData, selectedGroup]
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleEditIssue = useCallback(
|
|
|
|
(issue: IIssue) => {
|
|
|
|
setEditIssueModal(true);
|
|
|
|
setIssueToEdit({
|
|
|
|
...issue,
|
|
|
|
actionType: "edit",
|
|
|
|
cycle: issue.issue_cycle ? issue.issue_cycle.cycle : null,
|
|
|
|
module: issue.issue_module ? issue.issue_module.module : null,
|
2023-02-05 16:31:23 +00:00
|
|
|
});
|
2023-02-13 05:02:02 +00:00
|
|
|
},
|
|
|
|
[setEditIssueModal, setIssueToEdit]
|
|
|
|
);
|
|
|
|
|
|
|
|
const removeIssueFromCycle = useCallback(
|
|
|
|
(bridgeId: string) => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
|
|
|
mutate<CycleIssueResponse[]>(
|
|
|
|
CYCLE_ISSUES(cycleId as string),
|
|
|
|
(prevData) => prevData?.filter((p) => p.id !== bridgeId),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
issuesService
|
|
|
|
.removeIssueFromCycle(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
cycleId as string,
|
|
|
|
bridgeId
|
|
|
|
)
|
|
|
|
.then((res) => {
|
|
|
|
console.log(res);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[workspaceSlug, projectId, cycleId]
|
|
|
|
);
|
|
|
|
|
|
|
|
const removeIssueFromModule = useCallback(
|
|
|
|
(bridgeId: string) => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
|
|
|
mutate<ModuleIssueResponse[]>(
|
|
|
|
MODULE_ISSUES(moduleId as string),
|
|
|
|
(prevData) => prevData?.filter((p) => p.id !== bridgeId),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
modulesService
|
|
|
|
.removeIssueFromModule(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
moduleId as string,
|
|
|
|
bridgeId
|
|
|
|
)
|
|
|
|
.then((res) => {
|
|
|
|
console.log(res);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[workspaceSlug, projectId, moduleId]
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleTrashBox = useCallback(
|
|
|
|
(isDragging: boolean) => {
|
|
|
|
if (isDragging && !trashBox) setTrashBox(true);
|
|
|
|
},
|
|
|
|
[trashBox, setTrashBox]
|
|
|
|
);
|
2023-02-05 16:31:23 +00:00
|
|
|
|
2023-02-05 11:27:37 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<CreateUpdateIssueModal
|
|
|
|
isOpen={createIssueModal && preloadedData?.actionType === "createIssue"}
|
|
|
|
handleClose={() => setCreateIssueModal(false)}
|
|
|
|
prePopulateData={{
|
|
|
|
...preloadedData,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<CreateUpdateIssueModal
|
|
|
|
isOpen={editIssueModal && issueToEdit?.actionType !== "delete"}
|
|
|
|
prePopulateData={{ ...issueToEdit }}
|
|
|
|
handleClose={() => setEditIssueModal(false)}
|
|
|
|
data={issueToEdit}
|
|
|
|
/>
|
|
|
|
<DeleteIssueModal
|
|
|
|
handleClose={() => setDeleteIssueModal(false)}
|
|
|
|
isOpen={deleteIssueModal}
|
|
|
|
data={issueToDelete}
|
|
|
|
/>
|
2023-02-13 05:02:02 +00:00
|
|
|
|
|
|
|
<div className="relative">
|
|
|
|
<DragDropContext onDragEnd={handleOnDragEnd}>
|
|
|
|
<StrictModeDroppable droppableId="trashBox">
|
|
|
|
{(provided, snapshot) => (
|
|
|
|
<div
|
|
|
|
className={`${
|
|
|
|
trashBox ? "opacity-100 pointer-events-auto" : "opacity-0 pointer-events-none"
|
|
|
|
} fixed z-20 top-12 left-1/2 -translate-x-1/2 flex items-center gap-2 bg-red-100 border-2 border-red-500 p-3 text-xs rounded ${
|
|
|
|
snapshot.isDraggingOver ? "bg-red-500 text-white" : ""
|
|
|
|
} duration-200`}
|
|
|
|
ref={provided.innerRef}
|
|
|
|
{...provided.droppableProps}
|
|
|
|
>
|
|
|
|
<TrashIcon className="h-3 w-3" />
|
|
|
|
Drop issue here to delete
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</StrictModeDroppable>
|
|
|
|
{issueView === "list" ? (
|
|
|
|
<AllLists
|
|
|
|
type={type}
|
|
|
|
issues={issues}
|
|
|
|
states={states}
|
|
|
|
members={members}
|
|
|
|
addIssueToState={addIssueToState}
|
|
|
|
handleEditIssue={handleEditIssue}
|
|
|
|
handleDeleteIssue={handleDeleteIssue}
|
|
|
|
openIssuesListModal={type !== "issue" ? openIssuesListModal : null}
|
|
|
|
removeIssue={
|
|
|
|
type === "cycle"
|
|
|
|
? removeIssueFromCycle
|
|
|
|
: type === "module"
|
|
|
|
? removeIssueFromModule
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
userAuth={userAuth}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<AllBoards
|
|
|
|
type={type}
|
|
|
|
issues={issues}
|
|
|
|
states={states}
|
|
|
|
members={members}
|
|
|
|
addIssueToState={addIssueToState}
|
2023-02-14 14:35:32 +00:00
|
|
|
handleEditIssue={handleEditIssue}
|
2023-02-13 05:02:02 +00:00
|
|
|
openIssuesListModal={type !== "issue" ? openIssuesListModal : null}
|
|
|
|
handleDeleteIssue={handleDeleteIssue}
|
|
|
|
handleTrashBox={handleTrashBox}
|
2023-02-14 14:35:32 +00:00
|
|
|
removeIssue={
|
|
|
|
type === "cycle"
|
|
|
|
? removeIssueFromCycle
|
|
|
|
: type === "module"
|
|
|
|
? removeIssueFromModule
|
|
|
|
: null
|
|
|
|
}
|
2023-02-13 05:02:02 +00:00
|
|
|
userAuth={userAuth}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</DragDropContext>
|
|
|
|
</div>
|
2023-02-05 11:27:37 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|