Portal/rsconcept/frontend/src/dialogs/DlgCstTemplate/TabTemplate.tsx

132 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
'use client';
import { Dispatch, useEffect, useState } from 'react';
2024-06-07 20:17:03 +03:00
import RSInput from '@/components/RSInput';
import PickConstituenta from '@/components/select/PickConstituenta';
import SelectSingle from '@/components/ui/SelectSingle';
import TextArea from '@/components/ui/TextArea';
import { useLibrary } from '@/context/LibraryContext';
import { CATEGORY_CST_TYPE, IConstituenta, IRSForm } from '@/models/rsform';
import { applyFilterCategory } from '@/models/rsformAPI';
import { prefixes } from '@/utils/constants';
2024-08-29 12:41:59 +03:00
2024-06-07 20:17:03 +03:00
export interface ITemplateState {
templateID?: number;
prototype?: IConstituenta;
filterCategory?: IConstituenta;
}
interface TabTemplateProps {
2024-06-07 20:17:03 +03:00
state: ITemplateState;
partialUpdate: Dispatch<Partial<ITemplateState>>;
2024-08-29 12:41:59 +03:00
templateSchema?: IRSForm;
2024-06-07 20:17:03 +03:00
}
2024-08-29 12:41:59 +03:00
function TabTemplate({ state, partialUpdate, templateSchema }: TabTemplateProps) {
const { templates } = useLibrary();
2024-06-07 20:17:03 +03:00
const [filteredData, setFilteredData] = useState<IConstituenta[]>([]);
const prototypeInfo = !state.prototype
? ''
: `${state.prototype?.term_raw}${state.prototype?.definition_raw ? `${state.prototype?.definition_raw}` : ''}`;
2024-06-07 20:17:03 +03:00
const templateSelector = templates.map(template => ({
value: template.id,
label: template.title
}));
2024-06-07 20:17:03 +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
}));
2024-06-07 20:17:03 +03:00
useEffect(() => {
if (templates.length > 0 && !state.templateID) {
partialUpdate({ templateID: templates[0].id });
}
}, [templates, state.templateID, partialUpdate]);
useEffect(() => {
2024-06-19 12:09:10 +03:00
if (!templateSchema) {
2024-06-07 20:17:03 +03:00
return;
}
2024-06-19 12:09:10 +03:00
let data = templateSchema.items;
2024-06-07 20:17:03 +03:00
if (state.filterCategory) {
2024-06-19 12:09:10 +03:00
data = applyFilterCategory(state.filterCategory, templateSchema);
2024-06-07 20:17:03 +03:00
}
setFilteredData(data);
2024-06-19 12:09:10 +03:00
}, [state.filterCategory, templateSchema]);
2024-06-07 20:17:03 +03:00
return (
2024-12-12 13:17:24 +03:00
<div className='cc-fade-in'>
2024-06-23 00:12:18 +03:00
<div className='flex border-t border-x rounded-t-md clr-input'>
2024-06-07 20:17:03 +03:00
<SelectSingle
noBorder
2024-06-23 00:12:18 +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}
2024-06-07 20:17:03 +03:00
placeholder='Выберите категорию'
2024-06-23 00:12:18 +03:00
className='flex-grow ml-1 border-none'
2024-06-07 20:17:03 +03:00
options={categorySelector}
value={
2024-06-19 12:09:10 +03:00
state.filterCategory && templateSchema
2024-06-07 20:17:03 +03:00
? {
value: state.filterCategory.id,
label: state.filterCategory.term_raw
}
: null
}
2024-06-19 12:09:10 +03:00
onChange={data =>
partialUpdate({ filterCategory: data ? templateSchema?.cstByID.get(data?.value) : undefined })
}
2024-06-07 20:17:03 +03:00
isClearable
/>
</div>
<PickConstituenta
id='dlg_template_picker'
value={state.prototype}
data={filteredData}
onSelectValue={cst => partialUpdate({ prototype: cst })}
prefixID={prefixes.cst_template_ist}
className='rounded-t-none'
rows={8}
2024-06-07 20:17:03 +03:00
/>
2024-06-07 20:17:03 +03:00
<TextArea
id='dlg_template_term'
disabled
spellCheck
placeholder='Шаблон конституенты не выбран'
className='my-3'
rows={2}
value={prototypeInfo}
/>
<RSInput
id='dlg_template_expression'
disabled
placeholder='Выберите шаблон из списка'
height='5.1rem'
value={state.prototype?.definition_formal}
/>
2024-12-12 13:17:24 +03:00
</div>
2024-06-07 20:17:03 +03:00
);
}
export default TabTemplate;