-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: magent-ui support knowledge curd operation
- Loading branch information
1 parent
af2d2dd
commit 5500303
Showing
10 changed files
with
378 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ModalContribution, singleton } from '@difizen/mana-app'; | ||
|
||
import { KnowledgeModal } from './modal.js'; | ||
|
||
@singleton({ contrib: [ModalContribution] }) | ||
export class KnowledgeModalContribution implements ModalContribution { | ||
registerModal() { | ||
return KnowledgeModal; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import type { ModalItem, ModalItemProps } from '@difizen/mana-app'; | ||
import { useInject, useMount } from '@difizen/mana-app'; | ||
import { Form, Input, message, Modal } from 'antd'; | ||
import React, { useCallback, useMemo, useState } from 'react'; | ||
import { history } from 'umi'; | ||
|
||
import { KnowledgeManager } from '../../../modules/knowledge/knowledge-manager.js'; | ||
import { KnowledgeSpace } from '../../../modules/knowledge/knowledge-space.js'; | ||
import { KnowledgeModalId } from '../protocol.js'; | ||
import { KnowledgeView } from '../view.js'; | ||
|
||
export const KnowledgeModalComponent = ( | ||
props: ModalItemProps<{ type: 'create' | 'edit'; knowledge_id?: string }>, | ||
) => { | ||
const knowledgeSpace = useInject(KnowledgeSpace); | ||
const knowledgeManager = useInject(KnowledgeManager); | ||
const instance = useInject(KnowledgeView); | ||
|
||
const { visible, close, data } = props; | ||
|
||
const [form] = Form.useForm(); | ||
|
||
const [nameValue, setName] = useState<string | undefined>(undefined); | ||
const [idValue, setId] = useState<string | undefined>(undefined); | ||
|
||
useMount(() => { | ||
knowledgeSpace.update(); | ||
}); | ||
|
||
const strategiesValues = useMemo( | ||
() => ({ | ||
create: { | ||
title: '创建知识库', | ||
okText: '创建', | ||
}, | ||
edit: { | ||
title: '编辑知识库', | ||
okText: '确定', | ||
}, | ||
}), | ||
[], | ||
); | ||
|
||
const strategiesMethod = useMemo( | ||
() => ({ | ||
create: async () => { | ||
const formValues = form.getFieldsValue(); | ||
const id = await knowledgeManager.createKnowledge( | ||
formValues.nickname, | ||
formValues.description, | ||
); | ||
if (id) { | ||
setId(id); | ||
message.success('创建成功'); | ||
history.push(`/portal/knowledge/${id}/upload`); | ||
} | ||
close(); | ||
}, | ||
edit: async () => { | ||
if (!data?.knowledge_id) { | ||
return; | ||
} | ||
const formValues = form.getFieldsValue(); | ||
const id = await knowledgeManager.updateKnowledge({ | ||
id: data.knowledge_id, | ||
nickname: formValues.nickname, | ||
description: formValues.description, | ||
}); | ||
if (id) { | ||
setId(id); | ||
message.success('更新成功'); | ||
await instance.update(); | ||
} | ||
close(); | ||
}, | ||
}), | ||
[close, data, form, instance, knowledgeManager], | ||
); | ||
|
||
const submit = useCallback(async () => { | ||
const method = strategiesMethod[data?.type || 'create']; | ||
if (method) { | ||
await method(); | ||
} else { | ||
console.error('Invalid type'); | ||
} | ||
}, [data, strategiesMethod]); | ||
|
||
return ( | ||
<Modal | ||
className="magent-knowledge-create-modal-form" | ||
open={visible} | ||
width={640} | ||
title={strategiesValues[data?.type || 'create'].title} | ||
onCancel={() => close()} | ||
cancelText="取消" | ||
onOk={() => submit()} | ||
okText={strategiesValues[data?.type || 'create'].okText} | ||
> | ||
<Form | ||
layout="vertical" | ||
form={form} | ||
onFieldsChange={(changed) => { | ||
const idChanged = changed.find( | ||
(item) => | ||
item.name instanceof Array && | ||
item.name.length === 1 && | ||
item.name[0] === 'nickname', | ||
); | ||
if (idChanged && idChanged.validated) { | ||
setName(idChanged.value); | ||
} | ||
}} | ||
> | ||
<Form.Item required label={'名称'} name="nickname"> | ||
<Input /> | ||
</Form.Item> | ||
{/* <Form.Item name="avatar" label="头像"> | ||
<AvatarUpload | ||
disabled={!nameValue} | ||
data={(file) => { | ||
const extname = path.extname(file.name); | ||
const filename = `${nameValue}${extname}`; | ||
return { | ||
file, | ||
filename, | ||
}; | ||
}} | ||
AvatarRender={UploadButton} | ||
/> | ||
</Form.Item> */} | ||
<Form.Item label="描述" name="description"> | ||
<Input /> | ||
</Form.Item> | ||
</Form> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export const KnowledgeModal: ModalItem = { | ||
id: KnowledgeModalId, | ||
component: KnowledgeModalComponent, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const KnowledgeModalId = 'knowledge.modal'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { InboxOutlined } from '@ant-design/icons'; | ||
import { BaseView, inject, prop, singleton, view } from '@difizen/mana-app'; | ||
import { message, Upload } from 'antd'; | ||
|
||
import './index.less'; | ||
import { history } from 'umi'; | ||
import { useParams } from 'umi'; | ||
|
||
import { MainView } from '@/modules/base-layout/main-view.js'; | ||
import type { NavigatablePage } from '@/modules/base-layout/protocol.js'; | ||
|
||
const { Dragger } = Upload; | ||
|
||
const viewId = 'magent-knowledge-upload'; | ||
export const uploadslot = `${viewId}-slot`; | ||
|
||
const KnowledgeUploadComponent = () => { | ||
const { knowledgeId } = useParams(); | ||
|
||
return ( | ||
<div className={`${viewId}-wrapper`}> | ||
<Dragger | ||
className={`${viewId}-dragger`} | ||
name="file" | ||
multiple | ||
data={{ | ||
knowledge_id: knowledgeId, | ||
}} | ||
method="post" | ||
action="/api/v1/knowledge/upload" | ||
listType="picture" | ||
onChange={(info) => { | ||
const { status } = info.file; | ||
if (status === 'done') { | ||
message.success(`${info.file.name} file uploaded successfully.`); | ||
} else if (status === 'error') { | ||
message.error(`${info.file.name} file upload failed.`); | ||
} | ||
}} | ||
// onDrop={(e) => {}} | ||
> | ||
<p className="ant-upload-drag-icon"> | ||
<InboxOutlined /> | ||
</p> | ||
<p className="ant-upload-text">Click or drag file to this area to upload</p> | ||
<p className="ant-upload-hint"> | ||
Support for a single or bulk upload. Strictly prohibited from uploading | ||
company data or other banned files. | ||
</p> | ||
</Dragger> | ||
</div> | ||
); | ||
}; | ||
|
||
@singleton() | ||
@view(viewId) | ||
export class KnowledgeUploadView extends BaseView implements NavigatablePage { | ||
@inject(MainView) protected mainView: MainView; | ||
|
||
override view = KnowledgeUploadComponent; | ||
|
||
override onViewUnmount(): void { | ||
this.mainView.active = undefined; | ||
} | ||
override onViewMount(): void { | ||
this.mainView.active = this; | ||
} | ||
|
||
goBack = () => history.push('/portal/knowledge'); | ||
|
||
pageTitle = () => <>上传知识</>; | ||
} |
Oops, something went wrong.