♻️ Use revalidatePath instead of client side refresh (#1802)

This commit is contained in:
Luke Vella 2025-07-09 12:19:41 +03:00 committed by GitHub
parent 961a493a29
commit 6bd24e7e1f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 22 additions and 29 deletions

View file

@ -1,6 +1,7 @@
"use server";
import { ActionError, authActionClient } from "@/features/safe-action/server";
import { signOut } from "@/next-auth";
import { subject } from "@casl/ability";
import { prisma } from "@rallly/database";
@ -33,6 +34,16 @@ export const deleteCurrentUserAction = authActionClient.action(
},
});
ctx.posthog?.capture({
event: "delete_account",
distinctId: ctx.user.id,
properties: {
email: ctx.user.email,
},
});
await signOut();
return {
success: true,
};

View file

@ -1,5 +1,4 @@
"use client";
import { usePostHog } from "@rallly/posthog/client";
import { Button } from "@rallly/ui/button";
import type { DialogProps } from "@rallly/ui/dialog";
import {
@ -13,7 +12,6 @@ import {
} from "@rallly/ui/dialog";
import { Form, FormField, FormItem, FormMessage } from "@rallly/ui/form";
import { Input } from "@rallly/ui/input";
import { signOut } from "next-auth/react";
import { useForm } from "react-hook-form";
import { Trans } from "@/components/trans";
@ -34,16 +32,7 @@ export function DeleteAccountDialog({
},
});
const posthog = usePostHog();
const deleteUser = useSafeAction(deleteCurrentUserAction, {
onSuccess: () => {
posthog?.capture("delete account");
signOut({
redirectTo: "/login",
});
},
});
const deleteUser = useSafeAction(deleteCurrentUserAction);
const { t } = useTranslation();
return (

View file

@ -3,6 +3,7 @@ import { authActionClient } from "@/features/safe-action/server";
import { ActionError } from "@/features/safe-action/server";
import { subject } from "@casl/ability";
import { prisma } from "@rallly/database";
import { redirect } from "next/navigation";
export const makeMeAdminAction = authActionClient.action(async ({ ctx }) => {
if (ctx.ability.cannot("update", subject("User", ctx.user), "role")) {
@ -20,4 +21,6 @@ export const makeMeAdminAction = authActionClient.action(async ({ ctx }) => {
role: "admin",
},
});
redirect("/control-panel");
});

View file

@ -3,16 +3,10 @@
import { Trans } from "@/components/trans";
import { useSafeAction } from "@/features/safe-action/client";
import { Button } from "@rallly/ui/button";
import { useRouter } from "next/navigation";
import { makeMeAdminAction } from "./actions";
export function MakeMeAdminButton() {
const router = useRouter();
const makeMeAdmin = useSafeAction(makeMeAdminAction, {
onSuccess: () => {
router.replace("/control-panel");
},
});
const makeMeAdmin = useSafeAction(makeMeAdminAction);
return (
<Button
onClick={async () => {

View file

@ -24,7 +24,6 @@ import {
FormMessage,
} from "@rallly/ui/form";
import { Input } from "@rallly/ui/input";
import { useRouter } from "next/navigation";
import { useForm } from "react-hook-form";
import { z } from "zod";
@ -53,7 +52,6 @@ export function DeleteUserDialog({
open: boolean;
onOpenChange: (open: boolean) => void;
}) {
const router = useRouter();
const schema = useSchema(email);
const form = useForm({
resolver: zodResolver(schema),
@ -64,7 +62,6 @@ export function DeleteUserDialog({
const deleteUser = useSafeAction(deleteUserAction, {
onSuccess: () => {
router.refresh();
onOpenChange(false);
},
});

View file

@ -23,7 +23,6 @@ import {
} from "@rallly/ui/dropdown-menu";
import { Icon } from "@rallly/ui/icon";
import { MoreHorizontal, TrashIcon, UserPenIcon } from "lucide-react";
import { useRouter } from "next/navigation";
import { useTransition } from "react";
import { DeleteUserDialog } from "./dialogs/delete-user-dialog";
@ -40,12 +39,7 @@ export function UserRow({
image?: string;
role: "admin" | "user";
}) {
const router = useRouter();
const changeRole = useSafeAction(changeRoleAction, {
onSuccess: () => {
router.refresh();
},
});
const changeRole = useSafeAction(changeRoleAction);
const [isPending, startTransition] = useTransition();
const { user } = useUser();

View file

@ -2,6 +2,7 @@
import { ActionError, authActionClient } from "@/features/safe-action/server";
import { subject } from "@casl/ability";
import { prisma } from "@rallly/database";
import { revalidatePath } from "next/cache";
import { z } from "zod";
import { getUser } from "./queries";
@ -46,6 +47,8 @@ export const changeRoleAction = authActionClient
role,
},
});
revalidatePath("/control-panel");
});
export const deleteUserAction = authActionClient
@ -83,6 +86,8 @@ export const deleteUserAction = authActionClient
},
});
revalidatePath("/control-panel");
return {
success: true,
};