2023-11-04 01:29:21 +03:00
|
|
|
import { Dispatch, useEffect, useMemo, useState } from 'react';
|
|
|
|
|
2023-11-27 12:11:39 +03:00
|
|
|
import SelectSingle from '../../components/Common/SelectSingle';
|
|
|
|
import TextArea from '../../components/Common/TextArea';
|
2023-11-04 01:29:21 +03:00
|
|
|
import RSInput from '../../components/RSInput';
|
2023-11-27 12:11:39 +03:00
|
|
|
import ConstituentaPicker from '../../components/Shared/ConstituentaPicker';
|
2023-11-04 01:29:21 +03:00
|
|
|
import { useLibrary } from '../../context/LibraryContext';
|
2023-11-06 20:21:30 +03:00
|
|
|
import { CATEGORY_CST_TYPE, IConstituenta, IRSForm } from '../../models/rsform';
|
|
|
|
import { applyFilterCategory } from '../../models/rsformAPI';
|
2023-11-04 01:29:21 +03:00
|
|
|
import { prefixes } from '../../utils/constants';
|
|
|
|
export interface ITemplateState {
|
|
|
|
templateID?: number
|
|
|
|
prototype?: IConstituenta
|
|
|
|
filterCategory?: IConstituenta
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TemplateTabProps {
|
|
|
|
state: ITemplateState
|
|
|
|
partialUpdate: Dispatch<Partial<ITemplateState>>
|
|
|
|
}
|
|
|
|
|
2023-11-06 18:03:23 +03:00
|
|
|
function TemplateTab({ state, partialUpdate }: TemplateTabProps) {
|
2023-11-04 01:29:21 +03:00
|
|
|
const { templates, retrieveTemplate } = useLibrary();
|
|
|
|
const [ selectedSchema, setSelectedSchema ] = useState<IRSForm | undefined>(undefined);
|
|
|
|
|
|
|
|
const [filteredData, setFilteredData] = useState<IConstituenta[]>([]);
|
|
|
|
|
|
|
|
const prototypeInfo = useMemo(
|
|
|
|
() => {
|
|
|
|
if (!state.prototype) {
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return `${state.prototype?.term_raw}${state.prototype?.definition_raw ? ` — ${state.prototype?.definition_raw}` : ''}`;
|
|
|
|
}
|
|
|
|
}, [state.prototype]);
|
|
|
|
|
|
|
|
const templateSelector = useMemo(
|
|
|
|
() => templates.map(
|
|
|
|
(template) => ({
|
|
|
|
value: template.id,
|
|
|
|
label: template.title
|
|
|
|
})
|
|
|
|
), [templates]);
|
|
|
|
|
|
|
|
const categorySelector = useMemo(
|
|
|
|
(): {value: number, label: string}[] => {
|
|
|
|
if (!selectedSchema) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return selectedSchema.items
|
|
|
|
.filter(cst => cst.cst_type === CATEGORY_CST_TYPE)
|
|
|
|
.map(cst => ({
|
|
|
|
value: cst.id,
|
|
|
|
label: cst.term_raw
|
|
|
|
}));
|
|
|
|
}, [selectedSchema]);
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
() => {
|
|
|
|
if (templates.length > 0 && !state.templateID) {
|
|
|
|
partialUpdate({ templateID: templates[0].id });
|
|
|
|
}
|
|
|
|
}, [templates, state.templateID, partialUpdate]);
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
() => {
|
|
|
|
if (!state.templateID) {
|
|
|
|
setSelectedSchema(undefined);
|
|
|
|
} else {
|
|
|
|
retrieveTemplate(state.templateID, setSelectedSchema);
|
|
|
|
}
|
|
|
|
}, [state.templateID, retrieveTemplate]);
|
|
|
|
|
|
|
|
// Filter constituents
|
|
|
|
useEffect(
|
|
|
|
() => {
|
|
|
|
if (!selectedSchema) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let data = selectedSchema.items;
|
|
|
|
if (state.filterCategory) {
|
|
|
|
data = applyFilterCategory(state.filterCategory, selectedSchema);
|
|
|
|
}
|
|
|
|
setFilteredData(data);
|
2023-11-06 18:03:23 +03:00
|
|
|
}, [state.filterCategory, selectedSchema]);
|
2023-11-04 01:29:21 +03:00
|
|
|
|
|
|
|
return (
|
2023-11-06 02:20:16 +03:00
|
|
|
<div className='flex flex-col gap-3'>
|
2023-11-10 15:34:59 +03:00
|
|
|
<div>
|
2023-11-30 02:14:24 +03:00
|
|
|
<div className='flex justify-between'>
|
2023-11-10 15:34:59 +03:00
|
|
|
<SelectSingle
|
|
|
|
placeholder='Выберите категорию'
|
2023-11-30 02:14:24 +03:00
|
|
|
className='w-full border-none'
|
|
|
|
options={categorySelector}
|
2023-11-10 15:34:59 +03:00
|
|
|
value={state.filterCategory && selectedSchema ? {
|
|
|
|
value: state.filterCategory.id,
|
|
|
|
label: state.filterCategory.term_raw
|
|
|
|
} : null}
|
|
|
|
onChange={data => partialUpdate({filterCategory: selectedSchema?.items.find(cst => cst.id === data?.value) })}
|
|
|
|
isClearable
|
|
|
|
/>
|
|
|
|
<SelectSingle
|
2023-11-30 02:14:24 +03:00
|
|
|
placeholder='Выберите источник'
|
2023-11-10 15:34:59 +03:00
|
|
|
className='min-w-[15rem]'
|
|
|
|
options={templateSelector}
|
|
|
|
value={state.templateID ? { value: state.templateID, label: templates.find(item => item.id == state.templateID)!.title }: null}
|
|
|
|
onChange={data => partialUpdate({templateID: (data ? data.value : undefined)})}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<ConstituentaPicker
|
|
|
|
value={state.prototype}
|
|
|
|
data={filteredData}
|
|
|
|
onSelectValue={cst => partialUpdate( { prototype: cst } )}
|
|
|
|
prefixID={prefixes.cst_template_ist}
|
|
|
|
rows={9}
|
2023-11-04 01:29:21 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<TextArea id='term'
|
2023-11-10 15:34:59 +03:00
|
|
|
rows={2}
|
2023-11-04 01:29:21 +03:00
|
|
|
disabled
|
|
|
|
placeholder='Шаблон конституенты не выбран'
|
|
|
|
value={prototypeInfo}
|
|
|
|
spellCheck
|
|
|
|
/>
|
|
|
|
<RSInput id='expression'
|
2023-11-07 18:14:48 +03:00
|
|
|
height='5.1rem'
|
2023-11-04 01:29:21 +03:00
|
|
|
placeholder='Выберите шаблон из списка'
|
2023-11-06 02:20:16 +03:00
|
|
|
disabled
|
2023-11-04 01:29:21 +03:00
|
|
|
value={state.prototype?.definition_formal}
|
|
|
|
/>
|
|
|
|
</div>);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TemplateTab;
|