|
| 1 | +/* |
| 2 | +Licensed to LinDB under one or more contributor |
| 3 | +license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright |
| 5 | +ownership. LinDB licenses this file to you under |
| 6 | +the Apache License, Version 2.0 (the "License"); you may |
| 7 | +not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + |
| 11 | +Unless required by applicable law or agreed to in writing, |
| 12 | +software distributed under the License is distributed on an |
| 13 | +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +KIND, either express or implied. See the License for the |
| 15 | +specific language governing permissions and limitations |
| 16 | +under the License. |
| 17 | +*/ |
| 18 | +import React, { useCallback, useEffect, useRef, useState } from 'react'; |
| 19 | +import { useRequest } from '@src/hooks'; |
| 20 | +import { ComponentSrv } from '@src/services'; |
| 21 | +import { Component, RoleList } from '@src/types'; |
| 22 | +import { isEmpty } from 'lodash-es'; |
| 23 | +import { Button, Col, Divider, Empty, Form, Row, Tree, Typography } from '@douyinfe/semi-ui'; |
| 24 | +import { Icon, Notification } from '@src/components'; |
| 25 | +import EmptyImg from '@src/images/empty.svg'; |
| 26 | +import './component.scss'; |
| 27 | +import { IconRefresh, IconSaveStroked } from '@douyinfe/semi-icons'; |
| 28 | +import { ApiKit } from '@src/utils'; |
| 29 | + |
| 30 | +const OrgComponent: React.FC = () => { |
| 31 | + const { result: navTree, refetch } = useRequest(['load_org_component_tree'], () => { |
| 32 | + return ComponentSrv.getOrgComponentTree(); |
| 33 | + }); |
| 34 | + const [expandedKeys, setExpandedKeys] = useState<string[]>([]); |
| 35 | + const [treeData, setTreeData] = useState<any[]>([]); |
| 36 | + const [editing, setEditing] = useState(false); |
| 37 | + const [submitting, setSubmitting] = useState(false); |
| 38 | + const [selectItem, setSelectItem] = useState<any>({}); |
| 39 | + const formApi = useRef<any>(); |
| 40 | + |
| 41 | + const buildTree = useCallback((navTree: Component[], parent: Component | null): any[] => { |
| 42 | + return navTree.map((nav: Component) => { |
| 43 | + const item = { |
| 44 | + label: nav.label, |
| 45 | + uid: nav.uid, |
| 46 | + key: nav.uid, |
| 47 | + value: nav.uid, |
| 48 | + icon: <Icon icon={nav.icon} style={{ marginRight: 8 }} />, |
| 49 | + raw: nav, |
| 50 | + parent: parent, |
| 51 | + children: !isEmpty(nav.children) ? buildTree(nav.children, nav) : [], |
| 52 | + }; |
| 53 | + return item; |
| 54 | + }); |
| 55 | + }, []); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + setTreeData(buildTree(navTree || [], null)); |
| 59 | + }, [navTree, buildTree]); |
| 60 | + |
| 61 | + return ( |
| 62 | + <Row className="menu-setting"> |
| 63 | + <Col span={10} className="menu-tree"> |
| 64 | + <div className="buttons"> |
| 65 | + <Button |
| 66 | + icon={<IconRefresh />} |
| 67 | + type="tertiary" |
| 68 | + onClick={() => { |
| 69 | + refetch(); |
| 70 | + }} |
| 71 | + /> |
| 72 | + </div> |
| 73 | + <Divider /> |
| 74 | + <Tree |
| 75 | + style={{ marginRight: 4 }} |
| 76 | + motion={false} |
| 77 | + draggable |
| 78 | + onExpand={(keys: string[]) => setExpandedKeys(keys)} |
| 79 | + expandedKeys={expandedKeys} |
| 80 | + treeData={treeData} |
| 81 | + renderLabel={(label: any, item: any) => ( |
| 82 | + <div style={{ display: 'flex', alignItems: 'center' }}> |
| 83 | + <Typography.Text |
| 84 | + ellipsis={{ showTooltip: true }} |
| 85 | + style={{ flex: 1 }} |
| 86 | + onClick={() => { |
| 87 | + setSelectItem(item.raw); |
| 88 | + formApi.current.setValues(item.raw, { isOverride: true }); |
| 89 | + setEditing(true); |
| 90 | + }}> |
| 91 | + {label} |
| 92 | + </Typography.Text> |
| 93 | + </div> |
| 94 | + )} |
| 95 | + /> |
| 96 | + </Col> |
| 97 | + <Col span={14} className="menu-form"> |
| 98 | + {!editing && ( |
| 99 | + <Empty |
| 100 | + title="Oops! No edit menu" |
| 101 | + style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', marginTop: 50 }} |
| 102 | + image={<img src={EmptyImg} style={{ width: 150, height: 150 }} />} |
| 103 | + darkModeImage={<img src={EmptyImg} style={{ width: 150, height: 150 }} />} |
| 104 | + layout="horizontal" |
| 105 | + description="Please select edit menu" |
| 106 | + /> |
| 107 | + )} |
| 108 | + |
| 109 | + <Form |
| 110 | + style={{ display: editing ? 'block' : 'none', paddingLeft: 24 }} |
| 111 | + className="linsight-form" |
| 112 | + labelPosition="left" |
| 113 | + labelAlign="right" |
| 114 | + labelWidth={120} |
| 115 | + allowEmpty |
| 116 | + onSubmit={async (values: Component) => { |
| 117 | + try { |
| 118 | + setSubmitting(true); |
| 119 | + await ComponentSrv.updateRoleOfOrgComponent([{ componentUid: selectItem.uid, role: values.role }]); |
| 120 | + refetch(); |
| 121 | + Notification.success('Component save successfully!'); |
| 122 | + } catch (err) { |
| 123 | + Notification.error(ApiKit.getErrorMsg(err)); |
| 124 | + } finally { |
| 125 | + setSubmitting(false); |
| 126 | + } |
| 127 | + }} |
| 128 | + getFormApi={(api: any) => { |
| 129 | + formApi.current = api; |
| 130 | + }}> |
| 131 | + <Form.Slot label="Label"> |
| 132 | + <Icon icon={selectItem.icon} /> |
| 133 | + <Typography.Text style={{ marginLeft: 4 }}>{selectItem.label}</Typography.Text> |
| 134 | + </Form.Slot> |
| 135 | + <Form.Select |
| 136 | + label="Role" |
| 137 | + field="role" |
| 138 | + optionList={RoleList} |
| 139 | + rules={[{ required: true, message: 'Label is required' }]} |
| 140 | + /> |
| 141 | + <Form.Slot> |
| 142 | + <Button |
| 143 | + icon={<IconSaveStroked />} |
| 144 | + loading={submitting} |
| 145 | + onClick={() => { |
| 146 | + formApi.current.submitForm(); |
| 147 | + }}> |
| 148 | + Save |
| 149 | + </Button> |
| 150 | + </Form.Slot> |
| 151 | + </Form> |
| 152 | + </Col> |
| 153 | + </Row> |
| 154 | + ); |
| 155 | +}; |
| 156 | + |
| 157 | +export default OrgComponent; |
0 commit comments