2023-03-28 09:19:27 +00:00
|
|
|
import React, { Dispatch, SetStateAction, useCallback } from "react";
|
2023-09-11 06:15:28 +00:00
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
2023-03-28 09:19:27 +00:00
|
|
|
import useSWR, { mutate } from "swr";
|
|
|
|
|
|
|
|
// cmdk
|
|
|
|
import { Command } from "cmdk";
|
2023-09-11 06:15:28 +00:00
|
|
|
// services
|
2023-09-20 15:03:25 +00:00
|
|
|
import issuesService from "services/issue.service";
|
|
|
|
import stateService from "services/project_state.service";
|
2023-03-28 09:19:27 +00:00
|
|
|
// ui
|
|
|
|
import { Spinner } from "components/ui";
|
2023-09-11 06:15:28 +00:00
|
|
|
// icons
|
|
|
|
import { CheckIcon, StateGroupIcon } from "components/icons";
|
2023-03-28 09:19:27 +00:00
|
|
|
// helpers
|
|
|
|
import { getStatesList } from "helpers/state.helper";
|
|
|
|
// types
|
2023-06-06 16:06:00 +00:00
|
|
|
import { ICurrentUserResponse, IIssue } from "types";
|
2023-03-28 09:19:27 +00:00
|
|
|
// fetch keys
|
2023-04-22 12:49:35 +00:00
|
|
|
import { ISSUE_DETAILS, PROJECT_ISSUES_ACTIVITY, STATES_LIST } from "constants/fetch-keys";
|
2023-03-28 09:19:27 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
setIsPaletteOpen: Dispatch<SetStateAction<boolean>>;
|
|
|
|
issue: IIssue;
|
2023-06-06 16:06:00 +00:00
|
|
|
user: ICurrentUserResponse | undefined;
|
2023-03-28 09:19:27 +00:00
|
|
|
};
|
|
|
|
|
2023-06-06 16:06:00 +00:00
|
|
|
export const ChangeIssueState: React.FC<Props> = ({ setIsPaletteOpen, issue, user }) => {
|
2023-03-28 09:19:27 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId, issueId } = router.query;
|
|
|
|
|
|
|
|
const { data: stateGroups, mutate: mutateIssueDetails } = useSWR(
|
2023-04-22 12:49:35 +00:00
|
|
|
workspaceSlug && projectId ? STATES_LIST(projectId as string) : null,
|
2023-09-20 15:03:25 +00:00
|
|
|
workspaceSlug && projectId ? () => stateService.getStates(workspaceSlug as string, projectId as string) : null
|
2023-03-28 09:19:27 +00:00
|
|
|
);
|
2023-07-26 17:50:44 +00:00
|
|
|
const states = getStatesList(stateGroups);
|
2023-03-28 09:19:27 +00:00
|
|
|
|
|
|
|
const submitChanges = useCallback(
|
|
|
|
async (formData: Partial<IIssue>) => {
|
|
|
|
if (!workspaceSlug || !projectId || !issueId) return;
|
|
|
|
|
2023-05-30 13:44:35 +00:00
|
|
|
mutate<IIssue>(
|
2023-03-28 09:19:27 +00:00
|
|
|
ISSUE_DETAILS(issueId as string),
|
2023-05-30 13:44:35 +00:00
|
|
|
async (prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
return {
|
|
|
|
...prevData,
|
|
|
|
...formData,
|
|
|
|
};
|
|
|
|
},
|
2023-03-28 09:19:27 +00:00
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
const payload = { ...formData };
|
|
|
|
await issuesService
|
2023-06-06 16:06:00 +00:00
|
|
|
.patchIssue(workspaceSlug as string, projectId as string, issueId as string, payload, user)
|
2023-03-28 09:19:27 +00:00
|
|
|
.then(() => {
|
|
|
|
mutateIssueDetails();
|
|
|
|
mutate(PROJECT_ISSUES_ACTIVITY(issueId as string));
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
},
|
2023-06-20 11:02:02 +00:00
|
|
|
[workspaceSlug, issueId, projectId, mutateIssueDetails, user]
|
2023-03-28 09:19:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const handleIssueState = (stateId: string) => {
|
|
|
|
submitChanges({ state: stateId });
|
|
|
|
setIsPaletteOpen(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{states ? (
|
|
|
|
states.length > 0 ? (
|
|
|
|
states.map((state) => (
|
2023-09-20 15:03:25 +00:00
|
|
|
<Command.Item key={state.id} onSelect={() => handleIssueState(state.id)} className="focus:outline-none">
|
2023-03-28 09:19:27 +00:00
|
|
|
<div className="flex items-center space-x-3">
|
2023-09-20 15:03:25 +00:00
|
|
|
<StateGroupIcon stateGroup={state.group} color={state.color} height="16px" width="16px" />
|
2023-03-28 09:19:27 +00:00
|
|
|
<p>{state.name}</p>
|
|
|
|
</div>
|
|
|
|
<div>{state.id === issue.state && <CheckIcon className="h-3 w-3" />}</div>
|
|
|
|
</Command.Item>
|
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<div className="text-center">No states found</div>
|
|
|
|
)
|
|
|
|
) : (
|
|
|
|
<Spinner />
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|