mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-07-27 02:04:39 +10:00
feat: refactor news and migrate rest of useFetch to $dropFetch
This commit is contained in:
+34
-29
@@ -1,35 +1,40 @@
|
||||
export const useNews = () => {
|
||||
const getAll = async (options?: {
|
||||
limit?: number;
|
||||
skip?: number;
|
||||
orderBy?: "asc" | "desc";
|
||||
tags?: string[];
|
||||
search?: string;
|
||||
}) => {
|
||||
const query = new URLSearchParams();
|
||||
import type { Article } from "@prisma/client";
|
||||
import type { SerializeObject } from "nitropack";
|
||||
|
||||
if (options?.limit) query.set("limit", options.limit.toString());
|
||||
if (options?.skip) query.set("skip", options.skip.toString());
|
||||
if (options?.orderBy) query.set("order", options.orderBy);
|
||||
if (options?.tags?.length) query.set("tags", options.tags.join(","));
|
||||
if (options?.search) query.set("search", options.search);
|
||||
export const useNews = () =>
|
||||
useState<
|
||||
| Array<
|
||||
SerializeObject<
|
||||
Article & {
|
||||
tags: Array<{ id: string; name: string }>;
|
||||
author: { displayName: string; id: string } | null;
|
||||
}
|
||||
>
|
||||
>
|
||||
| undefined
|
||||
>("news", () => undefined);
|
||||
|
||||
return await useFetch(`/api/v1/news?${query.toString()}`);
|
||||
};
|
||||
export const fetchNews = async (options?: {
|
||||
limit?: number;
|
||||
skip?: number;
|
||||
orderBy?: "asc" | "desc";
|
||||
tags?: string[];
|
||||
search?: string;
|
||||
}) => {
|
||||
const query = new URLSearchParams();
|
||||
|
||||
const getById = async (id: string) => {
|
||||
return await useFetch(`/api/v1/news/${id}`);
|
||||
};
|
||||
if (options?.limit) query.set("limit", options.limit.toString());
|
||||
if (options?.skip) query.set("skip", options.skip.toString());
|
||||
if (options?.orderBy) query.set("order", options.orderBy);
|
||||
if (options?.tags?.length) query.set("tags", options.tags.join(","));
|
||||
if (options?.search) query.set("search", options.search);
|
||||
|
||||
const remove = async (id: string) => {
|
||||
return await $dropFetch(`/api/v1/admin/news/${id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
};
|
||||
const news = useNews();
|
||||
|
||||
return {
|
||||
getAll,
|
||||
getById,
|
||||
remove,
|
||||
};
|
||||
// @ts-ignore
|
||||
const newValue = await $dropFetch(`/api/v1/news?${query.toString()}`);
|
||||
|
||||
news.value = newValue;
|
||||
|
||||
return newValue;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user