2024-06-07 20:17:03 +03:00
|
|
|
'use client';
|
|
|
|
|
2025-02-06 14:09:20 +03:00
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
2024-06-07 20:17:03 +03:00
|
|
|
import clsx from 'clsx';
|
2025-02-06 14:09:20 +03:00
|
|
|
import { Controller, useForm, useWatch } from 'react-hook-form';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
2025-02-10 01:32:16 +03:00
|
|
|
import { Checkbox, TextArea, TextInput } from '@/components/Input';
|
|
|
|
import { ModalForm } from '@/components/Modal';
|
2025-02-10 14:10:51 +03:00
|
|
|
import { IVersionCreateDTO, schemaVersionCreate } from '@/features/library/backend/api';
|
2025-02-10 01:32:16 +03:00
|
|
|
import { useVersionCreate } from '@/features/library/backend/useVersionCreate';
|
|
|
|
import { IVersionInfo, LibraryItemID, VersionID } from '@/features/library/models/library';
|
|
|
|
import { nextVersion } from '@/features/library/models/libraryAPI';
|
2025-01-16 16:31:03 +03:00
|
|
|
import { useDialogsStore } from '@/stores/dialogs';
|
2025-02-06 20:27:56 +03:00
|
|
|
import { errors } from '@/utils/labels';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
2025-02-10 01:32:16 +03:00
|
|
|
import { ConstituentaID } from '../models/rsform';
|
|
|
|
|
2025-01-16 16:31:03 +03:00
|
|
|
export interface DlgCreateVersionProps {
|
2025-02-06 14:09:20 +03:00
|
|
|
itemID: LibraryItemID;
|
2024-06-07 20:17:03 +03:00
|
|
|
versions: IVersionInfo[];
|
2025-02-06 14:09:20 +03:00
|
|
|
onCreate: (newVersion: VersionID) => void;
|
2024-08-24 12:29:09 +03:00
|
|
|
selected: ConstituentaID[];
|
|
|
|
totalCount: number;
|
2024-06-07 20:17:03 +03:00
|
|
|
}
|
|
|
|
|
2025-01-16 16:31:03 +03:00
|
|
|
function DlgCreateVersion() {
|
2025-02-06 14:09:20 +03:00
|
|
|
const {
|
|
|
|
itemID, //
|
|
|
|
versions,
|
|
|
|
selected,
|
|
|
|
totalCount,
|
|
|
|
onCreate
|
|
|
|
} = useDialogsStore(state => state.props as DlgCreateVersionProps);
|
|
|
|
const { versionCreate } = useVersionCreate();
|
2024-06-07 20:17:03 +03:00
|
|
|
|
2025-02-06 14:09:20 +03:00
|
|
|
const { register, handleSubmit, control } = useForm<IVersionCreateDTO>({
|
2025-02-10 14:10:51 +03:00
|
|
|
resolver: zodResolver(schemaVersionCreate),
|
2025-02-06 14:09:20 +03:00
|
|
|
defaultValues: {
|
|
|
|
version: versions.length > 0 ? nextVersion(versions[0].version) : '1.0.0',
|
|
|
|
description: '',
|
|
|
|
items: undefined
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const version = useWatch({ control, name: 'version' });
|
2024-12-13 21:30:49 +03:00
|
|
|
const canSubmit = !versions.find(ver => ver.version === version);
|
2024-06-07 20:17:03 +03:00
|
|
|
|
2025-02-06 14:09:20 +03:00
|
|
|
function onSubmit(data: IVersionCreateDTO) {
|
2025-02-11 20:15:34 +03:00
|
|
|
return versionCreate({ itemID, data }).then(onCreate);
|
2024-06-07 20:17:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2025-02-06 20:27:56 +03:00
|
|
|
<ModalForm
|
|
|
|
header='Создание версии'
|
|
|
|
className={clsx('cc-column', 'w-[30rem]', 'py-2 px-6')}
|
|
|
|
canSubmit={canSubmit}
|
|
|
|
submitInvalidTooltip={errors.versionTaken}
|
|
|
|
submitText='Создать'
|
|
|
|
onSubmit={event => void handleSubmit(onSubmit)(event)}
|
|
|
|
>
|
|
|
|
<TextInput id='dlg_version' {...register('version')} dense label='Версия' className='w-[16rem]' />
|
|
|
|
<TextArea id='dlg_description' {...register('description')} spellCheck label='Описание' rows={3} />
|
|
|
|
{selected.length > 0 ? (
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name='items'
|
|
|
|
render={({ field }) => (
|
|
|
|
<Checkbox
|
|
|
|
id='dlg_only_selected'
|
|
|
|
label={`Только выбранные конституенты [${selected.length} из ${totalCount}]`}
|
|
|
|
value={field.value !== undefined}
|
|
|
|
onChange={value => field.onChange(value ? selected : undefined)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</ModalForm>
|
2024-06-07 20:17:03 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DlgCreateVersion;
|