Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: confirmation pop up before user account deletion #59

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/components/shared/form-delete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function FormDelete({
disabled,
extraComponent,
className,
customButton,
}: {
action: string // Example: /user/posts/delete
intentValue: string // Example: delete-post-by-id
Expand All @@ -39,6 +40,7 @@ export function FormDelete({
disabled?: boolean
extraComponent?: React.ReactNode
className?: string
customButton?: React.ReactNode
}) {
const [open, setOpen] = useState<boolean>()
const location = useLocation()
Expand All @@ -49,10 +51,10 @@ export function FormDelete({
return (
<AlertDialog open={open} onOpenChange={setOpen}>
<AlertDialogTrigger asChild className={className}>
<Button variant="outline" size="xs" disabled={disabled}>
{customButton || <Button variant="outline" size="xs" disabled={disabled}>
<Iconify icon="ph:trash-duotone" />
<span>{buttonText}</span>
</Button>
</Button>}
</AlertDialogTrigger>

<AlertDialogContent>
Expand Down
26 changes: 9 additions & 17 deletions app/routes/user.account.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { conform, useForm } from "@conform-to/react"
import { parse } from "@conform-to/zod"
import {
json,
Expand All @@ -8,16 +7,13 @@ import {
type MetaFunction,
} from "@remix-run/node"
import {
Form,
useActionData,
useLoaderData,
useNavigation,
} from "@remix-run/react"
import { type z } from "zod"
import { AvatarAuto } from "~/components/ui/avatar-auto"

import { ButtonLoading } from "~/components/ui/button-loading"
import { Input } from "~/components/ui/input"
import { FormDelete } from "~/components/shared/form-delete"
import { requireUser } from "~/helpers/auth"
import { modelUser } from "~/models/user.server"
import { schemaGeneralId } from "~/schemas/general"
Expand All @@ -39,17 +35,10 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {

export default function UserAccountRoute() {
const { user } = useLoaderData<typeof loader>()
const lastSubmission = useActionData<typeof action>()

const navigation = useNavigation()
const isSubmitting = navigation.state === "submitting"

const [form, { id }] = useForm<z.infer<typeof schemaGeneralId>>({
id: "delete-account",
lastSubmission,
defaultValue: { id: user.id },
})

return (
<div className="app-container">
<header className="app-header items-center gap-4">
Expand All @@ -69,9 +58,12 @@ export default function UserAccountRoute() {
</p>
</header>

<Form method="POST" {...form.props}>
<fieldset disabled={isSubmitting}>
<Input {...conform.input(id, { type: "hidden" })} required />
<FormDelete
action=""
intentValue=""
itemText={`your account: ${user.fullname} (@${user.username})`}
defaultValue={user.id}
customButton={
<ButtonLoading
type="submit"
size="sm"
Expand All @@ -81,8 +73,8 @@ export default function UserAccountRoute() {
>
Delete account
</ButtonLoading>
</fieldset>
</Form>
}
/>
</section>
</div>
)
Expand Down