|
|
@@ -1,36 +1,60 @@
|
|
|
import { Card, message, Button, Space, Spin } from 'antd';
|
|
|
-import { useQuery, useMutation, useQueryClient ,QueryClient,QueryClientProvider} from '@tanstack/react-query';
|
|
|
+import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
import ExcelToJson from '../components/ExcelToJson/index';
|
|
|
-import type { Template } from '@/share/exceltypes';
|
|
|
+import type { ExcelConfig } from '@/client/member/components/ExcelToJson/types';
|
|
|
import { useNavigate, useParams } from 'react-router';
|
|
|
+import { excelTemplateClient } from '@/client/api';
|
|
|
+import type { InferResponseType, InferRequestType } from 'hono/client';
|
|
|
+
|
|
|
+type TemplateDetail = InferResponseType<typeof excelTemplateClient[':id']['$get'], 200>;
|
|
|
+type CreateTemplateRequest = InferRequestType<typeof excelTemplateClient.$post>['json'];
|
|
|
+type UpdateTemplateRequest = InferRequestType<typeof excelTemplateClient[':id']['$put']>['json'];
|
|
|
|
|
|
function TemplateEdit() {
|
|
|
const navigate = useNavigate();
|
|
|
- const { id } = useParams();
|
|
|
+ const { id: idStr } = useParams();
|
|
|
+ const id = Number(idStr);
|
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
- const { data: template, isLoading } = useQuery<Template>({
|
|
|
+ const { data: template, isLoading } = useQuery({
|
|
|
queryKey: ['templates', 'detail', id],
|
|
|
queryFn: async () => {
|
|
|
if (!id) return null;
|
|
|
- const response = await requestWithToken({
|
|
|
- url: `/v1/member/templates?id=${id}`,
|
|
|
- method: 'GET'
|
|
|
+ const res = await excelTemplateClient[':id'].$get({
|
|
|
+ param: { id }
|
|
|
});
|
|
|
- return response.data;
|
|
|
+ if (res.status !== 200) {
|
|
|
+ const data = await res.json()
|
|
|
+ throw new Error(data.message);
|
|
|
+ }
|
|
|
+ return await res.json();
|
|
|
},
|
|
|
enabled: !!id
|
|
|
});
|
|
|
|
|
|
const { mutate: saveTemplate, isPending } = useMutation({
|
|
|
- mutationFn: async (data: Partial<Template>) => {
|
|
|
- const response = await requestWithToken({
|
|
|
- url: id ? `/v1/member/templates?id=${id}` : '/v1/member/templates',
|
|
|
- method: id ? 'PUT' : 'POST',
|
|
|
- data: data
|
|
|
- });
|
|
|
- return response.data;
|
|
|
+ mutationFn: async (data: CreateTemplateRequest | UpdateTemplateRequest) => {
|
|
|
+ if (id) {
|
|
|
+ const res = await excelTemplateClient[':id'].$put({
|
|
|
+ param: { id },
|
|
|
+ json: data as UpdateTemplateRequest
|
|
|
+ });
|
|
|
+ if (res.status !== 200) {
|
|
|
+ const data = await res.json()
|
|
|
+ throw new Error(data.message);
|
|
|
+ }
|
|
|
+ return await res.json();
|
|
|
+ } else {
|
|
|
+ const res = await excelTemplateClient.$post({
|
|
|
+ json: data as CreateTemplateRequest
|
|
|
+ });
|
|
|
+ if (res.status !== 200) {
|
|
|
+ const data = await res.json()
|
|
|
+ throw new Error(data.message);
|
|
|
+ }
|
|
|
+ return await res.json();
|
|
|
+ }
|
|
|
},
|
|
|
onSuccess: () => {
|
|
|
message.success('保存成功');
|