2023-12-13 14:32:57 +03:00
|
|
|
'use client';
|
|
|
|
|
2023-11-04 01:29:21 +03:00
|
|
|
import { Dispatch, useEffect, useMemo, useState } from 'react';
|
|
|
|
|
2023-12-13 14:32:57 +03:00
|
|
|
import SelectSingle from '@/components/Common/SelectSingle';
|
|
|
|
import TextArea from '@/components/Common/TextArea';
|
|
|
|
import RSInput from '@/components/RSInput';
|
|
|
|
import ConstituentaPicker from '@/components/Shared/ConstituentaPicker';
|
|
|
|
import { useLibrary } from '@/context/LibraryContext';
|
|
|
|
import { CATEGORY_CST_TYPE, IConstituenta, IRSForm } from '@/models/rsform';
|
|
|
|
import { applyFilterCategory } from '@/models/rsformAPI';
|
|
|
|
import { prefixes } from '@/utils/constants';
|
2023-11-04 01:29:21 +03:00
|
|
|
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();
|
2023-12-05 01:22:44 +03:00
|
|
|
const [selectedSchema, setSelectedSchema] = useState<IRSForm | undefined>(undefined);
|
2023-11-04 01:29:21 +03:00
|
|
|
|
|
|
|
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-12-18 19:42:27 +03:00
|
|
|
<>
|
2023-12-19 11:18:28 +03:00
|
|
|
<div className='flex'>
|
2023-12-18 19:42:27 +03:00
|
|
|
<SelectSingle
|
|
|
|
placeholder='Выберите категорию'
|
|
|
|
className='flex-grow border-none'
|
|
|
|
options={categorySelector}
|
|
|
|
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
|
|
|
|
placeholder='Выберите источник'
|
2023-12-19 11:18:28 +03:00
|
|
|
className='w-[12rem]'
|
2023-12-18 19:42:27 +03:00
|
|
|
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)})}
|
2023-11-04 01:29:21 +03:00
|
|
|
/>
|
|
|
|
</div>
|
2023-12-18 19:42:27 +03:00
|
|
|
<ConstituentaPicker
|
|
|
|
value={state.prototype}
|
|
|
|
data={filteredData}
|
|
|
|
onSelectValue={cst => partialUpdate( { prototype: cst } )}
|
|
|
|
prefixID={prefixes.cst_template_ist}
|
|
|
|
rows={9}
|
|
|
|
/>
|
|
|
|
<TextArea disabled spellCheck
|
2023-11-04 01:29:21 +03:00
|
|
|
placeholder='Шаблон конституенты не выбран'
|
2023-12-18 19:42:27 +03:00
|
|
|
className='my-3'
|
|
|
|
rows={2}
|
2023-11-04 01:29:21 +03:00
|
|
|
value={prototypeInfo}
|
|
|
|
/>
|
2023-12-18 19:42:27 +03:00
|
|
|
<RSInput disabled
|
2023-11-04 01:29:21 +03:00
|
|
|
placeholder='Выберите шаблон из списка'
|
2023-12-18 19:42:27 +03:00
|
|
|
height='5.1rem'
|
2023-11-04 01:29:21 +03:00
|
|
|
value={state.prototype?.definition_formal}
|
|
|
|
/>
|
2023-12-18 19:42:27 +03:00
|
|
|
</>);
|
2023-11-04 01:29:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default TemplateTab;
|