refactor: update DialogDelete component to use selectedId and close callback

This commit is contained in:
Ardeman 2025-03-13 09:36:40 +08:00
parent b00adf89ec
commit 798896e4ee
5 changed files with 18 additions and 36 deletions

View File

@ -5,31 +5,21 @@ import {
DialogPanel, DialogPanel,
DialogTitle, DialogTitle,
} from '@headlessui/react' } from '@headlessui/react'
import { import { useEffect, type PropsWithChildren } from 'react'
useEffect,
type Dispatch,
type PropsWithChildren,
type SetStateAction,
} from 'react'
import toast from 'react-hot-toast' import toast from 'react-hot-toast'
import { useFetcher } from 'react-router' import { useFetcher } from 'react-router'
import { Button } from '~/components/ui/button' import { Button } from '~/components/ui/button'
type T = {
id: string
} & any // eslint-disable-line @typescript-eslint/no-explicit-any
type TProperties = PropsWithChildren & { type TProperties = PropsWithChildren & {
selectedItem?: T selectedId?: string
setSelectedItem: Dispatch<SetStateAction<T | undefined>> close: () => void
title: string title: string
fetcherAction: string fetcherAction: string
} }
export const DialogDelete = (properties: TProperties) => { export const DialogDelete = (properties: TProperties) => {
const { selectedItem, setSelectedItem, children, title, fetcherAction } = const { selectedId, close, children, title, fetcherAction } = properties || {}
properties || {}
const fetcher = useFetcher() const fetcher = useFetcher()
useEffect(() => { useEffect(() => {
@ -39,7 +29,7 @@ export const DialogDelete = (properties: TProperties) => {
} }
if (fetcher.data?.success === true) { if (fetcher.data?.success === true) {
setSelectedItem(undefined) close()
toast.success(`${title} berhasil dihapus!`) toast.success(`${title} berhasil dihapus!`)
return return
} }
@ -48,10 +38,10 @@ export const DialogDelete = (properties: TProperties) => {
return ( return (
<Dialog <Dialog
open={!!selectedItem} open={!!selectedId}
onClose={() => { onClose={() => {
if (fetcher.state === 'idle') { if (fetcher.state === 'idle') {
setSelectedItem(undefined) close()
} }
}} }}
className="relative z-50" className="relative z-50"

View File

@ -8,22 +8,16 @@ import TextStyle from '@tiptap/extension-text-style'
import { EditorContent, useEditor } from '@tiptap/react' import { EditorContent, useEditor } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit' import StarterKit from '@tiptap/starter-kit'
import { useEffect, useId, useState } from 'react' import { useEffect, useId, useState } from 'react'
import { import { get, type FieldError, type RegisterOptions } from 'react-hook-form'
get,
type FieldError,
type FieldValues,
type Path,
type RegisterOptions,
} from 'react-hook-form'
import { useRemixFormContext } from 'remix-hook-form' import { useRemixFormContext } from 'remix-hook-form'
import { twMerge } from 'tailwind-merge' import { twMerge } from 'tailwind-merge'
import { EditorMenuBar } from './editor-menubar' import { EditorMenuBar } from './editor-menubar'
import { EditorTextArea } from './editor-textarea' import { EditorTextArea } from './editor-textarea'
type TProperties<TFormValues extends FieldValues> = { type TProperties = {
id?: string id?: string
name: Path<TFormValues> name: string
label?: string label?: string
placeholder?: string placeholder?: string
labelClassName?: string labelClassName?: string
@ -36,9 +30,7 @@ type TProperties<TFormValues extends FieldValues> = {
category: string category: string
} }
export const TextEditor = <TFormValues extends Record<string, unknown>>( export const TextEditor = (properties: TProperties) => {
properties: TProperties<TFormValues>,
) => {
const { const {
id, id,
label, label,
@ -92,7 +84,7 @@ export const TextEditor = <TFormValues extends Record<string, unknown>>(
immediatelyRender: false, immediatelyRender: false,
content: watchContent, content: watchContent,
onUpdate: ({ editor }) => { onUpdate: ({ editor }) => {
setValue(name, editor.getHTML() as any) // eslint-disable-line @typescript-eslint/no-explicit-any setValue(name, editor.getHTML() as string)
}, },
}) })
useEffect(() => { useEffect(() => {

View File

@ -98,8 +98,8 @@ export const AdvertisementsPage = () => {
/> />
<DialogDelete <DialogDelete
selectedItem={selectedAds} selectedId={selectedAds?.id}
setSelectedItem={setSelectedAds} close={() => setSelectedAds(undefined)}
title="Banner iklan" title="Banner iklan"
fetcherAction={`/actions/admin/advertisements/delete/${selectedAds?.id}`} fetcherAction={`/actions/admin/advertisements/delete/${selectedAds?.id}`}
> >

View File

@ -129,8 +129,8 @@ export const SubscribePlanPage = () => {
/> />
<DialogDelete <DialogDelete
selectedItem={selectedSubscribePlan} selectedId={selectedSubscribePlan?.id}
setSelectedItem={setSelectedSubscribePlan} close={() => setSelectedSubscribePlan(undefined)}
title="Subscribe plan" title="Subscribe plan"
fetcherAction={`/actions/admin/subscribe-plan/delete/${selectedSubscribePlan?.id}`} fetcherAction={`/actions/admin/subscribe-plan/delete/${selectedSubscribePlan?.id}`}
> >

View File

@ -102,8 +102,8 @@ export const TagsPage = () => {
/> />
<DialogDelete <DialogDelete
selectedItem={selectedTag} selectedId={selectedTag?.id}
setSelectedItem={setSelectedTag} close={() => setSelectedTag(undefined)}
title="Tag" title="Tag"
fetcherAction={`/actions/admin/tags/delete/${selectedTag?.id}`} fetcherAction={`/actions/admin/tags/delete/${selectedTag?.id}`}
> >