import req from 'editorReq'; import { useQuery } from 'react-query'; import { TResponce1 } from 'src/type'; import { API_CMS_ADMIN } from './config'; import _ from 'lodash'; import { useState } from 'react'; import { tablePageSize } from 'editorSrc/pages/config/table'; export const ARTICLE_GET_ALL = (page: number, pageSize: number) => { return `${API_CMS_ADMIN}/content/article/list/${pageSize}/${page}/created_at/desc/complex`; }; export async function getAll(url: string, param: ISearchParam) { return req({ url, method: 'PUT', data: _.pickBy({ ...param, hasMaterials: true }, _.identity), }).then((data: TResponce1) => { return { ...data, items: data.items.map((item) => ({ ...item, key: item.id, })), }; }) as Promise>; } export interface ISearchParam { cate_id?: string; keyword?: string; id?: string; } const defaultSearchParam = { cate_id: '', id: '', keyword: '', }; export function useArticle() { const [searchParam, setSearchParam] = useState(defaultSearchParam); const [page, setPage] = useState(1); const [pageSize, setPageSize] = useState(tablePageSize); const url = ARTICLE_GET_ALL(page, pageSize); const { data, ...rest } = useQuery([url, searchParam], () => getAll(url, searchParam)); const onSearch = (searchParam: ISearchParam) => { setPage(1); setSearchParam((old) => ({ ...old, ...searchParam })); }; const reset = () => { setSearchParam({ ...defaultSearchParam }); setPage(1); }; return { data, onSearch, setPageSize, setPage, reset, ...rest, }; } export interface IArticle { key: string; status: 0 | 1; abstract: string; cate_id: string; cate_pid: string; created_at: string; deleted_at: null | string; id: string; likes: number; updated_at: string; _id: string; _title: string; category: { name: 'string'; }; } export interface IArticles { firstPage: boolean; hasNextPage: boolean; hasPrePage: boolean; items: IArticle[]; lastPage: boolean; limit: number; nextPage: number; page: number; prePage: number; totalCount: number; totalPages: number; }