feat: migrate tag creation and update functionality to new form structure
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { CreateTagsPage } from '~/pages/dashboard-tags-create'
|
||||
import { FormTagPage } from '~/pages/form-tag'
|
||||
|
||||
const DashboardTagsCreateLayout = () => <CreateTagsPage />
|
||||
const DashboardTagsCreateLayout = () => <FormTagPage />
|
||||
export default DashboardTagsCreateLayout
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { getTags } from '~/apis/common/get-tags'
|
||||
import { handleCookie } from '~/libs/cookies'
|
||||
import { FormTagPage } from '~/pages/form-tag'
|
||||
|
||||
import type { Route } from './+types/_admin.lg-admin._dashboard.tags.update.$id'
|
||||
|
||||
export const loader = async ({ request, params }: Route.LoaderArgs) => {
|
||||
const { staffToken } = await handleCookie(request)
|
||||
const { data: tagsData } = await getTags({
|
||||
accessToken: staffToken,
|
||||
})
|
||||
const tagData = tagsData.find((tag) => tag.id === params.id)
|
||||
return { tagData }
|
||||
}
|
||||
|
||||
const DashboardTagUpdateLayout = ({ loaderData }: Route.ComponentProps) => {
|
||||
const tagData = loaderData.tagData
|
||||
return <FormTagPage tagData={tagData} />
|
||||
}
|
||||
export default DashboardTagUpdateLayout
|
||||
@@ -5,10 +5,7 @@ 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 { createTagSchema, type TTagSchema } from '~/pages/form-tag'
|
||||
|
||||
import type { Route } from './+types/actions.register'
|
||||
|
||||
@@ -21,7 +18,7 @@ export const action = async ({ request }: Route.ActionArgs) => {
|
||||
receivedValues: defaultValues,
|
||||
} = await getValidatedFormData<TTagSchema>(
|
||||
request,
|
||||
zodResolver(createTagsSchema),
|
||||
zodResolver(createTagSchema),
|
||||
false,
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import { zodResolver } from '@hookform/resolvers/zod'
|
||||
import { data } from 'react-router'
|
||||
import { getValidatedFormData } from 'remix-hook-form'
|
||||
import { XiorError } from 'xior'
|
||||
|
||||
import { updateTagRequest } from '~/apis/admin/update-tag'
|
||||
import { handleCookie } from '~/libs/cookies'
|
||||
import { createTagSchema, type TTagSchema } from '~/pages/form-tag'
|
||||
|
||||
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(createTagSchema),
|
||||
false,
|
||||
)
|
||||
|
||||
if (errors) {
|
||||
return data({ success: false, errors, defaultValues }, { status: 400 })
|
||||
}
|
||||
|
||||
const { data: tagData } = await updateTagRequest({
|
||||
accessToken: staffToken,
|
||||
payload,
|
||||
})
|
||||
|
||||
return data(
|
||||
{
|
||||
success: true,
|
||||
tagData,
|
||||
},
|
||||
{
|
||||
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 },
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user