refactor: drag function (#826)

This commit is contained in:
Aaryan Khandelwal 2023-04-14 16:41:28 +05:30 committed by GitHub
parent f50872f2a9
commit ce253b3cc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,8 +32,11 @@ import {
RectangleStackIcon, RectangleStackIcon,
TrashIcon, TrashIcon,
} from "@heroicons/react/24/outline"; } from "@heroicons/react/24/outline";
// images
import emptyIssue from "public/empty-state/empty-issue.svg";
// helpers // helpers
import { getStatesList } from "helpers/state.helper"; import { getStatesList } from "helpers/state.helper";
import { orderArrayBy } from "helpers/array.helper";
// types // types
import { IIssue, IIssueFilterOptions } from "types"; import { IIssue, IIssueFilterOptions } from "types";
// fetch-keys // fetch-keys
@ -46,8 +49,6 @@ import {
STATE_LIST, STATE_LIST,
} from "constants/fetch-keys"; } from "constants/fetch-keys";
// image // image
import emptyIssue from "public/empty-state/empty-issue.svg";
import { orderArrayBy } from "helpers/array.helper";
type Props = { type Props = {
type?: "issue" | "cycle" | "module"; type?: "issue" | "cycle" | "module";
@ -191,71 +192,31 @@ export const IssuesView: React.FC<Props> = ({
const sourceGroup = source.droppableId; // source group id const sourceGroup = source.droppableId; // source group id
// TODO: move this mutation logic to a separate function mutate<{
if (cycleId) [key: string]: IIssue[];
mutate<{ }>(
[key: string]: IIssue[]; cycleId
}>( ? CYCLE_ISSUES_WITH_PARAMS(cycleId as string, params)
CYCLE_ISSUES_WITH_PARAMS(cycleId as string, params), : moduleId
(prevData) => { ? MODULE_ISSUES_WITH_PARAMS(moduleId as string, params)
if (!prevData) return prevData; : PROJECT_ISSUES_LIST_WITH_PARAMS(projectId as string, params),
(prevData) => {
if (!prevData) return prevData;
const sourceGroupArray = prevData[sourceGroup]; const sourceGroupArray = prevData[sourceGroup];
const destinationGroupArray = groupedByIssues[destinationGroup]; const destinationGroupArray = groupedByIssues[destinationGroup];
sourceGroupArray.splice(source.index, 1); sourceGroupArray.splice(source.index, 1);
destinationGroupArray.splice(destination.index, 0, draggedItem); destinationGroupArray.splice(destination.index, 0, draggedItem);
return { return {
...prevData, ...prevData,
[sourceGroup]: orderArrayBy(sourceGroupArray, orderBy), [sourceGroup]: orderArrayBy(sourceGroupArray, orderBy),
[destinationGroup]: orderArrayBy(destinationGroupArray, orderBy), [destinationGroup]: orderArrayBy(destinationGroupArray, orderBy),
}; };
}, },
false false
); );
else if (moduleId)
mutate<{
[key: string]: IIssue[];
}>(
MODULE_ISSUES_WITH_PARAMS(moduleId as string, params),
(prevData) => {
if (!prevData) return prevData;
const sourceGroupArray = prevData[sourceGroup];
const destinationGroupArray = groupedByIssues[destinationGroup];
sourceGroupArray.splice(source.index, 1);
destinationGroupArray.splice(destination.index, 0, draggedItem);
return {
...prevData,
[sourceGroup]: orderArrayBy(sourceGroupArray, orderBy),
[destinationGroup]: orderArrayBy(destinationGroupArray, orderBy),
};
},
false
);
else
mutate<{ [key: string]: IIssue[] }>(
PROJECT_ISSUES_LIST_WITH_PARAMS(projectId as string, params),
(prevData) => {
if (!prevData) return prevData;
const sourceGroupArray = prevData[sourceGroup];
const destinationGroupArray = groupedByIssues[destinationGroup];
sourceGroupArray.splice(source.index, 1);
destinationGroupArray.splice(destination.index, 0, draggedItem);
return {
...prevData,
[sourceGroup]: orderArrayBy(sourceGroupArray, orderBy),
[destinationGroup]: orderArrayBy(destinationGroupArray, orderBy),
};
},
false
);
// patch request // patch request
issuesService issuesService