Skip to content

Commit

Permalink
chore: update files
Browse files Browse the repository at this point in the history
  • Loading branch information
haru52 committed May 25, 2024
1 parent 71d49ee commit d29e871
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
26 changes: 21 additions & 5 deletions src/app/_components/delete-user-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,39 @@

import { api } from "~/trpc/react";
import { useRouter } from "next/navigation";
import type { MouseEvent } from "react";

export function DeleteUserLink() {
const deleteUser = api.user.delete.useMutation();
const router = useRouter();
const deleteUser = api.user.delete.useMutation({
onSuccess: () => {
alert("アカウントを削除しました");
router.push("/");
router.refresh();
},
onError: (error) => {
alert(`アカウント削除に失敗しました。\n${error.message}`);
},
});

const handleDeleteUser = () => {
const handleDeleteUser = (
e: MouseEvent<HTMLAnchorElement, globalThis.MouseEvent>,
) => {
e.preventDefault();
if (deleteUser.isPending) return;
if (!confirm("本当にアカウントを削除しますか?")) {
return;
}
deleteUser.mutate();
router.push("/");
router.refresh();
};

return (
<p className="mt-12 text-center">
<a className="link link-error" onClick={handleDeleteUser}>
<a
className="link link-error"
onClick={handleDeleteUser}
aria-disabled={deleteUser.isPending}
>
アカウントを削除
</a>
</p>
Expand Down
2 changes: 1 addition & 1 deletion src/server/api/routers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const userRouter = createTRPCRouter({
return !!user;
}),

delete: protectedProcedure.mutation(({ ctx }) => {
delete: protectedProcedure.mutation(async ({ ctx }) => {
return ctx.db.user.delete({
where: { id: ctx.session.user.id },
});
Expand Down

0 comments on commit d29e871

Please sign in to comment.