From 0a88b3ed842601e480e2de96975cfe0d0d0023e7 Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal <65252264+aaryan610@users.noreply.github.com> Date: Mon, 13 Feb 2023 10:30:44 +0530 Subject: [PATCH] fix: state reordering (#269) * fix: state reordering * refactor: remove unnecessary argument * refactor: mutation after setting default --- apps/app/components/states/single-state.tsx | 89 ++++++++----------- .../projects/[projectId]/settings/states.tsx | 1 - 2 files changed, 37 insertions(+), 53 deletions(-) diff --git a/apps/app/components/states/single-state.tsx b/apps/app/components/states/single-state.tsx index 5e9aff80c..2c0f4071e 100644 --- a/apps/app/components/states/single-state.tsx +++ b/apps/app/components/states/single-state.tsx @@ -6,8 +6,6 @@ import { mutate } from "swr"; // services import stateService from "services/state.service"; -// hooks -import useToast from "hooks/use-toast"; // ui import { Tooltip } from "components/ui"; // icons @@ -29,7 +27,6 @@ import { STATE_LIST } from "constants/fetch-keys"; type Props = { index: number; - currentGroup: string; state: IState; statesList: IState[]; activeGroup: StateGroup; @@ -39,7 +36,6 @@ type Props = { export const SingleState: React.FC = ({ index, - currentGroup, state, statesList, activeGroup, @@ -51,15 +47,26 @@ export const SingleState: React.FC = ({ const router = useRouter(); 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 = (stateId: string) => { + const handleMakeDefault = () => { setIsSubmitting(true); 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) stateService .patchState(workspaceSlug as string, projectId as string, currentDefaultState?.id ?? "", { @@ -67,72 +74,48 @@ export const SingleState: React.FC = ({ }) .then(() => { stateService - .patchState(workspaceSlug as string, projectId as string, stateId, { + .patchState(workspaceSlug as string, projectId as string, state.id, { default: true, }) - .then((res) => { + .then(() => { mutate(STATE_LIST(projectId as string)); - setToastAlert({ - type: "success", - title: "Successful", - message: `${res.name} state set to default successfuly.`, - }); setIsSubmitting(false); }) - .catch((err) => { - setToastAlert({ - type: "error", - title: "Error", - message: "Error in setting the state to default.", - }); + .catch(() => { setIsSubmitting(false); }); }); else stateService - .patchState(workspaceSlug as string, projectId as string, stateId, { + .patchState(workspaceSlug as string, projectId as string, state.id, { default: true, }) - .then((res) => { + .then(() => { mutate(STATE_LIST(projectId as string)); - setToastAlert({ - type: "success", - title: "Successful", - message: `${res.name} state set to default successfuly.`, - }); setIsSubmitting(false); }) .catch(() => { - setToastAlert({ - type: "error", - title: "Error", - message: "Error in setting the state to default.", - }); setIsSubmitting(false); }); }; - const handleMove = (state: IState, index: number, direction: "up" | "down") => { + const handleMove = (state: IState, direction: "up" | "down") => { let newSequence = 15000; if (direction === "up") { - if (index === 1) newSequence = statesList[0].sequence - 15000; - else newSequence = (statesList[index - 2].sequence + statesList[index - 1].sequence) / 2; + if (index === 1) newSequence = groupStates[0].sequence - 15000; + else newSequence = (groupStates[index - 2].sequence + groupStates[index - 1].sequence) / 2; } else { - if (index === groupLength - 2) newSequence = statesList[groupLength - 1].sequence + 15000; - else newSequence = (statesList[index + 2].sequence + statesList[index + 1].sequence) / 2; + if (index === groupLength - 2) newSequence = groupStates[groupLength - 1].sequence + 15000; + else newSequence = (groupStates[index + 2].sequence + groupStates[index + 1].sequence) / 2; } - let newStatesList = statesList.map((s) => { - if (s.id === state.id) - return { - ...s, - sequence: newSequence, - }; - - return s; - }); + let newStatesList = statesList.map((s) => ({ + ...s, + sequence: s.id === state.id ? newSequence : s.sequence, + })); newStatesList = orderArrayBy(newStatesList, "sequence", "ascending"); + mutate( STATE_LIST(projectId as string), orderStateGroups(groupBy(newStatesList, "group")), @@ -155,7 +138,7 @@ export const SingleState: React.FC = ({ return (
@@ -165,14 +148,16 @@ export const SingleState: React.FC = ({ backgroundColor: state.color, }} /> -
{addSpaceIfCamelCase(state.name)}
+
+ {addSpaceIfCamelCase(state.name)} {state.sequence} +
{index !== 0 && ( @@ -181,7 +166,7 @@ export const SingleState: React.FC = ({ @@ -192,7 +177,7 @@ export const SingleState: React.FC = ({