From 2df5037fcdc6d163dd6244cca4eb89a7572ea948 Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal Date: Fri, 10 Feb 2023 19:19:29 +0530 Subject: [PATCH 1/3] fix: state reordering --- apps/app/components/states/single-state.tsx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/apps/app/components/states/single-state.tsx b/apps/app/components/states/single-state.tsx index 5e9aff80c6c..59fb834e8ac 100644 --- a/apps/app/components/states/single-state.tsx +++ b/apps/app/components/states/single-state.tsx @@ -53,7 +53,8 @@ export const SingleState: React.FC = ({ const { setToastAlert } = useToast(); - const groupLength = statesList.filter((s) => s.group === currentGroup).length; + const groupStates = statesList.filter((s) => s.group === currentGroup); + const groupLength = groupStates.length; const handleMakeDefault = (stateId: string) => { setIsSubmitting(true); @@ -116,11 +117,11 @@ export const SingleState: React.FC = ({ 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) => { @@ -133,6 +134,7 @@ export const SingleState: React.FC = ({ return s; }); newStatesList = orderArrayBy(newStatesList, "sequence", "ascending"); + mutate( STATE_LIST(projectId as string), orderStateGroups(groupBy(newStatesList, "group")), From c959ead7a80cf40a792b632b9e7982699f2007ff Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal Date: Fri, 10 Feb 2023 21:09:25 +0530 Subject: [PATCH 2/3] refactor: remove unnecessary argument --- apps/app/components/states/single-state.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/app/components/states/single-state.tsx b/apps/app/components/states/single-state.tsx index 59fb834e8ac..d5739151d86 100644 --- a/apps/app/components/states/single-state.tsx +++ b/apps/app/components/states/single-state.tsx @@ -113,7 +113,7 @@ export const SingleState: React.FC = ({ }); }; - const handleMove = (state: IState, index: number, direction: "up" | "down") => { + const handleMove = (state: IState, direction: "up" | "down") => { let newSequence = 15000; if (direction === "up") { @@ -174,7 +174,7 @@ export const SingleState: React.FC = ({ @@ -183,7 +183,7 @@ export const SingleState: React.FC = ({ From b3430cdc61144cc7e4686953eac4e5bc0c1df7fe Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal Date: Sat, 11 Feb 2023 16:03:21 +0530 Subject: [PATCH 3/3] refactor: mutation after setting default --- apps/app/components/states/single-state.tsx | 73 +++++++------------ .../projects/[projectId]/settings/states.tsx | 1 - 2 files changed, 28 insertions(+), 46 deletions(-) diff --git a/apps/app/components/states/single-state.tsx b/apps/app/components/states/single-state.tsx index d5739151d86..2c0f4071e85 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,16 +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 === currentGroup); + const groupStates = statesList.filter((s) => s.group === state.group); const groupLength = groupStates.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 ?? "", { @@ -68,47 +74,27 @@ 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); }); }; @@ -124,15 +110,10 @@ export const SingleState: React.FC = ({ 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( @@ -157,7 +138,7 @@ export const SingleState: React.FC = ({ return (
@@ -167,7 +148,9 @@ export const SingleState: React.FC = ({ backgroundColor: state.color, }} /> -
{addSpaceIfCamelCase(state.name)}
+
+ {addSpaceIfCamelCase(state.name)} {state.sequence} +
{index !== 0 && ( @@ -194,7 +177,7 @@ export const SingleState: React.FC = ({