feat: implement tag creation functionality with validation and routing

This commit is contained in:
fredy.siswanto
2025-03-07 19:31:37 +07:00
parent c91fe48eb9
commit 2b5c3b1111
6 changed files with 223 additions and 7 deletions
@@ -0,0 +1,4 @@
import { CreateTagsPage } from '~/pages/dashboard-tags-create'
const DashboardTagsCreateLayout = () => <CreateTagsPage />
export default DashboardTagsCreateLayout
+67
View 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 { createTagsRequest } from '~/apis/admin/create-tags'
import { handleCookie } from '~/libs/cookies'
import {
createTagsSchema,
type TTagSchema,
} from '~/pages/dashboard-tags-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<TTagSchema>(
request,
zodResolver(createTagsSchema),
false,
)
if (errors) {
return data({ success: false, errors, defaultValues }, { status: 400 })
}
const { data: tagsData } = await createTagsRequest({
accessToken: staffToken,
payload,
})
return data(
{
success: true,
tagsData,
},
{
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 },
)
}
}