2024-06-07 20:17:03 +03:00
'use client' ;
2025-02-12 21:36:03 +03:00
import { Controller , useForm } from 'react-hook-form' ;
2025-02-12 01:34:35 +03:00
import { zodResolver } from '@hookform/resolvers/zod' ;
2024-06-07 20:17:03 +03:00
import clsx from 'clsx' ;
2025-02-12 01:34:35 +03:00
import { z } from 'zod' ;
2024-06-07 20:17:03 +03:00
2025-02-12 21:36:03 +03:00
import { useAuthSuspense } from '@/features/auth' ;
2025-02-10 01:32:16 +03:00
import { Label , TextArea } from '@/components/Input' ;
import { ModalForm } from '@/components/Modal' ;
2025-01-16 16:31:03 +03:00
import { useDialogsStore } from '@/stores/dialogs' ;
2024-06-07 20:17:03 +03:00
import { limits } from '@/utils/constants' ;
2025-02-12 01:34:35 +03:00
import { errorMsg } from '@/utils/labels' ;
2024-06-07 20:17:03 +03:00
2025-02-12 15:12:59 +03:00
import { SelectLocationContext } from '../components/SelectLocationContext' ;
import { SelectLocationHead } from '../components/SelectLocationHead' ;
2025-02-10 01:32:16 +03:00
import { LocationHead } from '../models/library' ;
import { combineLocation , validateLocation } from '../models/libraryAPI' ;
2025-03-02 19:07:12 +03:00
const schemaLocation = z . strictObject ( {
2025-02-12 01:34:35 +03:00
location : z.string ( ) . refine ( data = > validateLocation ( data ) , { message : errorMsg.invalidLocation } )
} ) ;
type ILocationType = z . infer < typeof schemaLocation > ;
2025-01-16 16:31:03 +03:00
export interface DlgChangeLocationProps {
2024-06-07 20:17:03 +03:00
initial : string ;
onChangeLocation : ( newLocation : string ) = > void ;
}
2025-02-19 23:29:45 +03:00
export function DlgChangeLocation() {
2025-01-16 16:31:03 +03:00
const { initial , onChangeLocation } = useDialogsStore ( state = > state . props as DlgChangeLocationProps ) ;
2025-01-29 16:17:50 +03:00
const { user } = useAuthSuspense ( ) ;
2024-06-07 20:17:03 +03:00
2025-02-12 01:34:35 +03:00
const {
handleSubmit ,
control ,
formState : { errors , isValid , isDirty }
} = useForm < ILocationType > ( {
resolver : zodResolver ( schemaLocation ) ,
defaultValues : {
location : initial
} ,
mode : 'onChange'
} ) ;
2024-06-26 18:59:49 +03:00
2025-02-12 01:34:35 +03:00
function onSubmit ( data : ILocationType ) {
onChangeLocation ( data . location ) ;
2025-02-06 14:09:20 +03:00
}
2024-06-07 20:17:03 +03:00
return (
2025-02-06 20:27:56 +03:00
< ModalForm
2024-06-07 20:17:03 +03:00
overflowVisible
header = 'Изменение расположения'
submitText = 'Переместить'
submitInvalidTooltip = { ` Допустимы буквы, цифры, подчерк, пробел и "!". Сегмент пути не может начинаться и заканчиваться пробелом. Общая длина (с корнем) не должна превышать ${ limits . location_len } ` }
2025-02-12 01:34:35 +03:00
canSubmit = { isValid && isDirty }
onSubmit = { event = > void handleSubmit ( onSubmit ) ( event ) }
2024-09-19 20:52:17 +03:00
className = { clsx ( 'w-[35rem]' , 'pb-3 px-6 flex gap-3 h-[9rem]' ) }
2024-06-07 20:17:03 +03:00
>
2024-08-21 16:49:04 +03:00
< div className = 'flex flex-col gap-2 min-w-[7rem] h-min' >
2024-06-26 18:59:49 +03:00
< Label className = 'select-none' text = 'Корень' / >
2025-02-12 01:34:35 +03:00
< Controller
control = { control }
name = 'location'
render = { ( { field } ) = > (
< SelectLocationHead
value = { field . value . substring ( 0 , 2 ) as LocationHead }
onChange = { newValue = > field . onChange ( combineLocation ( newValue , field . value . substring ( 3 ) ) ) }
excluded = { ! user . is_staff ? [ LocationHead . LIBRARY ] : [ ] }
/ >
) }
2024-06-07 20:17:03 +03:00
/ >
< / div >
2025-02-12 01:34:35 +03:00
< Controller
control = { control }
name = 'location'
render = { ( { field } ) = > (
< SelectLocationContext className = 'max-h-[9.2rem]' value = { field . value } onChange = { field . onChange } / >
) }
/ >
< Controller
control = { control } //
name = 'location'
render = { ( { field } ) = > (
< TextArea
id = 'dlg_location'
label = 'Путь'
rows = { 3 }
value = { field . value . substring ( 3 ) }
onChange = { event = > field . onChange ( combineLocation ( field . value . substring ( 0 , 2 ) , event . target . value ) ) }
error = { errors . location }
/ >
) }
/ >
2025-02-06 20:27:56 +03:00
< / ModalForm >
2024-06-07 20:17:03 +03:00
) ;
}