Skip to content

Commit d183c94

Browse files
Revamped Automation Sheet 1/1
1 parent 9b7bde4 commit d183c94

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

src/actions/user/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
"use server";
22

33
import { currentUser } from "@clerk/nextjs/server";
4-
54
import { redirect } from "next/navigation";
65
import { createUser, findUser, updateSubscription } from "./queries";
76
import { refreshToken } from "@/lib/fetch";
87
import { updateIntegration } from "../integrations/queries";
98
import { stripe } from "@/lib/stripe";
109

1110
export const onCurrentUser = async () => {
12-
const user = await currentUser();
13-
14-
if (!user) return redirect("/sign-in");
15-
16-
return user;
11+
try {
12+
const user = await currentUser();
13+
if (!user) return redirect("/sign-in");
14+
return user;
15+
} catch (error) {
16+
console.error("Authentication error:", error);
17+
return redirect("/sign-in");
18+
}
1719
};
1820

1921
export const onBoardUser = async () => {

src/components/global/activate-automation-button/index.tsx

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { Button } from "@/components/ui/button";
1+
import { Switch } from "@/components/ui/switch";
22
import { Loader2 } from "lucide-react";
33
import React from "react";
44
import { useQueryAutomation } from "@/hooks/user-queries";
55
import { useMutationData } from "@/hooks/use-mutation-data";
66
import { activateAutomation } from "@/actions/automations";
7-
import { ActiveAutomation } from "@/icons/active-automation";
87

98
type Props = {
109
id: string;
@@ -18,18 +17,37 @@ const ActivateAutomationButton = ({ id }: Props) => {
1817
"automation-info"
1918
);
2019

21-
return (
22-
<Button
23-
disabled={isPending}
24-
onClick={() => mutate({ state: !data?.data?.active })}
25-
className="lg:px-10 bg-gradient-to-br hover:opacity-80 text-white rounded-full from-[#3352CC] font-medium to-[#1C2D70] ml-4"
26-
>
27-
{isPending ? <Loader2 className="animate-spin" /> : <ActiveAutomation />}
20+
const [optimisticState, setOptimisticState] = React.useState(data?.data?.active || false);
21+
22+
React.useEffect(() => {
23+
if (!isPending) {
24+
setOptimisticState(data?.data?.active || false);
25+
}
26+
}, [data?.data?.active, isPending]);
2827

29-
<p className="lg:inline hidden">
30-
{data?.data?.active ? "Disable" : "Activate"}
31-
</p>
32-
</Button>
28+
const handleStateChange = (checked: boolean) => {
29+
setOptimisticState(checked);
30+
mutate({ state: checked });
31+
};
32+
33+
return (
34+
<div className="flex items-center space-x-2">
35+
<Switch
36+
disabled={isPending}
37+
checked={optimisticState}
38+
onCheckedChange={handleStateChange}
39+
className="data-[state=checked]:hover:bg-green-600 data-[state=checked]:bg-green-500 data-[state=unchecked]:hover:bg-slate-500 data-[state=unchecked]:bg-slate-400 h-6 w-11 mx-4 [&>span]:data-[state=checked]:translate-x-6 [&>span]:bg-white transition-colors duration-200"
40+
/>
41+
<span className="text-sm font-medium">
42+
{isPending ? (
43+
<Loader2 className="h-4 w-4 animate-spin" />
44+
) : (
45+
<strong className={optimisticState ? "text-green-500" : ""}>
46+
{optimisticState ? "Active" : "Inactive"}
47+
</strong>
48+
)}
49+
</span>
50+
</div>
3351
);
3452
};
3553

0 commit comments

Comments
 (0)