2024-06-07 20:17:03 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
|
|
|
|
|
|
import DataTable, { createColumnHelper, IConditionalStyle } from '@/components/ui/DataTable';
|
|
|
|
|
import SearchBar from '@/components/ui/SearchBar';
|
2024-06-26 19:47:05 +03:00
|
|
|
|
import { useConceptOptions } from '@/context/ConceptOptionsContext';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
import { CstMatchMode } from '@/models/miscellaneous';
|
|
|
|
|
import { IConstituenta } from '@/models/rsform';
|
|
|
|
|
import { matchConstituenta } from '@/models/rsformAPI';
|
|
|
|
|
import { prefixes } from '@/utils/constants';
|
|
|
|
|
import { describeConstituenta } from '@/utils/labels';
|
|
|
|
|
|
|
|
|
|
import BadgeConstituenta from '../info/BadgeConstituenta';
|
2024-06-21 19:16:41 +03:00
|
|
|
|
import NoData from '../ui/NoData';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
interface PickConstituentaProps {
|
|
|
|
|
id?: string;
|
|
|
|
|
prefixID: string;
|
|
|
|
|
data?: IConstituenta[];
|
|
|
|
|
rows?: number;
|
|
|
|
|
|
|
|
|
|
initialFilter?: string;
|
|
|
|
|
onBeginFilter?: (cst: IConstituenta) => boolean;
|
|
|
|
|
describeFunc?: (cst: IConstituenta) => string;
|
|
|
|
|
matchFunc?: (cst: IConstituenta, filter: string) => boolean;
|
|
|
|
|
|
|
|
|
|
value?: IConstituenta;
|
|
|
|
|
onSelectValue: (newValue: IConstituenta) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const columnHelper = createColumnHelper<IConstituenta>();
|
|
|
|
|
|
|
|
|
|
function PickConstituenta({
|
|
|
|
|
id,
|
|
|
|
|
data,
|
|
|
|
|
value,
|
|
|
|
|
initialFilter = '',
|
|
|
|
|
rows = 4,
|
|
|
|
|
prefixID = prefixes.cst_list,
|
|
|
|
|
describeFunc = describeConstituenta,
|
|
|
|
|
matchFunc = (cst, filter) => matchConstituenta(cst, filter, CstMatchMode.ALL),
|
|
|
|
|
onBeginFilter,
|
|
|
|
|
onSelectValue
|
|
|
|
|
}: PickConstituentaProps) {
|
|
|
|
|
const { colors } = useConceptOptions();
|
|
|
|
|
const [filteredData, setFilteredData] = useState<IConstituenta[]>([]);
|
|
|
|
|
const [filterText, setFilterText] = useState(initialFilter);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!data) {
|
|
|
|
|
setFilteredData([]);
|
|
|
|
|
} else {
|
|
|
|
|
const newData = onBeginFilter ? data.filter(onBeginFilter) : data;
|
|
|
|
|
if (filterText) {
|
|
|
|
|
setFilteredData(newData.filter(cst => matchFunc(cst, filterText)));
|
|
|
|
|
} else {
|
|
|
|
|
setFilteredData(newData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [data, filterText, matchFunc, onBeginFilter]);
|
|
|
|
|
|
|
|
|
|
const columns = useMemo(
|
|
|
|
|
() => [
|
|
|
|
|
columnHelper.accessor('alias', {
|
|
|
|
|
id: 'alias',
|
|
|
|
|
size: 65,
|
|
|
|
|
minSize: 65,
|
|
|
|
|
maxSize: 65,
|
|
|
|
|
cell: props => <BadgeConstituenta theme={colors} value={props.row.original} prefixID={prefixID} />
|
|
|
|
|
}),
|
|
|
|
|
columnHelper.accessor(cst => describeFunc(cst), {
|
|
|
|
|
id: 'description'
|
|
|
|
|
})
|
|
|
|
|
],
|
|
|
|
|
[colors, prefixID, describeFunc]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const conditionalRowStyles = useMemo(
|
|
|
|
|
(): IConditionalStyle<IConstituenta>[] => [
|
|
|
|
|
{
|
|
|
|
|
when: (cst: IConstituenta) => cst.id === value?.id,
|
|
|
|
|
style: { backgroundColor: colors.bgSelected }
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[value, colors]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className='border divide-y'>
|
|
|
|
|
<SearchBar
|
|
|
|
|
id={id ? `${id}__search` : undefined}
|
2024-08-30 20:18:04 +03:00
|
|
|
|
className='clr-input'
|
2024-06-07 20:17:03 +03:00
|
|
|
|
noBorder
|
|
|
|
|
value={filterText}
|
|
|
|
|
onChange={newValue => setFilterText(newValue)}
|
|
|
|
|
/>
|
|
|
|
|
<DataTable
|
|
|
|
|
id={id}
|
|
|
|
|
rows={rows}
|
|
|
|
|
contentHeight='1.3rem'
|
|
|
|
|
dense
|
|
|
|
|
noHeader
|
|
|
|
|
noFooter
|
|
|
|
|
className='text-sm select-none cc-scroll-y'
|
|
|
|
|
data={filteredData}
|
|
|
|
|
columns={columns}
|
|
|
|
|
conditionalRowStyles={conditionalRowStyles}
|
|
|
|
|
noDataComponent={
|
2024-06-21 19:16:41 +03:00
|
|
|
|
<NoData className='min-h-[6rem]'>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
<p>Список конституент пуст</p>
|
|
|
|
|
<p>Измените параметры фильтра</p>
|
2024-06-21 19:16:41 +03:00
|
|
|
|
</NoData>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
}
|
|
|
|
|
onRowClicked={onSelectValue}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default PickConstituenta;
|