2023-02-10 12:32:18 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import { mutate } from "swr";
|
|
|
|
|
|
|
|
// services
|
|
|
|
import stateService from "services/state.service";
|
|
|
|
// ui
|
|
|
|
import { Tooltip } from "components/ui";
|
|
|
|
// icons
|
|
|
|
import {
|
|
|
|
ArrowDownIcon,
|
|
|
|
ArrowUpIcon,
|
|
|
|
PencilSquareIcon,
|
|
|
|
TrashIcon,
|
|
|
|
} from "@heroicons/react/24/outline";
|
|
|
|
// helpers
|
|
|
|
import { addSpaceIfCamelCase } from "helpers/string.helper";
|
|
|
|
import { groupBy, orderArrayBy } from "helpers/array.helper";
|
|
|
|
import { orderStateGroups } from "helpers/state.helper";
|
|
|
|
// types
|
|
|
|
import { IState } from "types";
|
|
|
|
import { StateGroup } from "components/states";
|
|
|
|
// fetch-keys
|
|
|
|
import { STATE_LIST } from "constants/fetch-keys";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
index: number;
|
|
|
|
state: IState;
|
|
|
|
statesList: IState[];
|
|
|
|
activeGroup: StateGroup;
|
|
|
|
handleEditState: () => void;
|
|
|
|
handleDeleteState: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const SingleState: React.FC<Props> = ({
|
|
|
|
index,
|
|
|
|
state,
|
|
|
|
statesList,
|
|
|
|
activeGroup,
|
|
|
|
handleEditState,
|
|
|
|
handleDeleteState,
|
|
|
|
}) => {
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
|
2023-02-13 05:00:44 +00:00
|
|
|
const groupStates = statesList.filter((s) => s.group === state.group);
|
|
|
|
const groupLength = groupStates.length;
|
2023-02-10 12:32:18 +00:00
|
|
|
|
2023-02-13 05:00:44 +00:00
|
|
|
const handleMakeDefault = () => {
|
2023-02-10 12:32:18 +00:00
|
|
|
setIsSubmitting(true);
|
|
|
|
|
|
|
|
const currentDefaultState = statesList.find((s) => s.default);
|
|
|
|
|
2023-02-13 05:00:44 +00:00
|
|
|
let newStatesList = statesList.map((s) => ({
|
|
|
|
...s,
|
|
|
|
default: s.id === state.id ? true : s.id === currentDefaultState?.id ? false : s.default,
|
|
|
|
}));
|
|
|
|
newStatesList = orderArrayBy(newStatesList, "sequence", "ascending");
|
|
|
|
|
|
|
|
mutate(
|
|
|
|
STATE_LIST(projectId as string),
|
|
|
|
orderStateGroups(groupBy(newStatesList, "group")),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2023-02-10 12:32:18 +00:00
|
|
|
if (currentDefaultState)
|
|
|
|
stateService
|
|
|
|
.patchState(workspaceSlug as string, projectId as string, currentDefaultState?.id ?? "", {
|
|
|
|
default: false,
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
stateService
|
2023-02-13 05:00:44 +00:00
|
|
|
.patchState(workspaceSlug as string, projectId as string, state.id, {
|
2023-02-10 12:32:18 +00:00
|
|
|
default: true,
|
|
|
|
})
|
2023-02-13 05:00:44 +00:00
|
|
|
.then(() => {
|
2023-02-10 12:32:18 +00:00
|
|
|
mutate(STATE_LIST(projectId as string));
|
|
|
|
setIsSubmitting(false);
|
|
|
|
})
|
2023-02-13 05:00:44 +00:00
|
|
|
.catch(() => {
|
2023-02-10 12:32:18 +00:00
|
|
|
setIsSubmitting(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
else
|
|
|
|
stateService
|
2023-02-13 05:00:44 +00:00
|
|
|
.patchState(workspaceSlug as string, projectId as string, state.id, {
|
2023-02-10 12:32:18 +00:00
|
|
|
default: true,
|
|
|
|
})
|
2023-02-13 05:00:44 +00:00
|
|
|
.then(() => {
|
2023-02-10 12:32:18 +00:00
|
|
|
mutate(STATE_LIST(projectId as string));
|
|
|
|
setIsSubmitting(false);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setIsSubmitting(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-02-13 05:00:44 +00:00
|
|
|
const handleMove = (state: IState, direction: "up" | "down") => {
|
2023-02-10 12:32:18 +00:00
|
|
|
let newSequence = 15000;
|
|
|
|
|
|
|
|
if (direction === "up") {
|
2023-02-13 05:00:44 +00:00
|
|
|
if (index === 1) newSequence = groupStates[0].sequence - 15000;
|
|
|
|
else newSequence = (groupStates[index - 2].sequence + groupStates[index - 1].sequence) / 2;
|
2023-02-10 12:32:18 +00:00
|
|
|
} else {
|
2023-02-13 05:00:44 +00:00
|
|
|
if (index === groupLength - 2) newSequence = groupStates[groupLength - 1].sequence + 15000;
|
|
|
|
else newSequence = (groupStates[index + 2].sequence + groupStates[index + 1].sequence) / 2;
|
2023-02-10 12:32:18 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 05:00:44 +00:00
|
|
|
let newStatesList = statesList.map((s) => ({
|
|
|
|
...s,
|
|
|
|
sequence: s.id === state.id ? newSequence : s.sequence,
|
|
|
|
}));
|
2023-02-10 12:32:18 +00:00
|
|
|
newStatesList = orderArrayBy(newStatesList, "sequence", "ascending");
|
2023-02-13 05:00:44 +00:00
|
|
|
|
2023-02-10 12:32:18 +00:00
|
|
|
mutate(
|
|
|
|
STATE_LIST(projectId as string),
|
|
|
|
orderStateGroups(groupBy(newStatesList, "group")),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
stateService
|
|
|
|
.patchState(workspaceSlug as string, projectId as string, state.id, {
|
|
|
|
sequence: newSequence,
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
console.log(res);
|
|
|
|
mutate(STATE_LIST(projectId as string));
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={`group flex items-center justify-between gap-2 border-b bg-gray-50 p-3 ${
|
2023-02-13 05:00:44 +00:00
|
|
|
activeGroup !== state.group ? "last:border-0" : ""
|
2023-02-10 12:32:18 +00:00
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
<div
|
|
|
|
className="h-3 w-3 flex-shrink-0 rounded-full"
|
|
|
|
style={{
|
|
|
|
backgroundColor: state.color,
|
|
|
|
}}
|
|
|
|
/>
|
2023-02-13 14:08:58 +00:00
|
|
|
<h6 className="text-sm">{addSpaceIfCamelCase(state.name)}</h6>
|
2023-02-10 12:32:18 +00:00
|
|
|
</div>
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
{index !== 0 && (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="hidden group-hover:inline-block text-gray-400 hover:text-gray-900"
|
2023-02-13 05:00:44 +00:00
|
|
|
onClick={() => handleMove(state, "up")}
|
2023-02-10 12:32:18 +00:00
|
|
|
>
|
|
|
|
<ArrowUpIcon className="h-4 w-4" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
{!(index === groupLength - 1) && (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="hidden group-hover:inline-block text-gray-400 hover:text-gray-900"
|
2023-02-13 05:00:44 +00:00
|
|
|
onClick={() => handleMove(state, "down")}
|
2023-02-10 12:32:18 +00:00
|
|
|
>
|
|
|
|
<ArrowDownIcon className="h-4 w-4" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
{state.default ? (
|
|
|
|
<span className="text-xs text-gray-400">Default</span>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="hidden group-hover:inline-block text-xs text-gray-400 hover:text-gray-900"
|
2023-02-13 05:00:44 +00:00
|
|
|
onClick={handleMakeDefault}
|
2023-02-10 12:32:18 +00:00
|
|
|
disabled={isSubmitting}
|
|
|
|
>
|
|
|
|
Set as default
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
<Tooltip content="Cannot delete the default state." disabled={!state.default}>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className={`${state.default ? "cursor-not-allowed" : ""} grid place-items-center`}
|
|
|
|
onClick={handleDeleteState}
|
|
|
|
disabled={state.default}
|
|
|
|
>
|
|
|
|
<TrashIcon className="h-4 w-4 text-red-400" />
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
<button type="button" className="grid place-items-center" onClick={handleEditState}>
|
|
|
|
<PencilSquareIcon className="h-4 w-4 text-gray-400" />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|