Profile Page with update user information.

This commit is contained in:
charles-gauthereau
2024-11-11 15:31:35 +01:00
parent cd4ad4be01
commit 14a14ecaea
7 changed files with 159 additions and 25 deletions
@@ -0,0 +1,45 @@
"use client"
import {Button} from "@/components/ui/button";
import {useState} from "react";
import {Loader2} from "lucide-react";
export type VariantButton = {
secondary: string
default: string
outline: string
ghost: string
link: string
destructive: string
}
export type ButtonWithConfirmProps = {
icon?: any,
text: string,
variant?: keyof VariantButton ,
className?: string,
onClick?: () => void,
isPending? : boolean
};
export const ButtonWithConfirm = (props: ButtonWithConfirmProps) => {
const [isConfirming, setIsConfirming] = useState(false)
return(
<Button
onClick={() => {
if (isConfirming) {
props.onClick()
} else {
setIsConfirming(true);
}
}}
variant={props.variant ? props.variant : "default"}
className={props.className}
>
{props.isPending && <Loader2 className="animate-spin mr-4" size={16}/>}
{isConfirming ? "Are you sure ?" : `${props.text}`}
<>
{props.icon ? props.icon : null}
</>
</Button>
)
}
@@ -0,0 +1,32 @@
"use client"
import {useMutation} from "@tanstack/react-query";
import {useRouter} from "next/navigation";
import {signOutAction} from "@/features/auth/auth.action";
import {ButtonWithConfirm} from "@/components/wrappers/Button/ButtonWithConfirm/ButtonWithConfirm";
import {deleteUserAction} from "@/components/wrappers/Dashboard/Profile/ButtonDeleteAccount/delete-account.action";
import {Trash2} from "lucide-react";
export type ButtonDeleteAccountProps = {}
export const ButtonDeleteAccount = (props: ButtonDeleteAccountProps) => {
const mutation = useMutation({
mutationFn: () => deleteUserAction(""),
onSuccess: async () => {
await signOutAction();
},
})
return (
<ButtonWithConfirm
text={"Delete my account"}
onClick={() => {
mutation.mutate()
}}
variant={"destructive"}
isPending={mutation.isPending}
className="gap-2"
icon={<Trash2/>}
/>
)
}
@@ -0,0 +1,42 @@
"use server"
import {userAction} from "@/safe-actions";
import {prisma} from "@/prisma";
import {z} from "zod";
import {v4 as uuidv4} from "uuid";
export const deleteUserAction = userAction
.schema(z.string())
.action(async ({parsedInput, ctx}) => {
const uuid = uuidv4()
const user = await prisma.user.update({
where: {
id: ctx.user.id,
},
data: {
email: `${uuid}@portabase.com`,
name: `${uuid}`,
}
})
const account = await prisma.account.findFirst({
where: {
userId: ctx.user.id,
}
})
if(account){
await prisma.account.delete({
where: {
id: account.id
}
})
}
return {
data: user,
}
});