2024-06-02 23:41:46 +03:00
'use client' ;
import clsx from 'clsx' ;
2024-12-13 21:31:09 +03:00
import { useState } from 'react' ;
2024-06-02 23:41:46 +03:00
2025-01-27 15:03:48 +03:00
import { useAuth } from '@/backend/auth/useAuth' ;
2024-06-26 19:00:29 +03:00
import SelectLocationContext from '@/components/select/SelectLocationContext' ;
2024-06-02 23:41:46 +03:00
import SelectLocationHead from '@/components/select/SelectLocationHead' ;
import Label from '@/components/ui/Label' ;
2025-01-16 16:31:32 +03:00
import Modal from '@/components/ui/Modal' ;
2024-06-02 23:41:46 +03:00
import TextArea from '@/components/ui/TextArea' ;
import { LocationHead } from '@/models/library' ;
import { combineLocation , validateLocation } from '@/models/libraryAPI' ;
2025-01-16 16:31:32 +03:00
import { useDialogsStore } from '@/stores/dialogs' ;
2024-06-02 23:41:46 +03:00
import { limits } from '@/utils/constants' ;
2025-01-16 16:31:32 +03:00
export interface DlgChangeLocationProps {
2024-06-02 23:41:46 +03:00
initial : string ;
onChangeLocation : ( newLocation : string ) = > void ;
}
2025-01-16 16:31:32 +03:00
function DlgChangeLocation() {
const { initial , onChangeLocation } = useDialogsStore ( state = > state . props as DlgChangeLocationProps ) ;
2024-06-02 23:41:46 +03:00
const { user } = useAuth ( ) ;
const [ head , setHead ] = useState < LocationHead > ( initial . substring ( 0 , 2 ) as LocationHead ) ;
const [ body , setBody ] = useState < string > ( initial . substring ( 3 ) ) ;
2024-12-13 21:31:09 +03:00
const location = combineLocation ( head , body ) ;
const isValid = initial !== location && validateLocation ( location ) ;
2024-06-02 23:41:46 +03:00
2024-12-13 21:31:09 +03:00
function handleSelectLocation ( newValue : string ) {
2024-06-26 19:00:29 +03:00
setHead ( newValue . substring ( 0 , 2 ) as LocationHead ) ;
setBody ( newValue . length > 3 ? newValue . substring ( 3 ) : '' ) ;
2024-12-13 21:31:09 +03:00
}
2024-06-26 19:00:29 +03:00
2024-06-02 23:41:46 +03:00
return (
< Modal
2024-06-05 14:43:52 +03:00
overflowVisible
2024-06-02 23:41:46 +03:00
header = 'Изменение расположения'
submitText = 'Переместить'
submitInvalidTooltip = { ` Допустимы буквы, цифры, подчерк, пробел и "!". Сегмент пути не может начинаться и заканчиваться пробелом. Общая длина (с корнем) не должна превышать ${ limits . location_len } ` }
canSubmit = { isValid }
2024-08-15 23:23:45 +03:00
onSubmit = { ( ) = > onChangeLocation ( location ) }
2024-09-19 20:52:37 +03:00
className = { clsx ( 'w-[35rem]' , 'pb-3 px-6 flex gap-3 h-[9rem]' ) }
2024-06-02 23:41:46 +03:00
>
2024-08-21 16:49:17 +03:00
< div className = 'flex flex-col gap-2 min-w-[7rem] h-min' >
2024-06-26 19:00:29 +03:00
< Label className = 'select-none' text = 'Корень' / >
2024-06-02 23:41:46 +03:00
< SelectLocationHead
value = { head } // prettier: split-lines
onChange = { setHead }
excluded = { ! user ? . is_staff ? [ LocationHead . LIBRARY ] : [ ] }
/ >
< / div >
2025-01-27 15:03:48 +03:00
< SelectLocationContext value = { location } onChange = { handleSelectLocation } className = 'max-h-[9.2rem]' / >
2024-08-28 22:38:50 +03:00
< TextArea id = 'dlg_cst_body' label = 'Путь' rows = { 3 } value = { body } onChange = { event = > setBody ( event . target . value ) } / >
2024-06-02 23:41:46 +03:00
< / Modal >
) ;
}
export default DlgChangeLocation ;