Portal/rsconcept/frontend/src/dialogs/DlgChangeInputSchema.tsx

78 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-07-28 21:29:46 +03:00
'use client';
import clsx from 'clsx';
import { useState } from 'react';
2024-07-28 21:29:46 +03:00
2025-01-23 19:41:31 +03:00
import { useLibrary } from '@/backend/library/useLibrary';
2024-07-28 21:29:46 +03:00
import { IconReset } from '@/components/Icons';
import PickSchema from '@/components/select/PickSchema';
2025-02-07 10:53:49 +03:00
import { MiniButton } from '@/components/ui/Control';
import { Label } from '@/components/ui/Input';
2025-02-06 20:27:56 +03:00
import { ModalForm } from '@/components/ui/Modal';
import { ILibraryItem, LibraryItemID, LibraryItemType } from '@/models/library';
import { IOperation, IOperationSchema, OperationID } from '@/models/oss';
import { sortItemsForOSS } from '@/models/ossAPI';
import { useDialogsStore } from '@/stores/dialogs';
2024-07-28 21:29:46 +03:00
export interface DlgChangeInputSchemaProps {
2024-07-28 21:29:46 +03:00
oss: IOperationSchema;
target: IOperation;
onSubmit: (target: OperationID, newSchema: LibraryItemID | undefined) => void;
2024-07-28 21:29:46 +03:00
}
function DlgChangeInputSchema() {
const { oss, target, onSubmit } = useDialogsStore(state => state.props as DlgChangeInputSchemaProps);
2024-07-28 21:29:46 +03:00
const [selected, setSelected] = useState<LibraryItemID | undefined>(target.result ?? undefined);
2025-01-23 19:41:31 +03:00
const { items } = useLibrary();
const sortedItems = sortItemsForOSS(oss, items);
const isValid = target.result !== selected;
2024-07-28 21:29:46 +03:00
function baseFilter(item: ILibraryItem) {
return !oss.schemas.includes(item.id) || item.id === selected || item.id === target.result;
}
2024-07-28 21:29:46 +03:00
function handleSelectLocation(newValue: LibraryItemID) {
2024-07-28 21:29:46 +03:00
setSelected(newValue);
}
2024-07-28 21:29:46 +03:00
2025-02-06 14:09:20 +03:00
function handleSubmit() {
onSubmit(target.id, selected);
return true;
}
2024-07-28 21:29:46 +03:00
return (
2025-02-06 20:27:56 +03:00
<ModalForm
2024-07-28 21:29:46 +03:00
overflowVisible
header='Выбор концептуальной схемы'
submitText='Подтвердить выбор'
canSubmit={isValid}
2025-02-06 14:09:20 +03:00
onSubmit={handleSubmit}
2024-07-28 21:29:46 +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:29:46 +03:00
value={selected} // prettier: split-line
onChange={handleSelectLocation}
2024-09-12 13:27:06 +03:00
rows={14}
2024-07-28 21:29:46 +03:00
baseFilter={baseFilter}
/>
2025-02-06 20:27:56 +03:00
</ModalForm>
2024-07-28 21:29:46 +03:00
);
}
export default DlgChangeInputSchema;