feat: restructure ads mgmt by creating a new layout for ads creation and updating related schemas

This commit is contained in:
Ardeman
2025-03-12 09:47:21 +08:00
parent 90e2042a91
commit 70cbb134d8
22 changed files with 382 additions and 71 deletions
+37 -32
View File
@@ -1,23 +1,36 @@
import type { ConfigColumns } from 'datatables.net-dt'
import type { DataTableSlots } from 'datatables.net-react'
import { Link } from 'react-router'
import { twMerge } from 'tailwind-merge'
import { Link, useRouteLoaderData } from 'react-router'
import { Button } from '~/components/ui/button'
import { UiTable } from '~/components/ui/table'
import { TitleDashboard } from '~/components/ui/title-dashboard'
import { BANNER } from '~/data/contents'
type TStatusColors = 'draft' | 'active' | 'inactive'
import type { loader } from '~/routes/_admin.lg-admin._dashboard.advertisements._index'
export const AdvertisementsPage = () => {
const dataBanner = BANNER
const loaderData = useRouteLoaderData<typeof loader>(
'routes/_admin.lg-admin._dashboard.advertisements._index',
)
const { adsData: dataTable } = loaderData || {}
const dataColumns: ConfigColumns[] = [
{ title: 'No', data: 'id' },
{ title: 'Banner', data: 'urlImage' },
{ title: 'Link', data: 'link' },
{ title: 'Tgl Create', data: 'createdAt' },
{ title: 'Status', data: 'status' },
{
title: 'No',
render: (
_data: unknown,
_type: unknown,
_row: unknown,
meta: { row: number },
) => {
return meta.row + 1
},
},
{ title: 'Banner', data: 'image_url' },
{ title: 'Link', data: 'url' },
{
title: 'Action',
data: 'id',
},
]
const dataSlot: DataTableSlots = {
1: (value: string) => {
@@ -25,30 +38,22 @@ export const AdvertisementsPage = () => {
<div>
<img
src={value}
alt={`banner - ${value}`}
className="aspect-[15/1] h-[50px] max-w-[200px] rounded"
alt={value}
className="aspect-[15/1] h-[50px] max-h-[100px] min-h-[65px] rounded object-cover"
/>
</div>
)
},
4: (value: string) => {
const statusColors = {
draft: 'bg-gray-300',
active: 'bg-[#04D182]',
inactive: 'bg-[#F96D19]',
}
const status = value as TStatusColors
return (
<span
className={twMerge(
'rounded-md px-2 py-1 text-sm',
status ? statusColors[status] : '',
)}
>
{status}
</span>
)
},
3: (value: string) => (
<Button
as="a"
href={`/lg-admin/advertisements/update/${value}`}
className="text-md rounded-md"
size="sm"
>
Update Banner Iklan
</Button>
),
}
return (
@@ -68,7 +73,7 @@ export const AdvertisementsPage = () => {
</div>
<UiTable
data={dataBanner}
data={dataTable}
columns={dataColumns}
slots={dataSlot}
title="Daftar Banner Iklan"
+5 -6
View File
@@ -3,7 +3,7 @@ import DataTable, { type DataTableSlots } from 'datatables.net-react'
import { Link, useRouteLoaderData } from 'react-router'
import type { TCategoryResponse } from '~/apis/common/get-categories'
import type { TNewsResponse } from '~/apis/common/get-news'
import type { TAuthor } from '~/apis/common/get-news'
import type { TTagResponse } from '~/apis/common/get-tags'
import { Button } from '~/components/ui/button'
import { UiTable } from '~/components/ui/table'
@@ -39,6 +39,7 @@ export const ContentsPage = () => {
},
{
title: 'Penulis',
data: 'author',
},
{ title: 'Judul', data: 'title' },
{
@@ -57,12 +58,10 @@ export const ContentsPage = () => {
]
const dataSlot: DataTableSlots = {
1: (value: string) => formatDate(value),
2: (_value: unknown, _type: unknown, data: TNewsResponse) => (
2: (value: TAuthor) => (
<div>
<div>{data.author.name}</div>
<div className="text-sm text-[#7C7C7C]">
ID: {data.author.id.slice(0, 8)}
</div>
<div>{value.name}</div>
<div className="text-sm text-[#7C7C7C]">ID: {value.id.slice(0, 8)}</div>
</div>
),
3: (value: string) => <span className="text-sm">{value}</span>,
+103
View File
@@ -0,0 +1,103 @@
import { zodResolver } from '@hookform/resolvers/zod'
import { useEffect, useState } from 'react'
import { useFetcher, useNavigate } from 'react-router'
import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
import { z } from 'zod'
import type { TAdResponse } from '~/apis/common/get-ads'
import { Button } from '~/components/ui/button'
import { Input } from '~/components/ui/input'
import { InputFile } from '~/components/ui/input-file'
import { TitleDashboard } from '~/components/ui/title-dashboard'
export const adsSchema = z.object({
id: z.string().optional(),
image: z.string().url({
message: 'Gambar must be a valid URL',
}),
url: z.string().url({
message: 'URL must be valid',
}),
})
export type TAdsSchema = z.infer<typeof adsSchema>
type TProperties = {
adData?: TAdResponse
}
export const FormAdvertisementsPage = (properties: TProperties) => {
const { adData } = properties || {}
const fetcher = useFetcher()
const navigate = useNavigate()
const formMethods = useRemixForm<TAdsSchema>({
mode: 'onSubmit',
fetcher,
resolver: zodResolver(adsSchema),
values: {
id: adData?.id || undefined,
image: adData?.image_url || '',
url: adData?.url || '',
},
})
const [error, setError] = useState<string>()
const { handleSubmit } = formMethods
useEffect(() => {
if (!fetcher.data?.success) {
setError(fetcher.data?.message)
return
}
navigate('/lg-admin/advertisements')
setError(undefined)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fetcher])
return (
<div className="relative">
<TitleDashboard title={`${adData ? 'Update' : 'Buat'} Banner Iklan`} />
<div>
<RemixFormProvider {...formMethods}>
<fetcher.Form
method="post"
onSubmit={handleSubmit}
action={`/actions/admin/advertisements/${adData ? 'update' : 'create'}`}
className="space-y-4"
>
{error && (
<div className="text-sm text-red-500 capitalize">{error}</div>
)}
<div className="flex items-end justify-between gap-4">
<InputFile
id="image"
label="Gambar"
placeholder="Masukkan Url Gambar"
name="image"
className="border-0 bg-white shadow read-only:bg-gray-100 focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none disabled:bg-gray-100"
labelClassName="text-sm font-medium text-[#363636]"
containerClassName="flex-1"
/>
<Input
id="url"
label="Link"
placeholder="Masukkan Url Link"
name="url"
className="border-0 bg-white shadow read-only:bg-gray-100 focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none disabled:bg-gray-100"
labelClassName="text-sm font-medium text-[#363636]"
containerClassName="flex-1"
/>
<Button
isLoading={fetcher.state !== 'idle'}
disabled={fetcher.state !== 'idle'}
type="submit"
size="lg"
className="text-md h-[42px] rounded-md"
>
Save
</Button>
</div>
</fetcher.Form>
</RemixFormProvider>
</div>
</div>
)
}
+3 -3
View File
@@ -10,14 +10,14 @@ import { Input } from '~/components/ui/input'
import { TitleDashboard } from '~/components/ui/title-dashboard'
import { urlFriendlyCode } from '~/utils/formatter'
export const createCategorySchema = z.object({
export const categorySchema = z.object({
id: z.string().optional(),
name: z.string().min(3, 'Nama minimal 3 karakter'),
code: z.string(),
sequence: z.preprocess(Number, z.number().optional()),
description: z.string(),
})
export type TCategorySchema = z.infer<typeof createCategorySchema>
export type TCategorySchema = z.infer<typeof categorySchema>
type TProperties = {
categoryData?: TCategoryResponse
}
@@ -29,7 +29,7 @@ export const FormCategoryPage = (properties: TProperties) => {
const formMethods = useRemixForm<TCategorySchema>({
mode: 'onSubmit',
fetcher,
resolver: zodResolver(createCategorySchema),
resolver: zodResolver(categorySchema),
values: {
id: categoryData?.id || undefined,
code: categoryData?.code || '',
+3 -3
View File
@@ -10,7 +10,7 @@ import { Input } from '~/components/ui/input'
import { TitleDashboard } from '~/components/ui/title-dashboard'
import { urlFriendlyCode } from '~/utils/formatter'
export const createSubscribePlanSchema = z.object({
export const subscribePlanSchema = z.object({
id: z.string().optional(),
name: z.string().min(3, 'Nama minimal 3 karakter'),
code: z.string(),
@@ -18,7 +18,7 @@ export const createSubscribePlanSchema = z.object({
price: z.preprocess(Number, z.number().optional()),
status: z.number().optional(),
})
export type TSubscribePlanSchema = z.infer<typeof createSubscribePlanSchema>
export type TSubscribePlanSchema = z.infer<typeof subscribePlanSchema>
type TProperties = {
subscribePlanData?: TSubscribePlanSchema
}
@@ -30,7 +30,7 @@ export const FormSubscribePlanPage = (properties: TProperties) => {
const formMethods = useRemixForm<TSubscribePlanSchema>({
mode: 'onSubmit',
fetcher,
resolver: zodResolver(createSubscribePlanSchema),
resolver: zodResolver(subscribePlanSchema),
values: {
id: subscribePlanData?.id || undefined,
code: subscribePlanData?.code || '',
+3 -3
View File
@@ -10,12 +10,12 @@ import { Input } from '~/components/ui/input'
import { TitleDashboard } from '~/components/ui/title-dashboard'
import { urlFriendlyCode } from '~/utils/formatter'
export const createTagSchema = z.object({
export const tagSchema = z.object({
id: z.string().optional(),
name: z.string().min(3, 'Nama minimal 3 karakter'),
code: z.string(),
})
export type TTagSchema = z.infer<typeof createTagSchema>
export type TTagSchema = z.infer<typeof tagSchema>
type TProperties = {
tagData?: TTagResponse
}
@@ -27,7 +27,7 @@ export const FormTagPage = (properties: TProperties) => {
const formMethods = useRemixForm<TTagSchema>({
mode: 'onSubmit',
fetcher,
resolver: zodResolver(createTagSchema),
resolver: zodResolver(tagSchema),
values: {
id: tagData?.id || undefined,
code: tagData?.code || '',