Merge commit '9c9d127dfb1a8e3633f803f527c27bbfc6346b4d'
* commit '9c9d127dfb1a8e3633f803f527c27bbfc6346b4d': feat: add delete functionality for news articles with confirmation dialog
This commit is contained in:
commit
bb3e1bb139
28
app/apis/admin/delete-contents.ts
Normal file
28
app/apis/admin/delete-contents.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||||
|
import type { TContentSchema } from '~/pages/form-contents'
|
||||||
|
|
||||||
|
const deleteContentsResponseSchema = z.object({
|
||||||
|
data: z.object({
|
||||||
|
Message: z.string(),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
type TParameters = {
|
||||||
|
id: TContentSchema['id']
|
||||||
|
} & THttpServer
|
||||||
|
|
||||||
|
export const deleteContentsRequest = async (parameters: TParameters) => {
|
||||||
|
const { id, ...restParameters } = parameters
|
||||||
|
try {
|
||||||
|
const { data } = await HttpServer(restParameters).delete(
|
||||||
|
`/api/news/${id}/delete`,
|
||||||
|
)
|
||||||
|
|
||||||
|
return deleteContentsResponseSchema.parse(data)
|
||||||
|
} catch (error) {
|
||||||
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||||
|
return Promise.reject(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,10 +1,17 @@
|
|||||||
|
import {
|
||||||
|
PencilSquareIcon,
|
||||||
|
PlusIcon,
|
||||||
|
TrashIcon,
|
||||||
|
} from '@heroicons/react/24/solid'
|
||||||
import DT, { type Config, type ConfigColumns } from 'datatables.net-dt'
|
import DT, { type Config, type ConfigColumns } from 'datatables.net-dt'
|
||||||
import DataTable, { type DataTableSlots } from 'datatables.net-react'
|
import DataTable, { type DataTableSlots } from 'datatables.net-react'
|
||||||
|
import { useState } from 'react'
|
||||||
import { Link, useRouteLoaderData } from 'react-router'
|
import { Link, useRouteLoaderData } from 'react-router'
|
||||||
|
|
||||||
import type { TCategoryResponse } from '~/apis/common/get-categories'
|
import type { TCategoryResponse } from '~/apis/common/get-categories'
|
||||||
import type { TAuthorResponse } from '~/apis/common/get-news'
|
import type { TAuthorResponse, TNewsResponse } from '~/apis/common/get-news'
|
||||||
import type { TTagResponse } from '~/apis/common/get-tags'
|
import type { TTagResponse } from '~/apis/common/get-tags'
|
||||||
|
import { DialogDelete } from '~/components/dialog/delete'
|
||||||
import { Button } from '~/components/ui/button'
|
import { Button } from '~/components/ui/button'
|
||||||
import { UiTable } from '~/components/ui/table'
|
import { UiTable } from '~/components/ui/table'
|
||||||
import { TitleDashboard } from '~/components/ui/title-dashboard'
|
import { TitleDashboard } from '~/components/ui/title-dashboard'
|
||||||
@ -15,7 +22,7 @@ export const ContentsPage = () => {
|
|||||||
const loaderData = useRouteLoaderData<typeof loader>(
|
const loaderData = useRouteLoaderData<typeof loader>(
|
||||||
'routes/_admin.lg-admin._dashboard.contents._index',
|
'routes/_admin.lg-admin._dashboard.contents._index',
|
||||||
)
|
)
|
||||||
|
const [selectedContent, setSelectedContent] = useState<TNewsResponse>()
|
||||||
DataTable.use(DT)
|
DataTable.use(DT)
|
||||||
const dataTable =
|
const dataTable =
|
||||||
loaderData?.newsData?.sort(
|
loaderData?.newsData?.sort(
|
||||||
@ -81,15 +88,26 @@ export const ContentsPage = () => {
|
|||||||
Normal
|
Normal
|
||||||
</div>
|
</div>
|
||||||
),
|
),
|
||||||
7: (value: string) => (
|
7: (value: string, _type: unknown, data: TNewsResponse) => (
|
||||||
<Button
|
<div className="flex space-x-2">
|
||||||
as="a"
|
<Button
|
||||||
href={`/lg-admin/contents/update/${encodeURIComponent(value)}`}
|
as="a"
|
||||||
className="text-md rounded-md"
|
href={`/lg-admin/contents/update/${encodeURIComponent(value)}`}
|
||||||
size="sm"
|
size="icon"
|
||||||
>
|
title="Update Artikel"
|
||||||
Update Artikel
|
>
|
||||||
</Button>
|
<PencilSquareIcon className="size-4" />
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
size="icon"
|
||||||
|
variant="danger"
|
||||||
|
onClick={() => setSelectedContent(data)}
|
||||||
|
title="Hapus Artikel"
|
||||||
|
>
|
||||||
|
<TrashIcon className="size-4" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
const dataOptions: Config = {
|
const dataOptions: Config = {
|
||||||
@ -110,7 +128,7 @@ export const ContentsPage = () => {
|
|||||||
size="lg"
|
size="lg"
|
||||||
className="text-md h-[42px] px-4"
|
className="text-md h-[42px] px-4"
|
||||||
>
|
>
|
||||||
Buat Artikel
|
<PlusIcon className="size-8" /> Buat Artikel
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -121,6 +139,15 @@ export const ContentsPage = () => {
|
|||||||
options={dataOptions}
|
options={dataOptions}
|
||||||
title="Daftar Artikel"
|
title="Daftar Artikel"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<DialogDelete
|
||||||
|
selectedId={selectedContent?.id}
|
||||||
|
close={() => setSelectedContent(undefined)}
|
||||||
|
title="Artikel"
|
||||||
|
fetcherAction={`/actions/admin/contents/delete/${selectedContent?.id}`}
|
||||||
|
>
|
||||||
|
<p>{selectedContent?.title}</p>
|
||||||
|
</DialogDelete>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
48
app/routes/actions.admin.contents.delete.$id.ts
Normal file
48
app/routes/actions.admin.contents.delete.$id.ts
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import { data } from 'react-router'
|
||||||
|
import { XiorError } from 'xior'
|
||||||
|
|
||||||
|
import { deleteContentsRequest } from '~/apis/admin/delete-contents'
|
||||||
|
import { handleCookie } from '~/libs/cookies'
|
||||||
|
|
||||||
|
import type { Route } from './+types/actions.admin.contents.delete.$id'
|
||||||
|
|
||||||
|
export const action = async ({ request, params }: Route.ActionArgs) => {
|
||||||
|
const { staffToken: accessToken } = await handleCookie(request)
|
||||||
|
const { id } = params
|
||||||
|
try {
|
||||||
|
const { data: newsData } = await deleteContentsRequest({
|
||||||
|
accessToken,
|
||||||
|
id,
|
||||||
|
})
|
||||||
|
|
||||||
|
return data(
|
||||||
|
{
|
||||||
|
success: true,
|
||||||
|
newsData,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 200,
|
||||||
|
statusText: 'OK',
|
||||||
|
},
|
||||||
|
)
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof XiorError) {
|
||||||
|
return data(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
message: error?.response?.data?.error?.message || error.message,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: error?.response?.status || 500,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return data(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
message: 'Internal server error',
|
||||||
|
},
|
||||||
|
{ status: 500 },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user