2024-06-07 20:17:03 +03:00
|
|
|
'use client';
|
|
|
|
|
2025-02-12 21:36:03 +03:00
|
|
|
import { Controller, useForm, useWatch } from 'react-hook-form';
|
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-10 01:32:16 +03:00
|
|
|
import { Checkbox, TextArea, TextInput } from '@/components/Input';
|
|
|
|
import { ModalForm } from '@/components/Modal';
|
2025-01-16 16:31:03 +03:00
|
|
|
import { useDialogsStore } from '@/stores/dialogs';
|
2025-02-12 01:34:35 +03:00
|
|
|
import { errorMsg } from '@/utils/labels';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
2025-02-12 20:53:01 +03:00
|
|
|
import { IVersionCreateDTO, schemaVersionCreate } from '../backend/types';
|
2025-02-12 15:12:59 +03:00
|
|
|
import { useVersionCreate } from '../backend/useVersionCreate';
|
|
|
|
import { IVersionInfo } from '../models/library';
|
|
|
|
import { nextVersion } from '../models/libraryAPI';
|
2025-02-10 01:32:16 +03:00
|
|
|
|
2025-01-16 16:31:03 +03:00
|
|
|
export interface DlgCreateVersionProps {
|
2025-02-12 15:12:59 +03:00
|
|
|
itemID: number;
|
2024-06-07 20:17:03 +03:00
|
|
|
versions: IVersionInfo[];
|
2025-02-12 15:12:59 +03:00
|
|
|
onCreate: (newVersion: number) => void;
|
|
|
|
selected: number[];
|
2024-08-24 12:29:09 +03:00
|
|
|
totalCount: number;
|
2024-06-07 20:17:03 +03:00
|
|
|
}
|
|
|
|
|
2025-01-16 16:31:03 +03:00
|
|
|
function DlgCreateVersion() {
|
2025-02-12 15:12:59 +03:00
|
|
|
const { itemID, versions, selected, totalCount, onCreate } = useDialogsStore(
|
|
|
|
state => state.props as DlgCreateVersionProps
|
|
|
|
);
|
2025-02-06 14:09:20 +03:00
|
|
|
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}
|
2025-02-12 01:34:35 +03:00
|
|
|
submitInvalidTooltip={errorMsg.versionTaken}
|
2025-02-06 20:27:56 +03:00
|
|
|
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;
|