import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { EditingFormItemDto, GridDto, PlatformEditorTypes } from '@/proxy/form/models' import { captionize } from 'devextreme/core/utils/inflector' import { useListFormCustomDataSource } from '@/shared/useListFormCustomDataSource' import { Button, Pagination, Select } from '@/components/ui' import classNames from 'classnames' import { getList } from '@/services/form.service' import { FaInbox, FaSearch } from 'react-icons/fa' import FormDevExpress from '../form/FormDevExpress' import { GroupItem } from 'devextreme/ui/form' import { Form as FormDx } from 'devextreme-react/form' import FormButtons from '../form/FormButtons' import { useNavigate } from 'react-router-dom' import { ROUTES_ENUM } from '@/routes/route.constant' import CustomStore from 'devextreme/data/custom_store' import { PermissionResults, SimpleItemWithColData } from '../form/types' import { usePermission } from '@/utils/hooks/usePermission' import { useLocalization } from '@/utils/hooks/useLocalization' import { useLookupDataSource } from '../form/useLookupDataSource' import { Container, Loading } from '@/components/shared' import WidgetGroup from '@/components/common/WidgetGroup' import { GridExtraFilterState } from './Utils' interface CardProps { listFormCode: string searchParams?: URLSearchParams } type Option = { value: number label: string } const CardItem = ({ isSubForm, row, gridDto, listFormCode, dataSource, refreshData, getCachedLookupDataSource, }: { isSubForm?: boolean row: any gridDto: GridDto listFormCode: string dataSource: CustomStore refreshData: () => void getCachedLookupDataSource: (editorOptions: any, colData: any) => any }) => { const [formData, setFormData] = useState(row) const refForm = useRef(null) const navigate = useNavigate() const { checkPermission } = usePermission() const { translate } = useLocalization() const keyField = gridDto.gridOptions.keyFieldName const rowId = row[keyField!] // Form Items const formItems: GroupItem[] = useMemo(() => { if (!gridDto) return [] return gridDto.gridOptions.editingFormDto ?.sort((a, b) => (a.order >= b.order ? 1 : -1)) .map((e) => { return { itemType: e.itemType, colCount: e.colCount, colSpan: e.colSpan, caption: e.caption, items: e.items ?.sort((a, b) => (a.order >= b.order ? 1 : -1)) .map((i: EditingFormItemDto) => { let editorOptions = {} const colData = gridDto.columnFormats.find((x) => x.fieldName === i.dataField) try { editorOptions = i.editorOptions && JSON.parse(i.editorOptions) } catch {} const item: SimpleItemWithColData = { canRead: colData?.canRead ?? false, canUpdate: colData?.canUpdate ?? false, canCreate: colData?.canCreate ?? false, canExport: colData?.canExport ?? false, dataField: i.dataField, name: i.dataField, editorType2: i.editorType2, editorType: i.editorType2 == PlatformEditorTypes.dxGridBox ? 'dxDropDownBox' : i.editorType2, colSpan: i.colSpan, isRequired: i.isRequired, editorOptions: { ...editorOptions, ...(colData?.lookupDto?.dataSourceType ? { dataSource: getCachedLookupDataSource(colData?.editorOptions, colData), valueExpr: colData?.lookupDto?.valueExpr?.toLowerCase(), displayExpr: colData?.lookupDto?.displayExpr?.toLowerCase(), } : {}), }, colData, tagBoxOptions: i.tagBoxOptions, gridBoxOptions: i.gridBoxOptions, } if (i.dataField.indexOf(':') >= 0) { item.label = { text: captionize(i.dataField.split(':')[1]) } } item.editorOptions = { ...item.editorOptions, readOnly: true, } return item }), } as GroupItem }) }, [gridDto, getCachedLookupDataSource]) const permissionResults: PermissionResults = { c: gridDto?.gridOptions.editingOptionDto.allowAdding === true && checkPermission(gridDto?.gridOptions.permissionDto.c), r: checkPermission(gridDto?.gridOptions.permissionDto.r), u: gridDto?.gridOptions.editingOptionDto.allowUpdating === true && checkPermission(gridDto?.gridOptions.permissionDto.u), d: gridDto?.gridOptions.editingOptionDto.allowDeleting === true && checkPermission(gridDto?.gridOptions.permissionDto.d), e: checkPermission(gridDto?.gridOptions.permissionDto.e), i: checkPermission(gridDto?.gridOptions.permissionDto.i), } const onActionEdit = () => { navigate( ROUTES_ENUM.protected.admin.formEdit .replace(':listFormCode', listFormCode) .replace(':id', rowId!), ) } const onActionNew = () => { navigate(ROUTES_ENUM.protected.admin.formNew.replace(':listFormCode', listFormCode)) } return (
{!isSubForm &&

{translate('::' + gridDto?.gridOptions.title)}

} {permissionResults && ( ({})} refreshData={refreshData} getSelectedRowKeys={() => [rowId]} getSelectedRowsData={() => [row]} getFilter={() => null} onActionEdit={onActionEdit} onActionNew={onActionNew} /> )}
) } const Card = ({ listFormCode, searchParams }: CardProps) => { const { createSelectDataSource } = useListFormCustomDataSource({}) const [gridDto, setGridDto] = useState() const [data, setData] = useState([]) const [totalCount, setTotalCount] = useState(0) const [currentPage, setCurrentPage] = useState(1) const [pageSize, setPageSize] = useState(20) const [pageSizeOptions, setPageSizeOptions] = useState([]) const [gridDataSource, setGridDataSource] = useState>() const [extraFilters, setExtraFilters] = useState([]) const [layoutCount, setLayoutCount] = useState(4) const { getLookupDataSource } = useLookupDataSource({ listFormCode }) const lookupCache = useRef>(new Map()) const getCachedLookupDataSource = useCallback( (editorOptions: any, colData: any) => { const key = colData?.fieldName if (!key) return null if (!lookupCache.current.has(key)) { lookupCache.current.set(key, getLookupDataSource(editorOptions, colData)) } return lookupCache.current.get(key) }, [getLookupDataSource], ) const onPageSizeSelect = ({ value }: Option) => { setPageSize(value) setCurrentPage(1) } const onPageChange = (page: number) => { setCurrentPage(page) } useEffect(() => { getList({ listFormCode }).then((res: any) => setGridDto(res.data)) }, [listFormCode]) useEffect(() => { if (gridDto) { const dataSource = createSelectDataSource(gridDto.gridOptions, listFormCode, searchParams) setGridDataSource(dataSource) } }, [gridDto, listFormCode, searchParams, createSelectDataSource]) const loadData = useCallback(() => { if (!gridDataSource) return const loadOptions = { skip: (currentPage - 1) * pageSize, take: pageSize, requireTotalCount: true, } gridDataSource.load(loadOptions).then((res: any) => { setData(res.data) setTotalCount(res.totalCount || 0) }) }, [gridDataSource, currentPage, pageSize]) useEffect(() => { if (gridDataSource) { loadData() } }, [gridDataSource, loadData]) useEffect(() => { if (!gridDto) return const pagerOptions = gridDto.gridOptions.pagerOptionDto const allowedSizes = pagerOptions?.allowedPageSizes ?.split(',') .map((s) => Number(s.trim())) .filter((n) => !isNaN(n) && n > 0) || [10, 20, 50, 100] setPageSizeOptions(allowedSizes.map((size) => ({ value: size, label: `${size} page` }))) }, [gridDto, listFormCode, searchParams]) // Data güncellendiğinde sayfanın en üstüne kaydır useEffect(() => { if (data.length > 0) { window.scrollTo({ top: 0, behavior: 'smooth' }) } }, [data]) if (!gridDto) return null return ( <>
{gridDataSource && data.map((row, idx) => { const keyField = gridDto.gridOptions.keyFieldName const rowId = row[keyField!] return ( ) })}
{gridDto.gridOptions.pagerOptionDto?.visible && totalCount > pageSize && (
Toplam {totalCount} kayıt