2023-12-13 14:32:57 +03:00
|
|
|
'use client';
|
|
|
|
|
2025-01-15 22:16:27 +03:00
|
|
|
import { useEffect } from 'react';
|
2023-12-08 19:24:08 +03:00
|
|
|
|
2024-08-03 11:31:13 +03:00
|
|
|
import { IconChild } from '@/components/Icons';
|
2024-05-02 17:04:18 +03:00
|
|
|
import SelectGraphFilter from '@/components/select/SelectGraphFilter';
|
|
|
|
import SelectMatchMode from '@/components/select/SelectMatchMode';
|
2024-08-03 11:31:13 +03:00
|
|
|
import MiniButton from '@/components/ui/MiniButton';
|
2024-01-04 19:38:12 +03:00
|
|
|
import SearchBar from '@/components/ui/SearchBar';
|
2023-12-26 14:23:51 +03:00
|
|
|
import { applyGraphFilter } from '@/models/miscellaneousAPI';
|
2024-03-17 19:24:12 +03:00
|
|
|
import { ConstituentaID, IConstituenta, IRSForm } from '@/models/rsform';
|
2024-09-23 11:54:18 +03:00
|
|
|
import { matchConstituenta } from '@/models/rsformAPI';
|
2025-01-15 22:16:27 +03:00
|
|
|
import { useCstSearchStore } from '@/stores/cstSearch';
|
2023-12-08 19:24:08 +03:00
|
|
|
|
|
|
|
interface ConstituentsSearchProps {
|
2025-01-28 19:47:24 +03:00
|
|
|
schema: IRSForm;
|
2024-06-04 14:20:43 +03:00
|
|
|
dense?: boolean;
|
2024-03-17 19:24:12 +03:00
|
|
|
activeID?: ConstituentaID;
|
2023-12-28 14:04:44 +03:00
|
|
|
activeExpression: string;
|
|
|
|
setFiltered: React.Dispatch<React.SetStateAction<IConstituenta[]>>;
|
2023-12-08 19:24:08 +03:00
|
|
|
}
|
|
|
|
|
2024-06-04 14:20:43 +03:00
|
|
|
function ConstituentsSearch({ schema, activeID, activeExpression, dense, setFiltered }: ConstituentsSearchProps) {
|
2025-01-15 22:16:27 +03:00
|
|
|
const query = useCstSearchStore(state => state.query);
|
|
|
|
const filterMatch = useCstSearchStore(state => state.match);
|
|
|
|
const filterSource = useCstSearchStore(state => state.source);
|
|
|
|
const includeInherited = useCstSearchStore(state => state.includeInherited);
|
|
|
|
const setQuery = useCstSearchStore(state => state.setQuery);
|
|
|
|
const setMatch = useCstSearchStore(state => state.setMatch);
|
|
|
|
const setSource = useCstSearchStore(state => state.setSource);
|
|
|
|
const toggleInherited = useCstSearchStore(state => state.toggleInherited);
|
2023-12-08 19:24:08 +03:00
|
|
|
|
2024-12-12 21:36:46 +03:00
|
|
|
useEffect(() => {
|
2025-01-28 19:47:24 +03:00
|
|
|
if (schema.items.length === 0) {
|
2023-12-08 19:24:08 +03:00
|
|
|
setFiltered([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let result: IConstituenta[] = [];
|
2024-09-23 11:54:18 +03:00
|
|
|
if (!activeID) {
|
2023-12-08 19:24:08 +03:00
|
|
|
result = schema.items;
|
|
|
|
} else {
|
|
|
|
result = applyGraphFilter(schema, activeID, filterSource);
|
|
|
|
}
|
2025-01-15 22:16:27 +03:00
|
|
|
if (query) {
|
|
|
|
result = result.filter(cst => matchConstituenta(cst, query, filterMatch));
|
2023-12-08 19:24:08 +03:00
|
|
|
}
|
2025-01-15 22:16:27 +03:00
|
|
|
if (!includeInherited) {
|
2024-08-03 11:31:13 +03:00
|
|
|
result = result.filter(cst => !cst.is_inherited);
|
|
|
|
}
|
2023-12-08 19:24:08 +03:00
|
|
|
setFiltered(result);
|
2024-08-03 11:31:13 +03:00
|
|
|
}, [
|
2025-01-15 22:16:27 +03:00
|
|
|
query,
|
2024-08-03 11:31:13 +03:00
|
|
|
setFiltered,
|
|
|
|
filterSource,
|
|
|
|
activeExpression,
|
2025-01-28 19:47:24 +03:00
|
|
|
schema.items,
|
2024-08-03 11:31:13 +03:00
|
|
|
schema,
|
|
|
|
filterMatch,
|
|
|
|
activeID,
|
2025-01-15 22:16:27 +03:00
|
|
|
includeInherited
|
2024-08-03 11:31:13 +03:00
|
|
|
]);
|
2023-12-08 19:24:08 +03:00
|
|
|
|
|
|
|
return (
|
2024-10-23 15:31:24 +03:00
|
|
|
<div className='flex border-b clr-input rounded-t-md'>
|
2024-03-18 16:21:39 +03:00
|
|
|
<SearchBar
|
|
|
|
id='constituents_search'
|
|
|
|
noBorder
|
2024-12-18 14:41:59 +03:00
|
|
|
className='min-w-[6rem] w-[6rem] mr-2 flex-grow'
|
2025-01-15 22:16:27 +03:00
|
|
|
query={query}
|
|
|
|
onChangeQuery={setQuery}
|
2024-03-18 16:21:39 +03:00
|
|
|
/>
|
2025-01-15 22:16:27 +03:00
|
|
|
<SelectMatchMode value={filterMatch} onChange={newValue => setMatch(newValue)} dense={dense} />
|
|
|
|
<SelectGraphFilter value={filterSource} onChange={newValue => setSource(newValue)} dense={dense} />
|
2025-01-28 19:47:24 +03:00
|
|
|
{schema.stats.count_inherited > 0 ? (
|
2024-08-03 11:31:13 +03:00
|
|
|
<MiniButton
|
|
|
|
noHover
|
2025-01-15 22:16:27 +03:00
|
|
|
titleHtml={`Наследованные: <b>${includeInherited ? 'отображать' : 'скрывать'}</b>`}
|
|
|
|
icon={<IconChild size='1rem' className={includeInherited ? 'icon-primary' : 'clr-text-controls'} />}
|
2024-08-03 11:31:13 +03:00
|
|
|
className='h-fit self-center'
|
2025-01-15 22:16:27 +03:00
|
|
|
onClick={toggleInherited}
|
2024-08-03 11:31:13 +03:00
|
|
|
/>
|
|
|
|
) : null}
|
2023-12-08 19:24:08 +03:00
|
|
|
</div>
|
2023-12-28 14:04:44 +03:00
|
|
|
);
|
2023-12-08 19:24:08 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
export default ConstituentsSearch;
|