Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | 1x 1x | import { FC, useState } from 'react'
import cn from 'classnames'
import { Button, Modal } from '@tmk/ui-kit'
import { queryClientAtom, useTranslate } from '@/shared/lib'
import { useAtomValue } from 'jotai'
import { FieldMetadata, viewerAtom } from '@/entities/viewer'
import TrashIcon from '@/shared/assets/icons/common/TrashIcon.svg'
import ArrowIcon from '@/shared/assets/icons/common/arrows.svg'
import {
ACCOUNTING_OBJECTS_COLLECTION_PRIMARY_KEY,
accountingObjectsFields,
DEFAULT_FIELDS_METADATA,
selectedTableFieldsAtom,
} from '@/entities/accounting-object'
import {
useColumnConfigCollection,
useMutateColumnConfig,
USER_TABLE_COLUMN_CONFIG_GET_PRIMARY_KEY,
} from '@/entities/user'
import { useUpdateAtom } from 'jotai/utils'
import { SelectSearch } from '@/shared/ui'
import { currentUserPermissionsAtom, PERMISSION_VIEW_PRICE } from '@/features/rolevik'
import { SelectOption } from '@/shared/@types'
export interface EditFieldsModalProps {
isOpen: boolean
onClose: () => void
onExit: () => void
}
export const EditTableFieldsModal: FC<EditFieldsModalProps> = ({ isOpen, onClose, onExit }) => {
const { t } = useTranslate(['accounting-object', 'common'])
const viewer = useAtomValue(viewerAtom)
const queryClient = useAtomValue(queryClientAtom)
const [fieldsMetadata, setFieldsMetadata] = useState<FieldMetadata[]>([])
const [searchValue, setSearchValue] = useState<string>('')
const setSelectedFields = useUpdateAtom(selectedTableFieldsAtom)
const currentPermissions = useAtomValue(currentUserPermissionsAtom)
const setDefaultFields = () => {
setFieldsMetadata(DEFAULT_FIELDS_METADATA)
setSelectedFields(DEFAULT_FIELDS_METADATA)
}
useColumnConfigCollection(viewer?.id as string, {
enabled: !!viewer?.id,
onSuccess: data => {
if (data?.columnsConfig?.length) {
setFieldsMetadata(data.columnsConfig)
setSelectedFields(data.columnsConfig)
} else {
setDefaultFields()
}
},
onError: () => {
setDefaultFields()
},
})
const { mutate, isLoading } = useMutateColumnConfig({
onSuccess: async () => {
await queryClient.invalidateQueries(USER_TABLE_COLUMN_CONFIG_GET_PRIMARY_KEY)
await queryClient.invalidateQueries(ACCOUNTING_OBJECTS_COLLECTION_PRIMARY_KEY)
onClose?.()
},
})
const getSortedFields = (fields: FieldMetadata[]) => {
return fields.sort((a, b) => a.index - b.index)
}
const changeFieldIndex = (field: FieldMetadata, newIndex: number) => {
const newFields = fieldsMetadata.map(f => {
if (f.field === field.field) {
return {
...f,
index: newIndex,
}
}
if (f.index === newIndex) {
return {
...f,
index: field.index,
}
}
return f
})
setFieldsMetadata(newFields)
}
const getTranslate = (key: string) => {
if (key === 'connection') return t('Thread')
if (key === 'batch') return t('Lot')
return t(key)
}
const getOptions = (alreadySelectedFields: FieldMetadata[]) => {
const options = accountingObjectsFields
.map(key => {
if (key.includes('price')) {
if (currentPermissions?.some(item => item.permissions?.includes(PERMISSION_VIEW_PRICE))) {
return {
id: key,
label: getTranslate(key),
}
}
} else {
return {
id: key,
label: getTranslate(key),
}
}
})
.filter(Boolean) as SelectOption[]
const filteredOptions = options.filter(option => !alreadySelectedFields.find(field => field.field === option?.id))
if (searchValue) {
return filteredOptions.filter(option => option?.label.toLowerCase().includes(searchValue.toLowerCase()))
}
return filteredOptions
}
const saveColumns = () => {
mutate({
id: viewer?.id as string,
data: {
columnsConfig: fieldsMetadata,
},
})
}
return (
<>
<Modal
withCloseButton
maskClosable
type='sidebar'
isOpen={isOpen}
onClose={onExit}
overlayClassName='backdrop-blur-none bg-opacity-30'
>
<div className={cn('w-fit flex flex-col gap-y-2.5 min-w-[400px] max-w-[500px]')}>
<div className='flex flex-col gap-y-2.5 text-center'>
<h1>{t('Edit the table')}</h1>
<span className='text-main-regular'>
{t(
'Arrange the column names in the order in which you want to see them in the table by clicking on the arrows'
)}
</span>
</div>
<div className='flex flex-col gap-y-2.5 w-full'>
<div className='flex flex-col gap-y-2.5 bg-gray-secondary p-5 w-full rounded-base'>
<SelectSearch
name='field'
options={getOptions(fieldsMetadata)}
onChange={value => {
if (value)
setFieldsMetadata(prev => [...prev, { field: value as string, index: fieldsMetadata.length }])
}}
onSearch={setSearchValue}
label={t('common:Add_a_column')}
value={undefined}
/>
<div className='flex flex-col'>
{getSortedFields(fieldsMetadata).map((field, index, arr) => {
return (
<div
key={field.field + field.index}
className={cn(
'flex items-center gap-x-2.5 h-[50px] px-3 py-2 bg-white border border-border border-b-0',
{
'rounded-t-base': index === 0,
'rounded-b-base !border-b': index === arr.length - 1,
'!bg-gray-secondary': field.disabled,
}
)}
>
<div className='flex flex-col'>
<Button
variant='icon'
onClick={() => changeFieldIndex(field, field.index - 1)}
disabled={index === 1 || field.disabled}
>
<ArrowIcon className='stroke-text-secondary rotate-180 hover:stroke-main' />
</Button>
<Button
variant='icon'
onClick={() => changeFieldIndex(field, field.index + 1)}
disabled={index === arr.length - 1 || field.disabled}
>
<ArrowIcon className='stroke-text-secondary hover:stroke-main' />
</Button>
</div>
<span className='text-main-regular'>{getTranslate(field.field)}</span>
<Button
disabled={field.disabled}
variant='icon'
className='ml-auto'
onClick={() => {
setFieldsMetadata(prev => prev.filter(f => f.field !== field.field))
}}
>
<TrashIcon
className={cn({
'fill-blue-dark hover:fill-main-hover': !field.disabled,
'fill-gray': field.disabled,
})}
/>
</Button>
</div>
)
})}
</div>
</div>
<div className='flex items-center gap-x-2.5 justify-center mt-2.5'>
<Button variant='outlined' color='secondary' onClick={onExit}>
<h2>{t('common:Cancel')}</h2>
</Button>
<Button onClick={saveColumns} loading={isLoading}>
<h2>{t('common:To_accept')}</h2>
</Button>
</div>
<Button
variant='text'
className='mx-auto mt-2.5'
onClick={() => setFieldsMetadata(DEFAULT_FIELDS_METADATA)}
>
<span className='text-main-semibold text-blue-dark'>{t('common:Reset_to_basic_settings')}</span>
</Button>
</div>
</div>
</Modal>
</>
)
}
|