2024-10-23 15:31:24 +03:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import clsx from 'clsx';
|
2024-12-13 21:31:09 +03:00
|
|
|
import { useState } from 'react';
|
2024-10-23 15:31:24 +03:00
|
|
|
|
2025-02-10 01:32:55 +03:00
|
|
|
import { MiniButton } from '@/components/Control';
|
2024-10-28 23:55:25 +03:00
|
|
|
import { RelocateUpIcon } from '@/components/DomainIcons';
|
2025-02-10 01:32:55 +03:00
|
|
|
import { Loader } from '@/components/Loader';
|
|
|
|
import { ModalForm } from '@/components/Modal';
|
|
|
|
import { HelpTopic } from '@/features/help/models/helpTopic';
|
|
|
|
import { useLibrary } from '@/features/library/backend/useLibrary';
|
|
|
|
import SelectLibraryItem from '@/features/library/components/SelectLibraryItem';
|
|
|
|
import { ILibraryItem, LibraryItemID } from '@/features/library/models/library';
|
|
|
|
import { ICstRelocateDTO } from '@/features/oss/backend/api';
|
|
|
|
import { useRSForm } from '@/features/rsform/backend/useRSForm';
|
|
|
|
import PickMultiConstituenta from '@/features/rsform/components/PickMultiConstituenta';
|
|
|
|
import { ConstituentaID } from '@/features/rsform/models/rsform';
|
2025-01-16 16:31:32 +03:00
|
|
|
import { useDialogsStore } from '@/stores/dialogs';
|
2024-10-23 15:31:24 +03:00
|
|
|
|
2025-02-10 01:32:55 +03:00
|
|
|
import { IOperation, IOperationSchema } from '../models/oss';
|
|
|
|
import { getRelocateCandidates } from '../models/ossAPI';
|
|
|
|
|
2025-01-16 16:31:32 +03:00
|
|
|
export interface DlgRelocateConstituentsProps {
|
2024-10-23 15:31:24 +03:00
|
|
|
oss: IOperationSchema;
|
2024-10-28 23:55:25 +03:00
|
|
|
initialTarget?: IOperation;
|
2025-01-27 15:03:48 +03:00
|
|
|
onSubmit: (data: ICstRelocateDTO) => void;
|
2024-10-23 15:31:24 +03:00
|
|
|
}
|
|
|
|
|
2025-01-16 16:31:32 +03:00
|
|
|
function DlgRelocateConstituents() {
|
|
|
|
const { oss, initialTarget, onSubmit } = useDialogsStore(state => state.props as DlgRelocateConstituentsProps);
|
2025-01-27 15:03:48 +03:00
|
|
|
const { items: libraryItems } = useLibrary();
|
2024-10-23 15:31:24 +03:00
|
|
|
|
2024-10-28 23:55:25 +03:00
|
|
|
const [directionUp, setDirectionUp] = useState(true);
|
2024-10-23 15:31:24 +03:00
|
|
|
const [destination, setDestination] = useState<ILibraryItem | undefined>(undefined);
|
|
|
|
const [selected, setSelected] = useState<ConstituentaID[]>([]);
|
2024-10-28 23:55:25 +03:00
|
|
|
const [source, setSource] = useState<ILibraryItem | undefined>(
|
2025-01-27 15:03:48 +03:00
|
|
|
libraryItems.find(item => item.id === initialTarget?.result)
|
2024-10-28 23:55:25 +03:00
|
|
|
);
|
2024-12-13 21:31:09 +03:00
|
|
|
const isValid = !!destination && selected.length > 0;
|
2024-10-28 23:55:25 +03:00
|
|
|
|
2024-12-13 21:31:09 +03:00
|
|
|
const operation = oss.items.find(item => item.result === source?.id);
|
2025-01-27 15:03:48 +03:00
|
|
|
const sourceSchemas = libraryItems.filter(item => oss.schemas.includes(item.id));
|
2024-12-13 21:31:09 +03:00
|
|
|
const destinationSchemas = (() => {
|
2024-10-28 23:55:25 +03:00
|
|
|
if (!operation) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const node = oss.graph.at(operation.id)!;
|
|
|
|
const ids: LibraryItemID[] = directionUp
|
|
|
|
? node.inputs.map(id => oss.operationByID.get(id)!.result).filter(id => id !== null)
|
|
|
|
: node.outputs.map(id => oss.operationByID.get(id)!.result).filter(id => id !== null);
|
2025-01-27 15:03:48 +03:00
|
|
|
return ids.map(id => libraryItems.find(item => item.id === id)).filter(item => item !== undefined);
|
2024-12-13 21:31:09 +03:00
|
|
|
})();
|
2024-10-23 15:31:24 +03:00
|
|
|
|
2025-01-27 15:03:48 +03:00
|
|
|
const sourceData = useRSForm({ itemID: source?.id });
|
2024-12-13 21:31:09 +03:00
|
|
|
const filteredConstituents = (() => {
|
2024-10-28 23:55:25 +03:00
|
|
|
if (!sourceData.schema || !destination || !operation) {
|
2024-10-23 15:31:24 +03:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const destinationOperation = oss.items.find(item => item.result === destination.id);
|
2024-10-28 23:55:25 +03:00
|
|
|
return getRelocateCandidates(operation.id, destinationOperation!.id, sourceData.schema, oss);
|
2024-12-13 21:31:09 +03:00
|
|
|
})();
|
2024-10-23 15:31:24 +03:00
|
|
|
|
2024-12-13 21:31:09 +03:00
|
|
|
function toggleDirection() {
|
2024-10-28 23:55:25 +03:00
|
|
|
setDirectionUp(prev => !prev);
|
|
|
|
setDestination(undefined);
|
2024-12-13 21:31:09 +03:00
|
|
|
}
|
2024-10-23 15:31:24 +03:00
|
|
|
|
2024-12-13 21:31:09 +03:00
|
|
|
function handleSelectSource(newValue: ILibraryItem | undefined) {
|
2024-10-28 23:55:25 +03:00
|
|
|
setSource(newValue);
|
|
|
|
setDestination(undefined);
|
2024-10-23 15:31:24 +03:00
|
|
|
setSelected([]);
|
2024-12-13 21:31:09 +03:00
|
|
|
}
|
2024-10-23 15:31:24 +03:00
|
|
|
|
2024-12-13 21:31:09 +03:00
|
|
|
function handleSelectDestination(newValue: ILibraryItem | undefined) {
|
2024-10-23 15:31:24 +03:00
|
|
|
setDestination(newValue);
|
2024-10-28 23:55:25 +03:00
|
|
|
setSelected([]);
|
2024-12-13 21:31:09 +03:00
|
|
|
}
|
2024-10-23 15:31:24 +03:00
|
|
|
|
2024-12-13 21:31:09 +03:00
|
|
|
function handleSubmit() {
|
2025-02-06 14:10:18 +03:00
|
|
|
if (destination) {
|
|
|
|
onSubmit({
|
|
|
|
destination: destination.id,
|
|
|
|
items: selected
|
|
|
|
});
|
2024-10-28 14:53:41 +03:00
|
|
|
}
|
2025-02-06 14:10:18 +03:00
|
|
|
return true;
|
2024-12-13 21:31:09 +03:00
|
|
|
}
|
2024-10-23 15:31:24 +03:00
|
|
|
|
|
|
|
return (
|
2025-02-06 20:28:23 +03:00
|
|
|
<ModalForm
|
2024-10-29 12:06:43 +03:00
|
|
|
header='Перенос конституент'
|
2024-10-23 15:31:24 +03:00
|
|
|
submitText='Переместить'
|
|
|
|
canSubmit={isValid}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
className={clsx('w-[40rem] h-[33rem]', 'py-3 px-6')}
|
2024-10-29 12:06:43 +03:00
|
|
|
helpTopic={HelpTopic.UI_RELOCATE_CST}
|
2024-10-23 15:31:24 +03:00
|
|
|
>
|
2024-10-28 23:55:25 +03:00
|
|
|
<div className='flex flex-col border'>
|
|
|
|
<div className='flex gap-1 items-center clr-input border-b rounded-t-md'>
|
|
|
|
<SelectLibraryItem
|
|
|
|
noBorder
|
|
|
|
className='w-1/2'
|
|
|
|
placeholder='Выберите исходную схему'
|
|
|
|
items={sourceSchemas}
|
|
|
|
value={source}
|
2025-02-04 20:35:55 +03:00
|
|
|
onChange={handleSelectSource}
|
2024-10-28 23:55:25 +03:00
|
|
|
/>
|
|
|
|
<MiniButton
|
|
|
|
title='Направление перемещения'
|
|
|
|
icon={<RelocateUpIcon value={directionUp} />}
|
|
|
|
onClick={toggleDirection}
|
|
|
|
/>
|
|
|
|
<SelectLibraryItem
|
|
|
|
noBorder
|
|
|
|
className='w-1/2'
|
|
|
|
placeholder='Выберите целевую схему'
|
|
|
|
items={destinationSchemas}
|
|
|
|
value={destination}
|
2025-02-04 20:35:55 +03:00
|
|
|
onChange={handleSelectDestination}
|
2024-10-23 15:31:24 +03:00
|
|
|
/>
|
2024-10-28 23:55:25 +03:00
|
|
|
</div>
|
2025-01-29 16:18:41 +03:00
|
|
|
{sourceData.isLoading ? <Loader /> : null}
|
|
|
|
{!sourceData.isLoading && sourceData.schema ? (
|
|
|
|
<PickMultiConstituenta
|
|
|
|
noBorder
|
|
|
|
schema={sourceData.schema}
|
2025-02-05 12:43:16 +03:00
|
|
|
items={filteredConstituents}
|
2025-01-29 16:18:41 +03:00
|
|
|
rows={12}
|
2025-02-04 20:35:55 +03:00
|
|
|
value={selected}
|
|
|
|
onChange={setSelected}
|
2025-01-29 16:18:41 +03:00
|
|
|
/>
|
|
|
|
) : null}
|
2024-10-28 23:55:25 +03:00
|
|
|
</div>
|
2025-02-06 20:28:23 +03:00
|
|
|
</ModalForm>
|
2024-10-23 15:31:24 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DlgRelocateConstituents;
|