2024-03-08 18:39:08 +03:00
|
|
|
'use client';
|
|
|
|
|
2025-02-12 21:36:25 +03:00
|
|
|
import { Controller, useForm, useWatch } from 'react-hook-form';
|
2025-02-06 14:10:18 +03:00
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
2024-03-08 18:39:08 +03:00
|
|
|
|
2025-03-12 12:04:50 +03:00
|
|
|
import { Checkbox, TextArea, TextInput } from '@/components/input';
|
|
|
|
import { ModalForm } from '@/components/modal';
|
2025-01-16 16:31:32 +03:00
|
|
|
import { useDialogsStore } from '@/stores/dialogs';
|
2025-02-12 01:35:41 +03:00
|
|
|
import { errorMsg } from '@/utils/labels';
|
2024-03-08 18:39:08 +03:00
|
|
|
|
2025-04-20 16:21:06 +03:00
|
|
|
import { type ICreateVersionDTO, type IVersionInfo, schemaCreateVersion } from '../backend/types';
|
|
|
|
import { useCreateVersion } from '../backend/use-create-version';
|
2025-03-12 11:55:43 +03:00
|
|
|
import { nextVersion } from '../models/library-api';
|
2025-02-10 01:32:55 +03:00
|
|
|
|
2025-01-16 16:31:32 +03:00
|
|
|
export interface DlgCreateVersionProps {
|
2025-02-12 15:13:37 +03:00
|
|
|
itemID: number;
|
2024-03-08 18:39:08 +03:00
|
|
|
versions: IVersionInfo[];
|
2025-02-12 15:13:37 +03:00
|
|
|
onCreate: (newVersion: number) => void;
|
|
|
|
selected: number[];
|
2024-08-24 12:29:38 +03:00
|
|
|
totalCount: number;
|
2024-03-08 18:39:08 +03:00
|
|
|
}
|
|
|
|
|
2025-02-19 23:30:35 +03:00
|
|
|
export function DlgCreateVersion() {
|
2025-02-12 15:13:37 +03:00
|
|
|
const { itemID, versions, selected, totalCount, onCreate } = useDialogsStore(
|
|
|
|
state => state.props as DlgCreateVersionProps
|
|
|
|
);
|
2025-04-20 16:21:06 +03:00
|
|
|
const { createVersion: versionCreate } = useCreateVersion();
|
2024-03-08 18:39:08 +03:00
|
|
|
|
2025-07-17 19:04:51 +03:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
control,
|
|
|
|
formState: { errors }
|
|
|
|
} = useForm<ICreateVersionDTO>({
|
2025-04-20 16:21:06 +03:00
|
|
|
resolver: zodResolver(schemaCreateVersion),
|
2025-02-06 14:10:18 +03:00
|
|
|
defaultValues: {
|
2025-02-15 15:37:19 +03:00
|
|
|
version: versions.length > 0 ? nextVersion(versions[versions.length - 1].version) : '1.0.0',
|
2025-02-06 14:10:18 +03:00
|
|
|
description: '',
|
2025-02-19 22:33:09 +03:00
|
|
|
items: []
|
2025-07-17 19:04:51 +03:00
|
|
|
},
|
|
|
|
mode: 'onChange'
|
2025-02-06 14:10:18 +03:00
|
|
|
});
|
|
|
|
const version = useWatch({ control, name: 'version' });
|
2025-07-17 19:04:51 +03:00
|
|
|
const canSubmit = !!version && !versions.find(ver => ver.version === version);
|
2024-03-08 18:39:08 +03:00
|
|
|
|
2025-04-20 16:21:06 +03:00
|
|
|
function onSubmit(data: ICreateVersionDTO) {
|
2025-02-11 20:16:11 +03:00
|
|
|
return versionCreate({ itemID, data }).then(onCreate);
|
2024-03-08 18:39:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2025-02-06 20:28:23 +03:00
|
|
|
<ModalForm
|
|
|
|
header='Создание версии'
|
2025-03-10 16:02:53 +03:00
|
|
|
className='cc-column w-120 py-2 px-6'
|
2025-02-06 20:28:23 +03:00
|
|
|
canSubmit={canSubmit}
|
2025-02-12 01:35:41 +03:00
|
|
|
submitInvalidTooltip={errorMsg.versionTaken}
|
2025-02-06 20:28:23 +03:00
|
|
|
submitText='Создать'
|
|
|
|
onSubmit={event => void handleSubmit(onSubmit)(event)}
|
|
|
|
>
|
2025-07-17 19:04:51 +03:00
|
|
|
<TextInput id='dlg_version' {...register('version')} label='Версия' className='w-64' error={errors.version} />
|
2025-02-06 20:28:23 +03:00
|
|
|
<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}]`}
|
2025-02-19 22:33:09 +03:00
|
|
|
value={field.value.length > 0}
|
|
|
|
onChange={value => field.onChange(value ? selected : [])}
|
2025-02-06 20:28:23 +03:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</ModalForm>
|
2024-03-08 18:39:08 +03:00
|
|
|
);
|
|
|
|
}
|