import {Atom} from '@reatom/core'; import {useAtom} from '@reatom/react'; import {head} from 'lodash'; import Table, {ColumnsType} from 'antd/lib/table'; import React, {FC, memo, useCallback, useEffect, useMemo} from 'react'; import {CrudService} from '../../services/CrudService'; import {EntityMode} from '../../types/EntityModes'; import {EntityWithId} from '../../api/CrudAPI'; type Options = { entityListAtom: Atom; service: CrudService; }; export const createEntityTable = function ({ entityListAtom, service, }: Options): FC { return memo(() => { const entityList = useAtom(entityListAtom); useEffect(() => { service.loadEntityList(); }, []); const onRow = useCallback((entity: EntityWithId) => ({ onClick: () => { service.navigate(EntityMode.Show, entity.id); }, }), []); const columns: ColumnsType> = useMemo(() => { const entity = head(entityList); if (entity) { return Object.keys(entity).map(field => { return { title: field, dataIndex: field, key: field, }; }); } return []; }, [entityList]); const dataSource = useMemo(() => { return entityList.map((entity: any) => { return { ...entity, key: entity.id, }; }); }, [entityList]); return ( ); }); };