2024-06-07 20:17:03 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import clsx from 'clsx';
|
|
|
|
|
import { useLayoutEffect, useMemo, useState } from 'react';
|
|
|
|
|
|
|
|
|
|
import DataTable, { createColumnHelper, RowSelectionState } from '@/components/ui/DataTable';
|
2024-06-26 19:47:05 +03:00
|
|
|
|
import { useConceptOptions } from '@/context/ConceptOptionsContext';
|
2024-08-25 13:45:32 +03:00
|
|
|
|
import { CstMatchMode } from '@/models/miscellaneous';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
import { ConstituentaID, IConstituenta, IRSForm } from '@/models/rsform';
|
2024-08-25 13:45:32 +03:00
|
|
|
|
import { isBasicConcept, matchConstituenta } from '@/models/rsformAPI';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
import { describeConstituenta } from '@/utils/labels';
|
|
|
|
|
|
|
|
|
|
import BadgeConstituenta from '../info/BadgeConstituenta';
|
2024-06-21 19:16:41 +03:00
|
|
|
|
import NoData from '../ui/NoData';
|
2024-08-25 13:45:32 +03:00
|
|
|
|
import SearchBar from '../ui/SearchBar';
|
2024-06-26 19:47:05 +03:00
|
|
|
|
import ToolbarGraphSelection from './ToolbarGraphSelection';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
interface PickMultiConstituentaProps {
|
|
|
|
|
id?: string;
|
|
|
|
|
schema?: IRSForm;
|
|
|
|
|
prefixID: string;
|
|
|
|
|
rows?: number;
|
|
|
|
|
|
|
|
|
|
selected: ConstituentaID[];
|
|
|
|
|
setSelected: React.Dispatch<React.SetStateAction<ConstituentaID[]>>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const columnHelper = createColumnHelper<IConstituenta>();
|
|
|
|
|
|
|
|
|
|
function PickMultiConstituenta({ id, schema, prefixID, rows, selected, setSelected }: PickMultiConstituentaProps) {
|
|
|
|
|
const { colors } = useConceptOptions();
|
|
|
|
|
const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
|
2024-08-25 13:45:32 +03:00
|
|
|
|
const [filtered, setFiltered] = useState<IConstituenta[]>(schema?.items ?? []);
|
|
|
|
|
const [filterText, setFilterText] = useState('');
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
2024-08-25 13:45:32 +03:00
|
|
|
|
if (filtered.length === 0) {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
setRowSelection({});
|
2024-08-25 13:45:32 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const newRowSelection: RowSelectionState = {};
|
|
|
|
|
filtered.forEach((cst, index) => {
|
|
|
|
|
newRowSelection[String(index)] = selected.includes(cst.id);
|
|
|
|
|
});
|
|
|
|
|
setRowSelection(newRowSelection);
|
|
|
|
|
}, [filtered, setRowSelection, selected]);
|
|
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
if (!schema || schema.items.length === 0) {
|
|
|
|
|
setFiltered([]);
|
|
|
|
|
} else if (filterText) {
|
|
|
|
|
setFiltered(schema.items.filter(cst => matchConstituenta(cst, filterText, CstMatchMode.ALL)));
|
2024-06-07 20:17:03 +03:00
|
|
|
|
} else {
|
2024-08-25 13:45:32 +03:00
|
|
|
|
setFiltered(schema.items);
|
2024-06-07 20:17:03 +03:00
|
|
|
|
}
|
2024-08-25 13:45:32 +03:00
|
|
|
|
}, [filterText, schema?.items, schema]);
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
function handleRowSelection(updater: React.SetStateAction<RowSelectionState>) {
|
|
|
|
|
if (!schema) {
|
|
|
|
|
setSelected([]);
|
|
|
|
|
} else {
|
|
|
|
|
const newRowSelection = typeof updater === 'function' ? updater(rowSelection) : updater;
|
|
|
|
|
const newSelection: ConstituentaID[] = [];
|
2024-08-25 13:45:32 +03:00
|
|
|
|
filtered.forEach((cst, index) => {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
if (newRowSelection[String(index)] === true) {
|
|
|
|
|
newSelection.push(cst.id);
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-08-25 13:45:32 +03:00
|
|
|
|
setSelected(prev => [...prev.filter(cst_id => !filtered.find(cst => cst.id === cst_id)), ...newSelection]);
|
2024-06-07 20:17:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const columns = useMemo(
|
|
|
|
|
() => [
|
|
|
|
|
columnHelper.accessor('alias', {
|
|
|
|
|
id: 'alias',
|
|
|
|
|
header: () => <span className='pl-3'>Имя</span>,
|
|
|
|
|
size: 65,
|
|
|
|
|
cell: props => <BadgeConstituenta theme={colors} value={props.row.original} prefixID={prefixID} />
|
|
|
|
|
}),
|
|
|
|
|
columnHelper.accessor(cst => describeConstituenta(cst), {
|
|
|
|
|
id: 'description',
|
|
|
|
|
size: 1000,
|
|
|
|
|
header: 'Описание'
|
|
|
|
|
})
|
|
|
|
|
],
|
|
|
|
|
[colors, prefixID]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
2024-09-02 12:14:19 +03:00
|
|
|
|
<div className='flex justify-between items-center clr-input px-3 border-x border-t rounded-t-md'>
|
2024-08-25 13:45:32 +03:00
|
|
|
|
<div className='w-[24ch] select-none whitespace-nowrap'>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
Выбраны {selected.length} из {schema?.items.length ?? 0}
|
2024-08-25 13:45:32 +03:00
|
|
|
|
</div>
|
|
|
|
|
<SearchBar
|
|
|
|
|
id='dlg_constituents_search'
|
|
|
|
|
noBorder
|
|
|
|
|
className='min-w-[6rem] pr-2 flex-grow'
|
|
|
|
|
value={filterText}
|
|
|
|
|
onChange={setFilterText}
|
|
|
|
|
/>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
{schema ? (
|
2024-06-26 19:47:05 +03:00
|
|
|
|
<ToolbarGraphSelection
|
2024-06-07 20:17:03 +03:00
|
|
|
|
graph={schema.graph}
|
2024-08-21 20:20:48 +03:00
|
|
|
|
isCore={cstID => isBasicConcept(schema.cstByID.get(cstID)?.cst_type)}
|
2024-08-21 20:32:04 +03:00
|
|
|
|
isOwned={cstID => !schema.cstByID.get(cstID)?.is_inherited}
|
2024-06-07 20:17:03 +03:00
|
|
|
|
setSelected={setSelected}
|
|
|
|
|
emptySelection={selected.length === 0}
|
2024-08-25 13:45:32 +03:00
|
|
|
|
className='w-fit'
|
2024-06-07 20:17:03 +03:00
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
<DataTable
|
|
|
|
|
id={id}
|
|
|
|
|
dense
|
|
|
|
|
noFooter
|
|
|
|
|
rows={rows}
|
|
|
|
|
contentHeight='1.3rem'
|
|
|
|
|
className={clsx('cc-scroll-y', 'border', 'text-sm', 'select-none')}
|
2024-08-25 13:45:32 +03:00
|
|
|
|
data={filtered}
|
2024-06-07 20:17:03 +03:00
|
|
|
|
columns={columns}
|
|
|
|
|
headPosition='0rem'
|
|
|
|
|
enableRowSelection
|
|
|
|
|
rowSelection={rowSelection}
|
|
|
|
|
onRowSelectionChange={handleRowSelection}
|
|
|
|
|
noDataComponent={
|
2024-06-21 19:16:41 +03:00
|
|
|
|
<NoData>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
<p>Список пуст</p>
|
2024-06-21 19:16:41 +03:00
|
|
|
|
</NoData>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default PickMultiConstituenta;
|