2024-10-23 15:31:24 +03:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import clsx from 'clsx';
|
2024-10-28 23:55:25 +03:00
|
|
|
import { useCallback, useMemo, useState } from 'react';
|
2024-10-23 15:31:24 +03:00
|
|
|
|
2024-10-28 23:55:25 +03:00
|
|
|
import { RelocateUpIcon } from '@/components/DomainIcons';
|
2024-10-23 15:31:24 +03:00
|
|
|
import PickMultiConstituenta from '@/components/select/PickMultiConstituenta';
|
|
|
|
import SelectLibraryItem from '@/components/select/SelectLibraryItem';
|
2024-10-28 23:55:25 +03:00
|
|
|
import MiniButton from '@/components/ui/MiniButton';
|
2024-10-23 15:31:24 +03:00
|
|
|
import Modal, { ModalProps } from '@/components/ui/Modal';
|
|
|
|
import DataLoader from '@/components/wrap/DataLoader';
|
|
|
|
import { useLibrary } from '@/context/LibraryContext';
|
|
|
|
import useRSFormDetails from '@/hooks/useRSFormDetails';
|
|
|
|
import { ILibraryItem, LibraryItemID } from '@/models/library';
|
|
|
|
import { ICstRelocateData, IOperation, IOperationSchema } from '@/models/oss';
|
|
|
|
import { getRelocateCandidates } from '@/models/ossAPI';
|
|
|
|
import { ConstituentaID } from '@/models/rsform';
|
|
|
|
import { prefixes } from '@/utils/constants';
|
|
|
|
|
|
|
|
interface DlgRelocateConstituentsProps extends Pick<ModalProps, 'hideWindow'> {
|
|
|
|
oss: IOperationSchema;
|
2024-10-28 23:55:25 +03:00
|
|
|
initialTarget?: IOperation;
|
2024-10-23 15:31:24 +03:00
|
|
|
onSubmit: (data: ICstRelocateData) => void;
|
|
|
|
}
|
|
|
|
|
2024-10-28 23:55:25 +03:00
|
|
|
function DlgRelocateConstituents({ oss, hideWindow, initialTarget, onSubmit }: DlgRelocateConstituentsProps) {
|
2024-10-23 15:31:24 +03:00
|
|
|
const library = useLibrary();
|
|
|
|
|
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>(
|
|
|
|
library.items.find(item => item.id === initialTarget?.result)
|
|
|
|
);
|
|
|
|
|
|
|
|
const operation = useMemo(() => oss.items.find(item => item.result === source?.id), [oss, source]);
|
|
|
|
const sourceSchemas = useMemo(() => library.items.filter(item => oss.schemas.includes(item.id)), [library, oss]);
|
|
|
|
const destinationSchemas = useMemo(() => {
|
|
|
|
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);
|
|
|
|
return ids.map(id => library.items.find(item => item.id === id)).filter(item => item !== undefined);
|
|
|
|
}, [oss, library.items, operation, directionUp]);
|
2024-10-23 15:31:24 +03:00
|
|
|
|
2024-10-28 23:55:25 +03:00
|
|
|
const sourceData = useRSFormDetails({ target: source ? String(source.id) : undefined });
|
|
|
|
const filteredConstituents = useMemo(() => {
|
|
|
|
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);
|
|
|
|
}, [destination, operation, sourceData.schema, oss]);
|
2024-10-23 15:31:24 +03:00
|
|
|
|
|
|
|
const isValid = useMemo(() => !!destination && selected.length > 0, [destination, selected]);
|
2024-10-28 23:55:25 +03:00
|
|
|
const toggleDirection = useCallback(() => {
|
|
|
|
setDirectionUp(prev => !prev);
|
|
|
|
setDestination(undefined);
|
|
|
|
}, []);
|
2024-10-23 15:31:24 +03:00
|
|
|
|
2024-10-28 23:55:25 +03:00
|
|
|
const handleSelectSource = useCallback((newValue: ILibraryItem | undefined) => {
|
|
|
|
setSource(newValue);
|
|
|
|
setDestination(undefined);
|
2024-10-23 15:31:24 +03:00
|
|
|
setSelected([]);
|
2024-10-28 23:55:25 +03:00
|
|
|
}, []);
|
2024-10-23 15:31:24 +03:00
|
|
|
|
|
|
|
const handleSelectDestination = useCallback((newValue: ILibraryItem | undefined) => {
|
|
|
|
setDestination(newValue);
|
2024-10-28 23:55:25 +03:00
|
|
|
setSelected([]);
|
2024-10-23 15:31:24 +03:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleSubmit = useCallback(() => {
|
2024-10-28 14:53:41 +03:00
|
|
|
if (!destination) {
|
|
|
|
return;
|
|
|
|
}
|
2024-10-23 15:31:24 +03:00
|
|
|
const data: ICstRelocateData = {
|
2024-10-28 14:53:41 +03:00
|
|
|
destination: destination.id,
|
|
|
|
items: selected
|
2024-10-23 15:31:24 +03:00
|
|
|
};
|
|
|
|
onSubmit(data);
|
2024-10-28 14:53:41 +03:00
|
|
|
}, [destination, onSubmit, selected]);
|
2024-10-23 15:31:24 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
header='Перемещение конституент'
|
|
|
|
submitText='Переместить'
|
|
|
|
hideWindow={hideWindow}
|
|
|
|
canSubmit={isValid}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
className={clsx('w-[40rem] h-[33rem]', 'py-3 px-6')}
|
|
|
|
>
|
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}
|
|
|
|
onSelectValue={handleSelectSource}
|
|
|
|
/>
|
|
|
|
<MiniButton
|
|
|
|
title='Направление перемещения'
|
|
|
|
icon={<RelocateUpIcon value={directionUp} />}
|
|
|
|
onClick={toggleDirection}
|
|
|
|
/>
|
|
|
|
<SelectLibraryItem
|
|
|
|
noBorder
|
|
|
|
className='w-1/2'
|
|
|
|
placeholder='Выберите целевую схему'
|
|
|
|
items={destinationSchemas}
|
|
|
|
value={destination}
|
|
|
|
onSelectValue={handleSelectDestination}
|
2024-10-23 15:31:24 +03:00
|
|
|
/>
|
2024-10-28 23:55:25 +03:00
|
|
|
</div>
|
|
|
|
<DataLoader id='dlg-relocate-constituents' isLoading={sourceData.loading} error={sourceData.error}>
|
|
|
|
{sourceData.schema ? (
|
|
|
|
<PickMultiConstituenta
|
|
|
|
noBorder
|
|
|
|
schema={sourceData.schema}
|
|
|
|
data={filteredConstituents}
|
|
|
|
rows={12}
|
|
|
|
prefixID={prefixes.dlg_cst_constituents_list}
|
|
|
|
selected={selected}
|
|
|
|
setSelected={setSelected}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</DataLoader>
|
|
|
|
</div>
|
2024-10-23 15:31:24 +03:00
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DlgRelocateConstituents;
|