feat: 实现前端资源管理
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
import { ResourceBadge } from "@/components/resource";
|
import { ResourceBadge } from "@/components/resource";
|
||||||
import { Card, CardContent } from "@/components/ui/card";
|
import { Card, CardContent } from "@/components/ui/card";
|
||||||
import { Resource } from "@/lib/types/resource";
|
import { PublicResource } from "@/lib/types/resource";
|
||||||
import ResourceCardImage from "./ResourceCardImage";
|
import ResourceCardImage from "./ResourceCardImage";
|
||||||
|
|
||||||
interface ResourceCardProps extends React.HTMLProps<HTMLAnchorElement> {
|
interface ResourceCardProps extends React.HTMLProps<HTMLAnchorElement> {
|
||||||
r: Resource;
|
r: PublicResource;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ResourceCard({ r, ...props }: ResourceCardProps) {
|
export function ResourceCard({ r, ...props }: ResourceCardProps) {
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ import { UserInfoEditor } from "./components/user-info-editor";
|
|||||||
import { User } from "@/lib/types/user";
|
import { User } from "@/lib/types/user";
|
||||||
import { CreateUserEditor } from "./components/create-user-editor";
|
import { CreateUserEditor } from "./components/create-user-editor";
|
||||||
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog";
|
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog";
|
||||||
import { AdminApi } from "@/lib/api";
|
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { ApiError } from "next/dist/server/api-utils";
|
import { ApiError } from "next/dist/server/api-utils";
|
||||||
|
import { AdminAPI } from "@/lib/api/client";
|
||||||
|
|
||||||
|
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
@@ -56,7 +56,7 @@ export default function Page() {
|
|||||||
const [deletedUserId, setDeletedUserId] = useState('');
|
const [deletedUserId, setDeletedUserId] = useState('');
|
||||||
const handleUserDelete = async (userId: string) => {
|
const handleUserDelete = async (userId: string) => {
|
||||||
try {
|
try {
|
||||||
await AdminApi.user.remove(userId, false);
|
await AdminAPI.removeUser(userId, false);
|
||||||
toast.success('删除成功');
|
toast.success('删除成功');
|
||||||
handleUserDeleteLocal(userId, false);
|
handleUserDeleteLocal(userId, false);
|
||||||
setDeletedUserId('');
|
setDeletedUserId('');
|
||||||
|
|||||||
@@ -13,13 +13,13 @@ import {
|
|||||||
import { Input } from "@/components/ui/input"
|
import { Input } from "@/components/ui/input"
|
||||||
import { Label } from "@/components/ui/label"
|
import { Label } from "@/components/ui/label"
|
||||||
import { Button } from "@/components/ui/button"
|
import { Button } from "@/components/ui/button"
|
||||||
import { AdminApi } from "@/lib/api"
|
|
||||||
import { toast } from "sonner"
|
import { toast } from "sonner"
|
||||||
import { ApiError } from "next/dist/server/api-utils"
|
import { ApiError } from "next/dist/server/api-utils"
|
||||||
import { ResourceBadge } from "@/components/resource"
|
import { ResourceBadge } from "@/components/resource"
|
||||||
import AddResourceTag from "./AddResourceTag"
|
import AddResourceTag from "./AddResourceTag"
|
||||||
import { Textarea } from "@/components/ui/textarea"
|
import { Textarea } from "@/components/ui/textarea"
|
||||||
import { Plus } from "lucide-react"
|
import { Plus } from "lucide-react"
|
||||||
|
import { AdminAPI } from "@/lib/api/client"
|
||||||
|
|
||||||
|
|
||||||
interface AddResourceProps {
|
interface AddResourceProps {
|
||||||
@@ -42,7 +42,7 @@ export default function AddResource({ children, refresh }: AddResourceProps) {
|
|||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
try {
|
try {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
await AdminApi.web.resource.create({
|
await AdminAPI.createResource({
|
||||||
...formData,
|
...formData,
|
||||||
});
|
});
|
||||||
toast.success("添加成功");
|
toast.success("添加成功");
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import AddResourceTag from "./AddResourceTag"
|
|||||||
import { Textarea } from "@/components/ui/textarea"
|
import { Textarea } from "@/components/ui/textarea"
|
||||||
import { Plus } from "lucide-react"
|
import { Plus } from "lucide-react"
|
||||||
import { Resource } from "@/lib/types/resource"
|
import { Resource } from "@/lib/types/resource"
|
||||||
import { AdminApi } from "@/lib/api"
|
|
||||||
import useSWR from "swr"
|
import useSWR from "swr"
|
||||||
import { ApiError } from "next/dist/server/api-utils"
|
import { ApiError } from "next/dist/server/api-utils"
|
||||||
import { Skeleton } from "@/components/ui/skeleton"
|
import { Skeleton } from "@/components/ui/skeleton"
|
||||||
@@ -33,6 +32,7 @@ import {
|
|||||||
AlertDialogTitle,
|
AlertDialogTitle,
|
||||||
AlertDialogTrigger,
|
AlertDialogTrigger,
|
||||||
} from "@/components/ui/alert-dialog"
|
} from "@/components/ui/alert-dialog"
|
||||||
|
import { AdminAPI } from "@/lib/api/client"
|
||||||
|
|
||||||
interface ResourceEditProps {
|
interface ResourceEditProps {
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
@@ -45,7 +45,7 @@ export default function ResourceEdit({ children, id, onRefresh }: ResourceEditPr
|
|||||||
|
|
||||||
const { data: resource, isLoading, mutate } = useSWR<Resource>(
|
const { data: resource, isLoading, mutate } = useSWR<Resource>(
|
||||||
open ? [`/api/admin/web/resource/${id}`] : null,
|
open ? [`/api/admin/web/resource/${id}`] : null,
|
||||||
() => AdminApi.web.resource.get(id),
|
() => AdminAPI.getResource(id),
|
||||||
{
|
{
|
||||||
revalidateOnFocus: false,
|
revalidateOnFocus: false,
|
||||||
revalidateOnReconnect: false,
|
revalidateOnReconnect: false,
|
||||||
@@ -57,7 +57,7 @@ export default function ResourceEdit({ children, id, onRefresh }: ResourceEditPr
|
|||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
if (!resource) return;
|
if (!resource) return;
|
||||||
try {
|
try {
|
||||||
await AdminApi.web.resource.update(id, {
|
await AdminAPI.updateResource(id, {
|
||||||
title: resource.title,
|
title: resource.title,
|
||||||
description: resource.description,
|
description: resource.description,
|
||||||
imageUrl: resource.imageUrl,
|
imageUrl: resource.imageUrl,
|
||||||
@@ -74,7 +74,7 @@ export default function ResourceEdit({ children, id, onRefresh }: ResourceEditPr
|
|||||||
|
|
||||||
const handleRemove = async (id: string) => {
|
const handleRemove = async (id: string) => {
|
||||||
try {
|
try {
|
||||||
await AdminApi.web.resource.remove(id);
|
await AdminAPI.removeResource(id);
|
||||||
toast.success("资源删除成功");
|
toast.success("资源删除成功");
|
||||||
onRefresh();
|
onRefresh();
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import { AdminApi } from "@/lib/api";
|
import { AdminAPI } from "@/lib/api/client";
|
||||||
import { useCallback } from "react";
|
import { useCallback } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
@@ -8,7 +8,7 @@ import useSWR from "swr";
|
|||||||
export function useResourceList() {
|
export function useResourceList() {
|
||||||
const { data, error, isLoading, mutate } = useSWR(
|
const { data, error, isLoading, mutate } = useSWR(
|
||||||
['/admin/web/resource'],
|
['/admin/web/resource'],
|
||||||
() => AdminApi.web.resource.list(),
|
() => AdminAPI.listResources(),
|
||||||
{
|
{
|
||||||
onError: (e) => {
|
onError: (e) => {
|
||||||
toast.error(`${e.message || e}`)
|
toast.error(`${e.message || e}`)
|
||||||
|
|||||||
@@ -1,19 +1,6 @@
|
|||||||
|
import { PublicResource } from "@/lib/types/resource";
|
||||||
import { serverFetch } from "../server";
|
import { serverFetch } from "../server";
|
||||||
|
|
||||||
export type ResourceTagType = {
|
|
||||||
name: string;
|
|
||||||
type: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Resource {
|
|
||||||
id: string;
|
|
||||||
title: string;
|
|
||||||
description: string;
|
|
||||||
imageUrl: string;
|
|
||||||
link: string;
|
|
||||||
tags: ResourceTagType[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function list() {
|
export async function list() {
|
||||||
return serverFetch<Resource[]>('/api/resource')
|
return serverFetch<PublicResource[]>('/api/resource')
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,15 @@ export type TagType = {
|
|||||||
type: string;
|
type: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface PublicResource {
|
||||||
|
id: string;
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
imageUrl: string;
|
||||||
|
link: string;
|
||||||
|
tags: TagType[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface Resource {
|
export interface Resource {
|
||||||
id: string;
|
id: string;
|
||||||
title: string;
|
title: string;
|
||||||
@@ -10,4 +19,6 @@ export interface Resource {
|
|||||||
imageUrl: string;
|
imageUrl: string;
|
||||||
link: string;
|
link: string;
|
||||||
tags: TagType[];
|
tags: TagType[];
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user