feat: add category creation functionality with validation and API integration
This commit is contained in:
parent
661437a2d3
commit
eac8e0cb43
37
app/apis/admin/create-category.ts
Normal file
37
app/apis/admin/create-category.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
import { HttpServer } from '~/libs/http-server'
|
||||||
|
|
||||||
|
type TCategorySchema = {
|
||||||
|
code: string
|
||||||
|
name: string
|
||||||
|
}
|
||||||
|
const categoryResponseSchema = z.object({
|
||||||
|
data: z.object({
|
||||||
|
Message: z.string(),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
type TParameter = {
|
||||||
|
accessToken: string
|
||||||
|
payload: TCategorySchema
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createCategoryRequest = async (parameters: TParameter) => {
|
||||||
|
const { accessToken, payload } = parameters
|
||||||
|
try {
|
||||||
|
const { ...restPayload } = payload
|
||||||
|
const transformedPayload = {
|
||||||
|
...restPayload,
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = await HttpServer({ accessToken }).post(
|
||||||
|
'/api/category/create',
|
||||||
|
transformedPayload,
|
||||||
|
)
|
||||||
|
return categoryResponseSchema.parse(data)
|
||||||
|
} catch (error) {
|
||||||
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||||
|
return Promise.reject(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
101
app/pages/category-create/index.tsx
Normal file
101
app/pages/category-create/index.tsx
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
import { zodResolver } from '@hookform/resolvers/zod'
|
||||||
|
import { useEffect, useState, type ChangeEvent } from 'react'
|
||||||
|
import { useFetcher } from 'react-router'
|
||||||
|
import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
|
||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
import { Button } from '~/components/ui/button'
|
||||||
|
import { Input } from '~/components/ui/input'
|
||||||
|
import { TitleDashboard } from '~/components/ui/title-dashboard'
|
||||||
|
|
||||||
|
export const createCategorySchema = z.object({
|
||||||
|
code: z.string().min(3, 'Kode minimal 3 karakter'),
|
||||||
|
name: z.string().min(3, 'Nama minimal 3 karakter'),
|
||||||
|
})
|
||||||
|
export type TCategorySchema = z.infer<typeof createCategorySchema>
|
||||||
|
|
||||||
|
export const CreateCategoryPage = () => {
|
||||||
|
const fetcher = useFetcher()
|
||||||
|
const formMethods = useRemixForm<TCategorySchema>({
|
||||||
|
mode: 'onSubmit',
|
||||||
|
fetcher,
|
||||||
|
resolver: zodResolver(createCategorySchema),
|
||||||
|
})
|
||||||
|
const [error, setError] = useState<string>()
|
||||||
|
const [disabled, setDisabled] = useState(false)
|
||||||
|
const [code, setCode] = useState<string>('')
|
||||||
|
const [name, setName] = useState<string>('')
|
||||||
|
|
||||||
|
const { handleSubmit } = formMethods
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!fetcher.data?.success) {
|
||||||
|
setError(fetcher.data?.message)
|
||||||
|
setDisabled(false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [fetcher])
|
||||||
|
|
||||||
|
const handelCodeAuto = (name: ChangeEvent<HTMLInputElement>) => {
|
||||||
|
const inputName = name.target.value
|
||||||
|
setName(inputName)
|
||||||
|
const urlFriendlyCode = inputName
|
||||||
|
.toLowerCase()
|
||||||
|
.replaceAll(/\s+/g, '-')
|
||||||
|
.replaceAll(/[^\w-]+/g, '')
|
||||||
|
setCode(urlFriendlyCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="relative">
|
||||||
|
<TitleDashboard title="Buat Kategory" />
|
||||||
|
<div>
|
||||||
|
<RemixFormProvider {...formMethods}>
|
||||||
|
<fetcher.Form
|
||||||
|
method="post"
|
||||||
|
onSubmit={handleSubmit}
|
||||||
|
action="/actions/admin/category/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">
|
||||||
|
<Input
|
||||||
|
id="name"
|
||||||
|
label="Nama Kategory"
|
||||||
|
placeholder="Masukkan Nama Kategory"
|
||||||
|
name="name"
|
||||||
|
onChange={handelCodeAuto}
|
||||||
|
className="border-0 bg-white shadow focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none"
|
||||||
|
labelClassName="text-sm font-medium text-[#363636]"
|
||||||
|
containerClassName="flex-1"
|
||||||
|
value={name}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
id="code"
|
||||||
|
label="Kode Kategory"
|
||||||
|
readOnly
|
||||||
|
placeholder="Masukkan Kode Kategory"
|
||||||
|
name="code"
|
||||||
|
className="border-0 bg-gray-100 shadow focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none"
|
||||||
|
labelClassName="text-sm font-medium text-[#363636]"
|
||||||
|
containerClassName="flex-1"
|
||||||
|
value={code}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
disabled={disabled}
|
||||||
|
type="submit"
|
||||||
|
size="lg"
|
||||||
|
className="text-md h-[42px] rounded-md"
|
||||||
|
>
|
||||||
|
Save
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</fetcher.Form>
|
||||||
|
</RemixFormProvider>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
11
app/routes/_admin.lg-admin.category.create.tsx
Normal file
11
app/routes/_admin.lg-admin.category.create.tsx
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { AdminDashboardLayout } from '~/layouts/admin/dashboard'
|
||||||
|
import { CreateCategoryPage } from '~/pages/category-create'
|
||||||
|
|
||||||
|
const DashboardContentsLayout = () => {
|
||||||
|
return (
|
||||||
|
<AdminDashboardLayout>
|
||||||
|
<CreateCategoryPage />
|
||||||
|
</AdminDashboardLayout>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
export default DashboardContentsLayout
|
||||||
67
app/routes/actions.admin.category.create.ts
Normal file
67
app/routes/actions.admin.category.create.ts
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
import { zodResolver } from '@hookform/resolvers/zod'
|
||||||
|
import { data } from 'react-router'
|
||||||
|
import { getValidatedFormData } from 'remix-hook-form'
|
||||||
|
import { XiorError } from 'xior'
|
||||||
|
|
||||||
|
import { createCategoryRequest } from '~/apis/admin/create-category'
|
||||||
|
import { handleCookie } from '~/libs/cookies'
|
||||||
|
import {
|
||||||
|
createCategorySchema,
|
||||||
|
type TCategorySchema,
|
||||||
|
} from '~/pages/category-create'
|
||||||
|
|
||||||
|
import type { Route } from './+types/actions.register'
|
||||||
|
|
||||||
|
export const action = async ({ request }: Route.ActionArgs) => {
|
||||||
|
const { staffToken } = await handleCookie(request)
|
||||||
|
try {
|
||||||
|
const {
|
||||||
|
errors,
|
||||||
|
data: payload,
|
||||||
|
receivedValues: defaultValues,
|
||||||
|
} = await getValidatedFormData<TCategorySchema>(
|
||||||
|
request,
|
||||||
|
zodResolver(createCategorySchema),
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
|
||||||
|
if (errors) {
|
||||||
|
return data({ success: false, errors, defaultValues }, { status: 400 })
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data: categoryData } = await createCategoryRequest({
|
||||||
|
accessToken: staffToken,
|
||||||
|
payload,
|
||||||
|
})
|
||||||
|
|
||||||
|
return data(
|
||||||
|
{
|
||||||
|
success: true,
|
||||||
|
categoryData,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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