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

140 lines
4.8 KiB
TypeScript
Raw Normal View History

'use client';
import clsx from 'clsx';
import { useState } from 'react';
import { useLibrary } from '@/backend/library/useLibrary';
import { ICstRelocateDTO } from '@/backend/oss/api';
import { useRSForm } from '@/backend/rsform/useRSForm';
2024-10-28 23:55:25 +03:00
import { RelocateUpIcon } from '@/components/DomainIcons';
import PickMultiConstituenta from '@/components/select/PickMultiConstituenta';
import SelectLibraryItem from '@/components/select/SelectLibraryItem';
import Loader from '@/components/ui/Loader';
2024-10-28 23:55:25 +03:00
import MiniButton from '@/components/ui/MiniButton';
import Modal from '@/components/ui/Modal';
import { ILibraryItem, LibraryItemID } from '@/models/library';
2024-10-29 12:06:43 +03:00
import { HelpTopic } from '@/models/miscellaneous';
import { IOperation, IOperationSchema } from '@/models/oss';
import { getRelocateCandidates } from '@/models/ossAPI';
import { ConstituentaID } from '@/models/rsform';
import { useDialogsStore } from '@/stores/dialogs';
import { prefixes } from '@/utils/constants';
export interface DlgRelocateConstituentsProps {
oss: IOperationSchema;
2024-10-28 23:55:25 +03:00
initialTarget?: IOperation;
onSubmit: (data: ICstRelocateDTO) => void;
}
function DlgRelocateConstituents() {
const { oss, initialTarget, onSubmit } = useDialogsStore(state => state.props as DlgRelocateConstituentsProps);
const { items: libraryItems } = useLibrary();
2024-10-28 23:55:25 +03:00
const [directionUp, setDirectionUp] = useState(true);
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>(
libraryItems.find(item => item.id === initialTarget?.result)
2024-10-28 23:55:25 +03:00
);
const isValid = !!destination && selected.length > 0;
2024-10-28 23:55:25 +03:00
const operation = oss.items.find(item => item.result === source?.id);
const sourceSchemas = libraryItems.filter(item => oss.schemas.includes(item.id));
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);
return ids.map(id => libraryItems.find(item => item.id === id)).filter(item => item !== undefined);
})();
const sourceData = useRSForm({ itemID: source?.id });
const filteredConstituents = (() => {
2024-10-28 23:55:25 +03:00
if (!sourceData.schema || !destination || !operation) {
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);
})();
function toggleDirection() {
2024-10-28 23:55:25 +03:00
setDirectionUp(prev => !prev);
setDestination(undefined);
}
function handleSelectSource(newValue: ILibraryItem | undefined) {
2024-10-28 23:55:25 +03:00
setSource(newValue);
setDestination(undefined);
setSelected([]);
}
function handleSelectDestination(newValue: ILibraryItem | undefined) {
setDestination(newValue);
2024-10-28 23:55:25 +03:00
setSelected([]);
}
function handleSubmit() {
2024-10-28 14:53:41 +03:00
if (!destination) {
return;
}
onSubmit({
2024-10-28 14:53:41 +03:00
destination: destination.id,
items: selected
});
}
return (
<Modal
2024-10-29 12:06:43 +03:00
header='Перенос конституент'
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-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-28 23:55:25 +03:00
</div>
{sourceData.isLoading ? <Loader /> : null}
{!sourceData.isLoading && sourceData.schema ? (
<PickMultiConstituenta
noBorder
schema={sourceData.schema}
data={filteredConstituents}
rows={12}
prefixID={prefixes.dlg_cst_constituents_list}
selected={selected}
setSelected={setSelected}
/>
) : null}
2024-10-28 23:55:25 +03:00
</div>
</Modal>
);
}
export default DlgRelocateConstituents;