ConceptPortal-public/rsconcept/frontend/src/dialogs/DlgChangeInputSchema.tsx

74 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-07-28 21:30:10 +03:00
'use client';
import clsx from 'clsx';
import { useCallback, useMemo, useState } from 'react';
import { IconReset } from '@/components/Icons';
import PickSchema from '@/components/select/PickSchema';
import Label from '@/components/ui/Label';
import MiniButton from '@/components/ui/MiniButton';
import Modal, { ModalProps } from '@/components/ui/Modal';
import { useLibrary } from '@/context/LibraryContext';
import { ILibraryItem, LibraryItemID, LibraryItemType } from '@/models/library';
2024-07-28 21:30:10 +03:00
import { IOperation, IOperationSchema } from '@/models/oss';
import { sortItemsForOSS } from '@/models/ossAPI';
2024-07-28 21:30:10 +03:00
interface DlgChangeInputSchemaProps extends Pick<ModalProps, 'hideWindow'> {
oss: IOperationSchema;
target: IOperation;
onSubmit: (newSchema: LibraryItemID | undefined) => void;
2024-07-28 21:30:10 +03:00
}
function DlgChangeInputSchema({ oss, hideWindow, target, onSubmit }: DlgChangeInputSchemaProps) {
const [selected, setSelected] = useState<LibraryItemID | undefined>(target.result ?? undefined);
const library = useLibrary();
const sortedItems = useMemo(() => sortItemsForOSS(oss, library.items), [oss, library.items]);
2024-07-28 21:30:10 +03:00
const baseFilter = useCallback(
(item: ILibraryItem) => !oss.schemas.includes(item.id) || item.id === selected || item.id === target.result,
[oss, selected, target]
);
const isValid = useMemo(() => target.result !== selected, [target, selected]);
const handleSelectLocation = useCallback((newValue: LibraryItemID) => {
setSelected(newValue);
}, []);
return (
<Modal
overflowVisible
header='Выбор концептуальной схемы'
submitText='Подтвердить выбор'
hideWindow={hideWindow}
canSubmit={isValid}
onSubmit={() => onSubmit(selected)}
2024-07-28 21:30:10 +03:00
className={clsx('w-[35rem]', 'pb-3 px-6 cc-column')}
>
<div className='flex justify-between gap-3 items-center'>
<div className='flex gap-3'>
<Label text='Загружаемая концептуальная схема' />
<MiniButton
title='Сбросить выбор схемы'
noHover
noPadding
icon={<IconReset size='1.25rem' className='icon-primary' />}
onClick={() => setSelected(undefined)}
disabled={selected == undefined}
/>
</div>
</div>
<PickSchema
items={sortedItems}
itemType={LibraryItemType.RSFORM}
2024-07-28 21:30:10 +03:00
value={selected} // prettier: split-line
onSelectValue={handleSelectLocation}
2024-09-12 13:27:20 +03:00
rows={14}
2024-07-28 21:30:10 +03:00
baseFilter={baseFilter}
/>
</Modal>
);
}
export default DlgChangeInputSchema;