From a58c2ccc889d038b5b087859d4bfc50bb7418600 Mon Sep 17 00:00:00 2001 From: hutanglong Date: Sun, 24 Sep 2023 18:37:21 +0800 Subject: [PATCH] update item --- .../console-new/src/page/item/create.tsx | 107 +++++++++++++++--- .../console-new/src/page/item/index.tsx | 41 +++++-- .../console-new/src/page/item/service.ts | 40 ++++--- .../console-new/src/page/tenant/service.ts | 18 ++- 4 files changed, 161 insertions(+), 45 deletions(-) diff --git a/threadpool/console-new/src/page/item/create.tsx b/threadpool/console-new/src/page/item/create.tsx index 860aef60..a776a039 100644 --- a/threadpool/console-new/src/page/item/create.tsx +++ b/threadpool/console-new/src/page/item/create.tsx @@ -1,29 +1,104 @@ import { useRequest } from 'ahooks'; -import { Form, Modal, Input, Select } from 'antd'; -import React from 'react'; -import { fetchTenantList } from '../tenant/service'; +import { Form, Modal, Input, Select, notification } from 'antd'; +import React, { useEffect } from 'react'; +import { fetchTenantOptions } from '../tenant/service'; +import { fetchAddItem, fetchUpdateItem } from './service'; -const ItemCreate: React.FC<{ +interface IProps { type: string; data: any; visible: boolean; onClose: () => void; -}> = props => { - const { visible, onClose, data, type } = props; - const { data: tenant } = useRequest(fetchTenantList); - console.log('tenant', tenant); + reset: () => void; +} + +const ItemCreate: React.FC = props => { + const { visible, onClose, data, type, reset } = props; + const [form] = Form.useForm(); + const tenantRequest = useRequest(fetchTenantOptions, { + ready: !!type, + }); + const updateRequest = useRequest(fetchUpdateItem, { + manual: true, + onSuccess: () => { + notification.success({ message: '更新成功' }); + form.resetFields(); + onClose(); + reset(); + }, + onError: err => { + notification.error({ message: err.message }); + }, + }); + const addRequest = useRequest(fetchAddItem, { + manual: true, + onSuccess: () => { + notification.success({ message: '创建成功' }); + form.resetFields(); + onClose(); + reset(); + }, + onError: err => { + notification.error({ message: err.message }); + }, + }); + const onSave = () => { + form + .validateFields() + .then(values => { + addRequest.run({ ...values }); + }) + .catch(info => { + console.log('Validate Failed:', info); + }); + }; + const onUpdate = () => { + form + .validateFields() + .then(values => { + updateRequest.run({ id: data.id, ...values }); + }) + .catch(info => { + console.log('Validate Failed:', info); + }); + }; + useEffect(() => { + if (type === 'edit') { + form.setFieldsValue({ + ...data, + }); + } + }, [type, data, form]); return ( - -
- - {/* */} + + + + + + + - - + + - - + + diff --git a/threadpool/console-new/src/page/item/index.tsx b/threadpool/console-new/src/page/item/index.tsx index ea506206..cf64f5ca 100644 --- a/threadpool/console-new/src/page/item/index.tsx +++ b/threadpool/console-new/src/page/item/index.tsx @@ -1,8 +1,8 @@ -import { useAntdTable } from 'ahooks'; -import { Button, Form, Input, Row, Space, Table, Col, Modal } from 'antd'; +import { useAntdTable, useRequest } from 'ahooks'; +import { Button, Form, Input, Row, Space, Table, Col, Modal, notification } from 'antd'; import { SearchOutlined, EditOutlined } from '@ant-design/icons'; import React, { useState } from 'react'; -import { fetchItemList } from './service'; +import { fetchDeleteItem, fetchItemList } from './service'; import { useUrlSet } from '@/hooks/useUrlSet'; import style from './index.module.less'; import ItemCreate from './create'; @@ -11,17 +11,16 @@ const baseColumns = [ { title: '序号', dataIndex: 'index', - with: 200, }, { title: '租户', dataIndex: 'tenantId', - with: 200, + // with: 200, }, { title: '项目', dataIndex: 'itemId', - with: 200, + // with: 200, }, { title: '项目名称', @@ -46,16 +45,26 @@ const Tenant: React.FC = () => { const [form] = Form.useForm(); const { setUrl } = useUrlSet({ form }); const { tableProps, search } = useAntdTable(fetchItemList, { form }); - // const {run: delete} = useRequest(fetchDeleteTenant, { manual: true }); + const deleteRequest = useRequest(fetchDeleteItem, { manual: true }); + const handleSearch = () => { setUrl(); search.submit(); }; const handleDelete = (item: any) => { Modal.confirm({ - title: `此操作将删除${item.itemName}, 是否继续?`, - onOk: () => { - search.submit(); + title: '提示', + content: `此操作将删除${item.itemName}, 是否继续?`, + onOk: async () => { + try { + const res = await deleteRequest.runAsync(item.itemId); + if (res && res.success) { + notification.success({ message: '删除成功' }); + search.reset(); + } + } catch (e) { + notification.error({ message: '删除失败' }); + } }, }); }; @@ -95,7 +104,7 @@ const Tenant: React.FC = () => { - @@ -131,7 +140,15 @@ const Tenant: React.FC = () => { }, ]} /> - + {editVisible && ( + search.reset()} + /> + )} ); }; diff --git a/threadpool/console-new/src/page/item/service.ts b/threadpool/console-new/src/page/item/service.ts index babf4216..0e8bbdf9 100644 --- a/threadpool/console-new/src/page/item/service.ts +++ b/threadpool/console-new/src/page/item/service.ts @@ -19,10 +19,10 @@ const fetchItemList = async ( list: res.data.records.map((item: any, index: number) => ({ index: index + 1, ...item })), }; } - throw new Error(res.msg || '服务器开小差啦~'); + throw new Error(res.message || '服务器开小差啦~'); }; -const fetchAddTenant = async (params: { +const fetchAddItem = async (params: { itemDesc: string; // 项目简介 itemId: string; // 项目 itemName: string; // 项目名称 @@ -31,39 +31,47 @@ const fetchAddTenant = async (params: { tenantDesc?: string; tenantName?: string; }) => { - const res = await request('/hippo4j/v1/cs/tenant/save', { + const res = await request('/hippo4j/v1/cs/item/save', { method: 'POST', body: { ...params }, }); - if (res && res.success) { return res; } - throw new Error(res.msg || '服务器开小差啦~'); + throw new Error(res.message || '服务器开小差啦~'); }; -const fetchDeleteItem = async (id: string) => { - const url = 'hippo4j/v1/cs/item/delete/prescription/' + id; - const res = await request(url, { - method: 'DELETE', +const fetchUpdateItem = async (params: { + id: string; + itemDesc: string; // 项目简介 + itemId: string; // 项目 + itemName: string; // 项目名称 + owner: string; // 负责人 + tenantId: string; // 租户 + tenantDesc?: string; + tenantName?: string; +}) => { + const res = await request('/hippo4j/v1/cs/item/update', { + method: 'POST', + body: { ...params }, }); if (res && res.success) { return res; } - throw new Error(res.msg || '服务器开小差啦~'); + throw new Error(res.message || '服务器开小差啦~'); }; -const fetchUpdateItem = async (id: string) => { - const res = await request('/hippo4j/v1/cs/item/update', { - method: 'POST', - params: { id }, +const fetchDeleteItem = async (id: string) => { + const url = 'hippo4j/v1/cs/item/delete/prescription/' + id; + const res = await request(url, { + method: 'DELETE', }); if (res && res.success) { return res; } - throw new Error(res.msg || '服务器开小差啦~'); + throw new Error(res.message || '服务器开小差啦~'); }; -export { fetchItemList, fetchAddTenant, fetchDeleteItem, fetchUpdateItem }; +export { fetchItemList, fetchAddItem, fetchDeleteItem, fetchUpdateItem }; diff --git a/threadpool/console-new/src/page/tenant/service.ts b/threadpool/console-new/src/page/tenant/service.ts index 181163e4..b271d0e2 100644 --- a/threadpool/console-new/src/page/tenant/service.ts +++ b/threadpool/console-new/src/page/tenant/service.ts @@ -1,5 +1,21 @@ import request from '@/utils'; +const fetchTenantOptions = async (tencent: string) => { + const res: any = await request('/hippo4j/v1/cs/tenant/query/page', { + method: 'POST', + body: { + tencent, + current: 1, + size: 10, + desc: true, + }, + }); + if (res && res.success) { + return res.data && res.data.records.map((item: any) => ({ value: item.tenantId, label: item.tenantName })); + } + throw new Error(res.msg || '服务器开小差啦~'); +}; + const fetchTenantList = async ( pageProps: { current: number; pageSize: number }, formData: { tencent: string | number } @@ -70,4 +86,4 @@ const fetchTenantDetail = async (id: string) => { throw new Error(res.msg || '服务器开小差啦~'); }; -export { fetchTenantList, fetchAddTenant, fetchDeleteTenant, fetchUpdateTenant, fetchTenantDetail }; +export { fetchTenantOptions, fetchTenantList, fetchAddTenant, fetchDeleteTenant, fetchUpdateTenant, fetchTenantDetail };