2023-12-13 14:32:57 +03:00
|
|
|
'use client';
|
|
|
|
|
2024-12-13 21:31:09 +03:00
|
|
|
import { Dispatch, useEffect, useState } from 'react';
|
2023-11-04 01:29:21 +03:00
|
|
|
|
2025-01-27 15:03:48 +03:00
|
|
|
import { useTemplatesSuspense } from '@/backend/library/useTemplates';
|
2023-12-13 14:32:57 +03:00
|
|
|
import RSInput from '@/components/RSInput';
|
2024-05-16 22:39:28 +03:00
|
|
|
import PickConstituenta from '@/components/select/PickConstituenta';
|
2024-01-04 19:38:12 +03:00
|
|
|
import SelectSingle from '@/components/ui/SelectSingle';
|
|
|
|
import TextArea from '@/components/ui/TextArea';
|
2023-12-13 14:32:57 +03:00
|
|
|
import { CATEGORY_CST_TYPE, IConstituenta, IRSForm } from '@/models/rsform';
|
|
|
|
import { applyFilterCategory } from '@/models/rsformAPI';
|
|
|
|
import { prefixes } from '@/utils/constants';
|
2024-08-29 12:42:26 +03:00
|
|
|
|
2023-11-04 01:29:21 +03:00
|
|
|
export interface ITemplateState {
|
2023-12-28 14:04:44 +03:00
|
|
|
templateID?: number;
|
|
|
|
prototype?: IConstituenta;
|
|
|
|
filterCategory?: IConstituenta;
|
2023-11-04 01:29:21 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 19:47:31 +03:00
|
|
|
interface TabTemplateProps {
|
2023-12-28 14:04:44 +03:00
|
|
|
state: ITemplateState;
|
|
|
|
partialUpdate: Dispatch<Partial<ITemplateState>>;
|
2024-08-29 12:42:26 +03:00
|
|
|
templateSchema?: IRSForm;
|
2023-11-04 01:29:21 +03:00
|
|
|
}
|
|
|
|
|
2024-08-29 12:42:26 +03:00
|
|
|
function TabTemplate({ state, partialUpdate, templateSchema }: TabTemplateProps) {
|
2025-01-27 15:03:48 +03:00
|
|
|
const { templates } = useTemplatesSuspense();
|
2023-12-28 14:04:44 +03:00
|
|
|
|
2023-11-04 01:29:21 +03:00
|
|
|
const [filteredData, setFilteredData] = useState<IConstituenta[]>([]);
|
|
|
|
|
2024-12-13 21:31:09 +03:00
|
|
|
const prototypeInfo = !state.prototype
|
|
|
|
? ''
|
|
|
|
: `${state.prototype?.term_raw}${state.prototype?.definition_raw ? ` — ${state.prototype?.definition_raw}` : ''}`;
|
2023-11-04 01:29:21 +03:00
|
|
|
|
2024-12-13 21:31:09 +03:00
|
|
|
const templateSelector = templates.map(template => ({
|
|
|
|
value: template.id,
|
|
|
|
label: template.title
|
|
|
|
}));
|
2023-11-04 01:29:21 +03:00
|
|
|
|
2024-12-13 21:31:09 +03:00
|
|
|
const categorySelector: { value: number; label: string }[] = !templateSchema
|
|
|
|
? []
|
|
|
|
: templateSchema.items
|
|
|
|
.filter(cst => cst.cst_type === CATEGORY_CST_TYPE)
|
|
|
|
.map(cst => ({
|
|
|
|
value: cst.id,
|
|
|
|
label: cst.term_raw
|
|
|
|
}));
|
2023-11-04 01:29:21 +03:00
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
useEffect(() => {
|
2023-11-04 01:29:21 +03:00
|
|
|
if (templates.length > 0 && !state.templateID) {
|
|
|
|
partialUpdate({ templateID: templates[0].id });
|
|
|
|
}
|
|
|
|
}, [templates, state.templateID, partialUpdate]);
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
useEffect(() => {
|
2024-06-19 12:13:05 +03:00
|
|
|
if (!templateSchema) {
|
2023-11-04 01:29:21 +03:00
|
|
|
return;
|
|
|
|
}
|
2024-06-19 12:13:05 +03:00
|
|
|
let data = templateSchema.items;
|
2023-11-04 01:29:21 +03:00
|
|
|
if (state.filterCategory) {
|
2024-06-19 12:13:05 +03:00
|
|
|
data = applyFilterCategory(state.filterCategory, templateSchema);
|
2023-11-04 01:29:21 +03:00
|
|
|
}
|
|
|
|
setFilteredData(data);
|
2024-06-19 12:13:05 +03:00
|
|
|
}, [state.filterCategory, templateSchema]);
|
2023-11-04 01:29:21 +03:00
|
|
|
|
|
|
|
return (
|
2024-12-12 13:19:12 +03:00
|
|
|
<div className='cc-fade-in'>
|
2024-06-23 00:13:03 +03:00
|
|
|
<div className='flex border-t border-x rounded-t-md clr-input'>
|
2023-12-28 14:04:44 +03:00
|
|
|
<SelectSingle
|
2024-04-07 19:22:19 +03:00
|
|
|
noBorder
|
2024-06-23 00:13:03 +03:00
|
|
|
placeholder='Источник'
|
|
|
|
className='w-[12rem]'
|
|
|
|
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 })}
|
|
|
|
/>
|
|
|
|
<SelectSingle
|
|
|
|
noBorder
|
|
|
|
isSearchable={false}
|
2023-12-28 14:04:44 +03:00
|
|
|
placeholder='Выберите категорию'
|
2024-06-23 00:13:03 +03:00
|
|
|
className='flex-grow ml-1 border-none'
|
2023-12-28 14:04:44 +03:00
|
|
|
options={categorySelector}
|
|
|
|
value={
|
2024-06-19 12:13:05 +03:00
|
|
|
state.filterCategory && templateSchema
|
2023-12-28 14:04:44 +03:00
|
|
|
? {
|
|
|
|
value: state.filterCategory.id,
|
|
|
|
label: state.filterCategory.term_raw
|
|
|
|
}
|
|
|
|
: null
|
|
|
|
}
|
2024-06-19 12:13:05 +03:00
|
|
|
onChange={data =>
|
|
|
|
partialUpdate({ filterCategory: data ? templateSchema?.cstByID.get(data?.value) : undefined })
|
|
|
|
}
|
2023-12-28 14:04:44 +03:00
|
|
|
isClearable
|
|
|
|
/>
|
|
|
|
</div>
|
2024-05-16 22:39:28 +03:00
|
|
|
<PickConstituenta
|
2024-03-18 16:21:39 +03:00
|
|
|
id='dlg_template_picker'
|
2023-12-28 14:04:44 +03:00
|
|
|
value={state.prototype}
|
|
|
|
data={filteredData}
|
2025-02-04 20:35:55 +03:00
|
|
|
onChange={cst => partialUpdate({ prototype: cst })}
|
2023-12-28 14:04:44 +03:00
|
|
|
prefixID={prefixes.cst_template_ist}
|
2024-10-29 12:45:03 +03:00
|
|
|
className='rounded-t-none'
|
2024-09-10 17:40:58 +03:00
|
|
|
rows={8}
|
2023-12-28 14:04:44 +03:00
|
|
|
/>
|
2024-10-29 12:45:03 +03:00
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
<TextArea
|
2024-03-18 16:21:39 +03:00
|
|
|
id='dlg_template_term'
|
2023-12-28 14:04:44 +03:00
|
|
|
disabled
|
|
|
|
spellCheck
|
|
|
|
placeholder='Шаблон конституенты не выбран'
|
|
|
|
className='my-3'
|
|
|
|
rows={2}
|
|
|
|
value={prototypeInfo}
|
2023-12-18 19:42:27 +03:00
|
|
|
/>
|
2023-12-28 14:04:44 +03:00
|
|
|
<RSInput
|
2024-03-18 16:21:39 +03:00
|
|
|
id='dlg_template_expression'
|
2023-12-28 14:04:44 +03:00
|
|
|
disabled
|
|
|
|
placeholder='Выберите шаблон из списка'
|
|
|
|
height='5.1rem'
|
|
|
|
value={state.prototype?.definition_formal}
|
2023-11-04 01:29:21 +03:00
|
|
|
/>
|
2024-12-12 13:19:12 +03:00
|
|
|
</div>
|
2023-12-28 14:04:44 +03:00
|
|
|
);
|
2023-11-04 01:29:21 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 19:47:31 +03:00
|
|
|
export default TabTemplate;
|