Merge pull request 'merge_from_upstream' (#463) from merge_from_upstream into @hera/dev

Reviewed-on: daoyoucloud/tachycode#463
This commit is contained in:
sealday 2024-03-22 03:20:43 +08:00
commit f6646f5825
3 changed files with 50 additions and 42 deletions

View File

@ -4,12 +4,6 @@ import { connect, useFieldSchema } from '@formily/react';
import { useCollectionManager, useRequest } from '@nocobase/client'; import { useCollectionManager, useRequest } from '@nocobase/client';
import _ from 'lodash'; import _ from 'lodash';
interface Option {
name: string | number;
id: string;
children?: Option[];
}
const AssociationCascader = connect((props) => { const AssociationCascader = connect((props) => {
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const collection = fieldSchema['collectionName']; const collection = fieldSchema['collectionName'];
@ -18,35 +12,34 @@ const AssociationCascader = connect((props) => {
const titleField = cm.getCollection(collection).titleField; const titleField = cm.getCollection(collection).titleField;
const joinTitleField = cm.getCollection(collection + '.' + associationField).titleField; const joinTitleField = cm.getCollection(collection + '.' + associationField).titleField;
const params = {
appends: [associationField],
fields: [titleField],
pageSize: 99999,
filter: props?.params?.filter ? { ...props?.params?.filter } : {},
};
const { data } = useRequest<any>({ const { data } = useRequest<any>({
resource: collection, resource: collection,
action: 'list', action: 'list',
params: { params,
appends: [associationField],
fields: [titleField],
pageSize: 99999,
},
}); });
const options = useMemo(() => { const options = useMemo(() => {
const dict = {}; const dict: { [key: string]: string[] } =
data?.data.forEach((item) => { data?.data.reduce((acc, item) => {
if (item[associationField]) { if (item[associationField]) {
dict[item[associationField][joinTitleField]] ??= []; const key = item[associationField][joinTitleField];
dict[item[associationField][joinTitleField]].push(item[titleField]); acc[key] ??= [];
} acc[key].push(item[titleField]);
}); }
const options: Option[] = []; return acc;
_.forEach(dict, (values: any, key) => { }, {}) || {};
options.push({ const options = Object.entries(dict).map(([key, values]) => ({
name: key, name: key,
id: key, id: key,
children: values.map((value) => ({ children: values.map((value) => ({ name: value, id: value })),
name: value, }));
id: value,
})),
});
});
return options; return options;
}, [associationField, joinTitleField, titleField, data?.data]); }, [associationField, joinTitleField, titleField, data?.data]);
return <Cascader options={options} {...props} showSearch />; return <Cascader options={options} {...props} showSearch />;

View File

@ -18,20 +18,32 @@ export const RolesMenu: React.FC & {
Item: React.FC<{ item: any; onEdit: () => void }>; Item: React.FC<{ item: any; onEdit: () => void }>;
} = () => { } = () => {
const { t } = useACLTranslation(); const { t } = useACLTranslation();
const { data, run, loading } = useResourceActionContext(); const { data, mutate } = useResourceActionContext();
const [visible, setVisible] = useState(false); const [visible, setVisible] = useState(false);
const [record, setRecord] = useState(null); const [record, setRecord] = useState(null);
const { role, setRole } = useContext(RolesManagerContext); const { role, setRole } = useContext(RolesManagerContext);
const [roles, setRoles] = useState([]); const [roles, setRoles] = useState([]);
const [loading, setLoading] = useState(false);
const api = useAPIClient();
const loadMore = useCallback(() => { const loadMore = useCallback(async () => {
const meta = data?.meta; const meta = data?.meta;
if (!meta || meta.page >= meta.totalPage) { if (!meta || meta.page >= meta.totalPage) {
return; return;
} }
run({ setLoading(true);
const res = await api.resource('roles').list({
page: meta.page + 1, page: meta.page + 1,
pageSize: meta.pageSize,
filter: {
'name.$ne': 'root',
},
showAnonymous: true,
sort: ['createdAt'],
appends: [],
}); });
mutate(res?.data || {});
setLoading(false);
}, [data]); }, [data]);
const { lastItem, setLastItem } = useLoadMoreObserver({ loadMore }); const { lastItem, setLastItem } = useLoadMoreObserver({ loadMore });
@ -54,7 +66,11 @@ export const RolesMenu: React.FC & {
const ref = React.createRef<any>(); const ref = React.createRef<any>();
setLastItem(ref); setLastItem(ref);
setRoles((prev) => prev.concat(data.data)); if (data.meta?.page > 1) {
setRoles((prev) => prev.concat(data.data));
} else {
setRoles(data.data);
}
}, [data, setLastItem]); }, [data, setLastItem]);
const items = useMemo( const items = useMemo(
@ -88,15 +104,12 @@ export const RolesMenu: React.FC & {
return ( return (
<> <>
{roles.length ? ( {roles.length ? (
<> <Menu
<Menu style={{ border: 'none', maxHeight: '65vh', overflowY: 'auto' }}
style={{ border: 'none', maxHeight: '65vh', overflowY: 'auto' }} items={[...items, ...(loading ? [{ key: 'loading', label: <Spin /> }] : [])]}
items={items} selectedKeys={[role?.name]}
selectedKeys={[role?.name]} onSelect={handleSelect}
onSelect={handleSelect} />
/>
{loading && <Spin />}
</>
) : ( ) : (
<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} /> <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
)} )}

View File

@ -93,7 +93,9 @@ export const RoleUsersManager: React.FC = () => {
const handleSelectRoleUsers = (_: number[], rows: any[]) => { const handleSelectRoleUsers = (_: number[], rows: any[]) => {
selectedRoleUsers.current = rows; selectedRoleUsers.current = rows;
}; };
const useAddRoleUsers = () => { const useAddRoleUsers = () => {
const { role } = useContext(RolesManagerContext);
const api = useAPIClient(); const api = useAPIClient();
const { setVisible } = useActionContext(); const { setVisible } = useActionContext();
const { refresh } = useResourceActionContext(); const { refresh } = useResourceActionContext();