forked from github/plane
fix: state reordering (#269)
* fix: state reordering * refactor: remove unnecessary argument * refactor: mutation after setting default
This commit is contained in:
parent
bb4ffec7e8
commit
0a88b3ed84
@ -6,8 +6,6 @@ import { mutate } from "swr";
|
|||||||
|
|
||||||
// services
|
// services
|
||||||
import stateService from "services/state.service";
|
import stateService from "services/state.service";
|
||||||
// hooks
|
|
||||||
import useToast from "hooks/use-toast";
|
|
||||||
// ui
|
// ui
|
||||||
import { Tooltip } from "components/ui";
|
import { Tooltip } from "components/ui";
|
||||||
// icons
|
// icons
|
||||||
@ -29,7 +27,6 @@ import { STATE_LIST } from "constants/fetch-keys";
|
|||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
index: number;
|
index: number;
|
||||||
currentGroup: string;
|
|
||||||
state: IState;
|
state: IState;
|
||||||
statesList: IState[];
|
statesList: IState[];
|
||||||
activeGroup: StateGroup;
|
activeGroup: StateGroup;
|
||||||
@ -39,7 +36,6 @@ type Props = {
|
|||||||
|
|
||||||
export const SingleState: React.FC<Props> = ({
|
export const SingleState: React.FC<Props> = ({
|
||||||
index,
|
index,
|
||||||
currentGroup,
|
|
||||||
state,
|
state,
|
||||||
statesList,
|
statesList,
|
||||||
activeGroup,
|
activeGroup,
|
||||||
@ -51,15 +47,26 @@ export const SingleState: React.FC<Props> = ({
|
|||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { workspaceSlug, projectId } = router.query;
|
const { workspaceSlug, projectId } = router.query;
|
||||||
|
|
||||||
const { setToastAlert } = useToast();
|
const groupStates = statesList.filter((s) => s.group === state.group);
|
||||||
|
const groupLength = groupStates.length;
|
||||||
|
|
||||||
const groupLength = statesList.filter((s) => s.group === currentGroup).length;
|
const handleMakeDefault = () => {
|
||||||
|
|
||||||
const handleMakeDefault = (stateId: string) => {
|
|
||||||
setIsSubmitting(true);
|
setIsSubmitting(true);
|
||||||
|
|
||||||
const currentDefaultState = statesList.find((s) => s.default);
|
const currentDefaultState = statesList.find((s) => s.default);
|
||||||
|
|
||||||
|
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
|
||||||
|
);
|
||||||
|
|
||||||
if (currentDefaultState)
|
if (currentDefaultState)
|
||||||
stateService
|
stateService
|
||||||
.patchState(workspaceSlug as string, projectId as string, currentDefaultState?.id ?? "", {
|
.patchState(workspaceSlug as string, projectId as string, currentDefaultState?.id ?? "", {
|
||||||
@ -67,72 +74,48 @@ export const SingleState: React.FC<Props> = ({
|
|||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
stateService
|
stateService
|
||||||
.patchState(workspaceSlug as string, projectId as string, stateId, {
|
.patchState(workspaceSlug as string, projectId as string, state.id, {
|
||||||
default: true,
|
default: true,
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.then(() => {
|
||||||
mutate(STATE_LIST(projectId as string));
|
mutate(STATE_LIST(projectId as string));
|
||||||
setToastAlert({
|
|
||||||
type: "success",
|
|
||||||
title: "Successful",
|
|
||||||
message: `${res.name} state set to default successfuly.`,
|
|
||||||
});
|
|
||||||
setIsSubmitting(false);
|
setIsSubmitting(false);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch(() => {
|
||||||
setToastAlert({
|
|
||||||
type: "error",
|
|
||||||
title: "Error",
|
|
||||||
message: "Error in setting the state to default.",
|
|
||||||
});
|
|
||||||
setIsSubmitting(false);
|
setIsSubmitting(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
else
|
else
|
||||||
stateService
|
stateService
|
||||||
.patchState(workspaceSlug as string, projectId as string, stateId, {
|
.patchState(workspaceSlug as string, projectId as string, state.id, {
|
||||||
default: true,
|
default: true,
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.then(() => {
|
||||||
mutate(STATE_LIST(projectId as string));
|
mutate(STATE_LIST(projectId as string));
|
||||||
setToastAlert({
|
|
||||||
type: "success",
|
|
||||||
title: "Successful",
|
|
||||||
message: `${res.name} state set to default successfuly.`,
|
|
||||||
});
|
|
||||||
setIsSubmitting(false);
|
setIsSubmitting(false);
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
setToastAlert({
|
|
||||||
type: "error",
|
|
||||||
title: "Error",
|
|
||||||
message: "Error in setting the state to default.",
|
|
||||||
});
|
|
||||||
setIsSubmitting(false);
|
setIsSubmitting(false);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleMove = (state: IState, index: number, direction: "up" | "down") => {
|
const handleMove = (state: IState, direction: "up" | "down") => {
|
||||||
let newSequence = 15000;
|
let newSequence = 15000;
|
||||||
|
|
||||||
if (direction === "up") {
|
if (direction === "up") {
|
||||||
if (index === 1) newSequence = statesList[0].sequence - 15000;
|
if (index === 1) newSequence = groupStates[0].sequence - 15000;
|
||||||
else newSequence = (statesList[index - 2].sequence + statesList[index - 1].sequence) / 2;
|
else newSequence = (groupStates[index - 2].sequence + groupStates[index - 1].sequence) / 2;
|
||||||
} else {
|
} else {
|
||||||
if (index === groupLength - 2) newSequence = statesList[groupLength - 1].sequence + 15000;
|
if (index === groupLength - 2) newSequence = groupStates[groupLength - 1].sequence + 15000;
|
||||||
else newSequence = (statesList[index + 2].sequence + statesList[index + 1].sequence) / 2;
|
else newSequence = (groupStates[index + 2].sequence + groupStates[index + 1].sequence) / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
let newStatesList = statesList.map((s) => {
|
let newStatesList = statesList.map((s) => ({
|
||||||
if (s.id === state.id)
|
|
||||||
return {
|
|
||||||
...s,
|
...s,
|
||||||
sequence: newSequence,
|
sequence: s.id === state.id ? newSequence : s.sequence,
|
||||||
};
|
}));
|
||||||
|
|
||||||
return s;
|
|
||||||
});
|
|
||||||
newStatesList = orderArrayBy(newStatesList, "sequence", "ascending");
|
newStatesList = orderArrayBy(newStatesList, "sequence", "ascending");
|
||||||
|
|
||||||
mutate(
|
mutate(
|
||||||
STATE_LIST(projectId as string),
|
STATE_LIST(projectId as string),
|
||||||
orderStateGroups(groupBy(newStatesList, "group")),
|
orderStateGroups(groupBy(newStatesList, "group")),
|
||||||
@ -155,7 +138,7 @@ export const SingleState: React.FC<Props> = ({
|
|||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={`group flex items-center justify-between gap-2 border-b bg-gray-50 p-3 ${
|
className={`group flex items-center justify-between gap-2 border-b bg-gray-50 p-3 ${
|
||||||
activeGroup !== currentGroup ? "last:border-0" : ""
|
activeGroup !== state.group ? "last:border-0" : ""
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
@ -165,14 +148,16 @@ export const SingleState: React.FC<Props> = ({
|
|||||||
backgroundColor: state.color,
|
backgroundColor: state.color,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<h6 className="text-sm">{addSpaceIfCamelCase(state.name)}</h6>
|
<h6 className="text-sm">
|
||||||
|
{addSpaceIfCamelCase(state.name)} {state.sequence}
|
||||||
|
</h6>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
{index !== 0 && (
|
{index !== 0 && (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="hidden group-hover:inline-block text-gray-400 hover:text-gray-900"
|
className="hidden group-hover:inline-block text-gray-400 hover:text-gray-900"
|
||||||
onClick={() => handleMove(state, index, "up")}
|
onClick={() => handleMove(state, "up")}
|
||||||
>
|
>
|
||||||
<ArrowUpIcon className="h-4 w-4" />
|
<ArrowUpIcon className="h-4 w-4" />
|
||||||
</button>
|
</button>
|
||||||
@ -181,7 +166,7 @@ export const SingleState: React.FC<Props> = ({
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="hidden group-hover:inline-block text-gray-400 hover:text-gray-900"
|
className="hidden group-hover:inline-block text-gray-400 hover:text-gray-900"
|
||||||
onClick={() => handleMove(state, index, "down")}
|
onClick={() => handleMove(state, "down")}
|
||||||
>
|
>
|
||||||
<ArrowDownIcon className="h-4 w-4" />
|
<ArrowDownIcon className="h-4 w-4" />
|
||||||
</button>
|
</button>
|
||||||
@ -192,7 +177,7 @@ export const SingleState: React.FC<Props> = ({
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="hidden group-hover:inline-block text-xs text-gray-400 hover:text-gray-900"
|
className="hidden group-hover:inline-block text-xs text-gray-400 hover:text-gray-900"
|
||||||
onClick={() => handleMakeDefault(state.id)}
|
onClick={handleMakeDefault}
|
||||||
disabled={isSubmitting}
|
disabled={isSubmitting}
|
||||||
>
|
>
|
||||||
Set as default
|
Set as default
|
||||||
|
@ -115,7 +115,6 @@ const StatesSettings: NextPage<UserAuth> = (props) => {
|
|||||||
<SingleState
|
<SingleState
|
||||||
key={state.id}
|
key={state.id}
|
||||||
index={index}
|
index={index}
|
||||||
currentGroup={key}
|
|
||||||
state={state}
|
state={state}
|
||||||
statesList={statesList}
|
statesList={statesList}
|
||||||
activeGroup={activeGroup}
|
activeGroup={activeGroup}
|
||||||
|
Loading…
Reference in New Issue
Block a user