refactor: reorganize API imports and simplify parameter handling in various components

This commit is contained in:
Ardeman 2025-03-09 10:23:11 +08:00
parent 687e3c8d01
commit d38bf3a705
10 changed files with 26 additions and 16 deletions

View File

@ -15,10 +15,10 @@ type TParameters = {
} }
export const updateTagRequest = async (parameters: TParameters) => { export const updateTagRequest = async (parameters: TParameters) => {
const { accessToken, payload } = parameters const { payload, ...restParameters } = parameters
try {
const { id, ...restPayload } = payload const { id, ...restPayload } = payload
const { data } = await HttpServer({ accessToken }).put( try {
const { data } = await HttpServer(restParameters).put(
`/api/tag/${id}/update`, `/api/tag/${id}/update`,
restPayload, restPayload,
) )

View File

@ -1,6 +1,6 @@
import { z } from 'zod' import { z } from 'zod'
import { newsResponseSchema } from '~/apis/admin/get-news' import { newsResponseSchema } from '~/apis/common/get-news'
import { HttpServer, type THttpServer } from '~/libs/http-server' import { HttpServer, type THttpServer } from '~/libs/http-server'
const dataResponseSchema = z.object({ const dataResponseSchema = z.object({

View File

@ -30,10 +30,20 @@ const dataResponseSchema = z.object({
export type TNewsResponse = z.infer<typeof newsResponseSchema> export type TNewsResponse = z.infer<typeof newsResponseSchema>
export type TAuthor = z.infer<typeof authorSchema> export type TAuthor = z.infer<typeof authorSchema>
type TParameters = {
categories?: string[]
tags?: string[]
} & THttpServer
export const getNews = async (parameters: THttpServer) => { export const getNews = async (parameters: TParameters) => {
const { categories, tags, ...restParameters } = parameters
try { try {
const { data } = await HttpServer(parameters).get(`/api/news`) const { data } = await HttpServer(restParameters).get(`/api/news`, {
params: {
...(categories && { categories: categories.join('+') }),
...(tags && { tags: tags.join('+') }),
},
})
return dataResponseSchema.parse(data) return dataResponseSchema.parse(data)
} catch (error) { } catch (error) {
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject // eslint-disable-next-line unicorn/no-useless-promise-resolve-reject

View File

@ -3,11 +3,11 @@ import type { ComponentProps, FC, PropsWithChildren } from 'react'
type TProperties = PropsWithChildren<ComponentProps<'div'>> type TProperties = PropsWithChildren<ComponentProps<'div'>>
export const Card: FC<TProperties> = (properties) => { export const Card: FC<TProperties> = (properties) => {
const { children, ...rest } = properties const { children, ...restProperties } = properties
return ( return (
<div <div
className="border-[.2px] border-black/20 bg-white p-[30px]" className="border-[.2px] border-black/20 bg-white p-[30px]"
{...rest} {...restProperties}
> >
{children} {children}
</div> </div>

View File

@ -53,7 +53,7 @@ export const Combobox = <TFormValues extends Record<string, unknown>>(
className, className,
labelClassName, labelClassName,
containerClassName, containerClassName,
...rest ...restProperties
} = properties } = properties
const { const {
control, control,
@ -88,7 +88,7 @@ export const Combobox = <TFormValues extends Record<string, unknown>>(
onChange={field.onChange} onChange={field.onChange}
disabled={disabled} disabled={disabled}
immediate immediate
{...rest} {...restProperties}
> >
<div className="relative"> <div className="relative">
<ComboboxInput <ComboboxInput

View File

@ -40,7 +40,7 @@ export const Input = <TFormValues extends Record<string, unknown>>(
className, className,
containerClassName, containerClassName,
labelClassName, labelClassName,
...rest ...restProperties
} = properties } = properties
const [inputType, setInputType] = useState(type) const [inputType, setInputType] = useState(type)
@ -68,7 +68,7 @@ export const Input = <TFormValues extends Record<string, unknown>>(
)} )}
placeholder={inputType === 'password' ? '******' : placeholder} placeholder={inputType === 'password' ? '******' : placeholder}
{...register(name, rules)} {...register(name, rules)}
{...rest} {...restProperties}
/> />
{type === 'password' && ( {type === 'password' && (
<Button <Button

View File

@ -1,4 +1,4 @@
import type { TAuthor } from '~/apis/admin/get-news' import type { TAuthor } from '~/apis/common/get-news'
import { ProfileIcon } from '~/components/icons/profile' import { ProfileIcon } from '~/components/icons/profile'
import { formatDate } from '~/utils/formatter' import { formatDate } from '~/utils/formatter'

View File

@ -2,8 +2,8 @@ import DT from 'datatables.net-dt'
import DataTable from 'datatables.net-react' import DataTable from 'datatables.net-react'
import { Link, useRouteLoaderData } from 'react-router' import { Link, useRouteLoaderData } from 'react-router'
import type { TNewsResponse } from '~/apis/admin/get-news'
import type { TCategoryResponse } from '~/apis/common/get-categories' import type { TCategoryResponse } from '~/apis/common/get-categories'
import type { TNewsResponse } from '~/apis/common/get-news'
import type { TTagResponse } from '~/apis/common/get-tags' import type { TTagResponse } from '~/apis/common/get-tags'
import { Button } from '~/components/ui/button' import { Button } from '~/components/ui/button'
import { UiTable } from '~/components/ui/table' import { UiTable } from '~/components/ui/table'

View File

@ -5,7 +5,7 @@ import { useFetcher, useNavigate, useRouteLoaderData } from 'react-router'
import { RemixFormProvider, useRemixForm } from 'remix-hook-form' import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
import { z } from 'zod' import { z } from 'zod'
import type { newsResponseSchema } from '~/apis/admin/get-news' import type { newsResponseSchema } from '~/apis/common/get-news'
import { TextEditor } from '~/components/text-editor' import { TextEditor } from '~/components/text-editor'
import { Button } from '~/components/ui/button' import { Button } from '~/components/ui/button'
import { Combobox } from '~/components/ui/combobox' import { Combobox } from '~/components/ui/combobox'

View File

@ -1,4 +1,4 @@
import { getNews } from '~/apis/admin/get-news' import { getNews } from '~/apis/common/get-news'
import { handleCookie } from '~/libs/cookies' import { handleCookie } from '~/libs/cookies'
import { ContentsPage } from '~/pages/dashboard-contents' import { ContentsPage } from '~/pages/dashboard-contents'