2024-03-20 15:03:53 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import clsx from 'clsx';
|
2024-04-04 20:03:41 +03:00
|
|
|
|
import { useLayoutEffect, useMemo, useState } from 'react';
|
2024-03-20 15:03:53 +03:00
|
|
|
|
|
2024-03-20 15:27:32 +03:00
|
|
|
|
import DataTable, { createColumnHelper, RowSelectionState } from '@/components/ui/DataTable';
|
2024-04-01 19:07:20 +03:00
|
|
|
|
import { useConceptOptions } from '@/context/OptionsContext';
|
2024-03-20 15:03:53 +03:00
|
|
|
|
import { ConstituentaID, IConstituenta, IRSForm } from '@/models/rsform';
|
2024-04-06 23:09:25 +03:00
|
|
|
|
import { isBasicConcept } from '@/models/rsformAPI';
|
2024-03-20 15:03:53 +03:00
|
|
|
|
import { describeConstituenta } from '@/utils/labels';
|
|
|
|
|
|
2024-05-16 22:39:28 +03:00
|
|
|
|
import BadgeConstituenta from '../info/BadgeConstituenta';
|
2024-03-20 15:27:32 +03:00
|
|
|
|
import FlexColumn from '../ui/FlexColumn';
|
2024-05-02 17:04:18 +03:00
|
|
|
|
import GraphSelectionToolbar from './GraphSelectionToolbar';
|
2024-03-20 15:03:53 +03:00
|
|
|
|
|
2024-05-16 22:39:28 +03:00
|
|
|
|
interface PickMultiConstituentaProps {
|
2024-03-20 15:03:53 +03:00
|
|
|
|
id?: string;
|
|
|
|
|
schema?: IRSForm;
|
|
|
|
|
prefixID: string;
|
|
|
|
|
rows?: number;
|
|
|
|
|
|
|
|
|
|
selected: ConstituentaID[];
|
2024-04-04 20:03:41 +03:00
|
|
|
|
setSelected: React.Dispatch<React.SetStateAction<ConstituentaID[]>>;
|
2024-03-20 15:03:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const columnHelper = createColumnHelper<IConstituenta>();
|
|
|
|
|
|
2024-05-16 22:39:28 +03:00
|
|
|
|
function PickMultiConstituenta({ id, schema, prefixID, rows, selected, setSelected }: PickMultiConstituentaProps) {
|
2024-04-01 19:07:20 +03:00
|
|
|
|
const { colors } = useConceptOptions();
|
2024-03-20 15:03:53 +03:00
|
|
|
|
const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
|
|
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
if (!schema || selected.length === 0) {
|
|
|
|
|
setRowSelection({});
|
|
|
|
|
} else {
|
|
|
|
|
const newRowSelection: RowSelectionState = {};
|
|
|
|
|
schema.items.forEach((cst, index) => {
|
|
|
|
|
newRowSelection[String(index)] = selected.includes(cst.id);
|
|
|
|
|
});
|
|
|
|
|
setRowSelection(newRowSelection);
|
|
|
|
|
}
|
|
|
|
|
}, [selected, schema]);
|
|
|
|
|
|
|
|
|
|
function handleRowSelection(updater: React.SetStateAction<RowSelectionState>) {
|
|
|
|
|
if (!schema) {
|
|
|
|
|
setSelected([]);
|
|
|
|
|
} else {
|
|
|
|
|
const newRowSelection = typeof updater === 'function' ? updater(rowSelection) : updater;
|
|
|
|
|
const newSelection: ConstituentaID[] = [];
|
|
|
|
|
schema.items.forEach((cst, index) => {
|
|
|
|
|
if (newRowSelection[String(index)] === true) {
|
|
|
|
|
newSelection.push(cst.id);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
setSelected(newSelection);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const columns = useMemo(
|
|
|
|
|
() => [
|
|
|
|
|
columnHelper.accessor('alias', {
|
|
|
|
|
id: 'alias',
|
2024-06-04 11:19:21 +03:00
|
|
|
|
header: () => <span className='pl-3'>Имя</span>,
|
2024-03-20 15:03:53 +03:00
|
|
|
|
size: 65,
|
2024-05-16 22:39:28 +03:00
|
|
|
|
cell: props => <BadgeConstituenta theme={colors} value={props.row.original} prefixID={prefixID} />
|
2024-03-20 15:03:53 +03:00
|
|
|
|
}),
|
|
|
|
|
columnHelper.accessor(cst => describeConstituenta(cst), {
|
|
|
|
|
id: 'description',
|
|
|
|
|
size: 1000,
|
|
|
|
|
header: 'Описание'
|
|
|
|
|
})
|
|
|
|
|
],
|
|
|
|
|
[colors, prefixID]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
2024-03-21 17:48:42 +03:00
|
|
|
|
<div className='flex items-end gap-3 mb-3'>
|
2024-03-20 15:03:53 +03:00
|
|
|
|
<span className='w-[24ch] select-none whitespace-nowrap'>
|
|
|
|
|
Выбраны {selected.length} из {schema?.items.length ?? 0}
|
|
|
|
|
</span>
|
2024-04-04 20:03:41 +03:00
|
|
|
|
{schema ? (
|
2024-05-02 17:04:18 +03:00
|
|
|
|
<GraphSelectionToolbar
|
2024-04-06 23:09:25 +03:00
|
|
|
|
graph={schema.graph}
|
|
|
|
|
core={schema.items.filter(cst => isBasicConcept(cst.cst_type)).map(cst => cst.id)}
|
2024-04-04 20:03:41 +03:00
|
|
|
|
setSelected={setSelected}
|
2024-05-23 13:36:16 +03:00
|
|
|
|
emptySelection={selected.length === 0}
|
2024-04-04 20:03:41 +03:00
|
|
|
|
className='w-full ml-8'
|
2024-03-20 15:03:53 +03:00
|
|
|
|
/>
|
2024-04-04 20:03:41 +03:00
|
|
|
|
) : null}
|
2024-03-20 15:03:53 +03:00
|
|
|
|
</div>
|
|
|
|
|
<DataTable
|
|
|
|
|
id={id}
|
|
|
|
|
dense
|
|
|
|
|
noFooter
|
|
|
|
|
rows={rows}
|
|
|
|
|
contentHeight='1.3rem'
|
2024-05-02 21:19:23 +03:00
|
|
|
|
className={clsx('cc-scroll-y', 'border', 'text-sm', 'select-none')}
|
2024-03-20 15:03:53 +03:00
|
|
|
|
data={schema?.items ?? []}
|
|
|
|
|
columns={columns}
|
|
|
|
|
headPosition='0rem'
|
|
|
|
|
enableRowSelection
|
|
|
|
|
rowSelection={rowSelection}
|
|
|
|
|
onRowSelectionChange={handleRowSelection}
|
|
|
|
|
noDataComponent={
|
|
|
|
|
<FlexColumn className='items-center p-3'>
|
|
|
|
|
<p>Список пуст</p>
|
|
|
|
|
</FlexColumn>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 22:39:28 +03:00
|
|
|
|
export default PickMultiConstituenta;
|