refactor: cycles toggle favorite (#648)

This commit is contained in:
Aaryan Khandelwal 2023-03-31 16:03:58 +05:30 committed by GitHub
parent 09e17858fe
commit 480e2c4d7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -90,16 +90,13 @@ export const SingleCycleCard: React.FC<TSingleStatProps> = ({
const startDate = new Date(cycle.start_date ?? "");
const handleAddToFavorites = () => {
if (!workspaceSlug && !projectId && !cycle) return;
if (!workspaceSlug || !projectId || !cycle) return;
cyclesService
.addCycleToFavorites(workspaceSlug as string, projectId as string, {
cycle: cycle.id,
})
.then(() => {
const cycleStatus = getDateRangeStatus(cycle.start_date, cycle.end_date);
if (cycleStatus === "current" || cycleStatus === "upcoming")
switch (cycleStatus) {
case "current":
case "upcoming":
mutate<CurrentAndUpcomingCyclesResponse>(
CYCLE_CURRENT_AND_UPCOMING_LIST(projectId as string),
(prevData) => ({
@ -114,7 +111,8 @@ export const SingleCycleCard: React.FC<TSingleStatProps> = ({
}),
false
);
else if (cycleStatus === "completed")
break;
case "completed":
mutate<CompletedCyclesResponse>(
CYCLE_COMPLETE_LIST(projectId as string),
(prevData) => ({
@ -125,7 +123,8 @@ export const SingleCycleCard: React.FC<TSingleStatProps> = ({
}),
false
);
else
break;
case "draft":
mutate<DraftCyclesResponse>(
CYCLE_DRAFT_LIST(projectId as string),
(prevData) => ({
@ -136,12 +135,12 @@ export const SingleCycleCard: React.FC<TSingleStatProps> = ({
}),
false
);
break;
}
setToastAlert({
type: "success",
title: "Success!",
message: "Successfully added the cycle to favorites.",
});
cyclesService
.addCycleToFavorites(workspaceSlug as string, projectId as string, {
cycle: cycle.id,
})
.catch(() => {
setToastAlert({
@ -153,14 +152,13 @@ export const SingleCycleCard: React.FC<TSingleStatProps> = ({
};
const handleRemoveFromFavorites = () => {
if (!workspaceSlug || !cycle) return;
if (!workspaceSlug || !projectId || !cycle) return;
cyclesService
.removeCycleFromFavorites(workspaceSlug as string, projectId as string, cycle.id)
.then(() => {
const cycleStatus = getDateRangeStatus(cycle.start_date, cycle.end_date);
if (cycleStatus === "current" || cycleStatus === "upcoming")
switch (cycleStatus) {
case "current":
case "upcoming":
mutate<CurrentAndUpcomingCyclesResponse>(
CYCLE_CURRENT_AND_UPCOMING_LIST(projectId as string),
(prevData) => ({
@ -175,7 +173,8 @@ export const SingleCycleCard: React.FC<TSingleStatProps> = ({
}),
false
);
else if (cycleStatus === "completed")
break;
case "completed":
mutate<CompletedCyclesResponse>(
CYCLE_COMPLETE_LIST(projectId as string),
(prevData) => ({
@ -186,7 +185,8 @@ export const SingleCycleCard: React.FC<TSingleStatProps> = ({
}),
false
);
else
break;
case "draft":
mutate<DraftCyclesResponse>(
CYCLE_DRAFT_LIST(projectId as string),
(prevData) => ({
@ -197,13 +197,11 @@ export const SingleCycleCard: React.FC<TSingleStatProps> = ({
}),
false
);
break;
}
setToastAlert({
type: "success",
title: "Success!",
message: "Successfully removed the cycle from favorites.",
});
})
cyclesService
.removeCycleFromFavorites(workspaceSlug as string, projectId as string, cycle.id)
.catch(() => {
setToastAlert({
type: "error",