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 | 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2x 2x 2x 2x 2x | import { FC, useCallback, useState } from 'react'
import { Checkbox } from '@/shared/ui'
import { queryClientAtom, useTranslate } from '@/shared/lib'
import { useMutateUserNotification, User } from '@/entities/user'
import { debounce } from 'lodash'
import {
EVENTS,
MANAGER_EVENTS,
MANAGER_OPERATIONS_EVENTS,
MOL_OPERATIONS_EVENTS,
NotificationType,
STATUS_EVENTS,
} from '../../lib'
import { useAtomValue } from 'jotai'
import { currentUserRolesAtom } from '@/features/rolevik'
export interface NotificationsEventSettingsProps {
user: User
invalidateKey?: string[]
}
export const NotificationsEventSettings: FC<NotificationsEventSettingsProps> = ({ user, invalidateKey }) => {
const { t } = useTranslate(['common'])
const queryClient = useAtomValue(queryClientAtom)
const [selectedValue, setSelectedValue] = useState<NotificationType[]>(user?.notificationTypes || [])
const { mutate } = useMutateUserNotification({
onSuccess: () => {
invalidateKey && queryClient.invalidateQueries(invalidateKey)
},
})
const currentRoles = useAtomValue(currentUserRolesAtom)
const isViewerManager = currentRoles?.some(role => role.role.includes('role_manager'))
const changeUserInfo = useCallback(
debounce(
(key: NotificationType[], value: boolean, selectedValue: NotificationType[]) =>
mutate(
{
id: user.id,
data: {
notificationTypes: selectedValue,
},
},
{
onError: () => {
setSelectedValue(prev => (!value ? [...prev, ...key] : prev.filter(event => key.includes(event))))
},
}
),
300
),
[]
)
const onChange = (key: NotificationType[], value: boolean) => {
const notificationTypes = value ? [...selectedValue, ...key] : selectedValue.filter(event => !key.includes(event))
changeUserInfo(key, value, notificationTypes)
setSelectedValue(notificationTypes)
}
return (
<div className='flex flex-col gap-y-2.5'>
<div>
<div className='px-5 border-b pt-base pb-extra-small border-b-gray'>
<h4 className='text-text-secondary'>{t('Which_operations_display')}</h4>
</div>
<div className='grid grid-cols-2 gap-2.5 px-5 py-small'>
{Object.entries(isViewerManager ? MANAGER_OPERATIONS_EVENTS : MOL_OPERATIONS_EVENTS).map(([key, value]) => (
<Checkbox
key={key}
name={key}
checked={
Array.isArray(value)
? value.every(event => selectedValue.includes(event))
: selectedValue.find(event => event === value) !== undefined
}
label={t(key)}
onChange={e => onChange(Array.isArray(value) ? value : [value], e.target.checked)}
/>
))}
</div>
</div>
{!isViewerManager && (
<div>
<div className='px-5 border-b pt-base pb-extra-small border-b-gray'>
<h4 className='text-text-secondary'>{t('Which_status_display')}</h4>
</div>
<div className='grid grid-cols-2 gap-2.5 px-5 py-small'>
{Object.entries(STATUS_EVENTS).map(([key, value]) => (
<Checkbox
key={key}
name={key}
checked={
Array.isArray(value)
? value.every(event => selectedValue.includes(event))
: selectedValue.includes(value)
}
label={t(key)}
onChange={e => onChange(Array.isArray(value) ? value : [value], e.target.checked)}
/>
))}
</div>
</div>
)}
<div>
<div className='px-5 border-b pt-base pb-extra-small border-b-gray'>
<h4 className='text-text-secondary'>{t('Which_events_display')}</h4>
</div>
<div className='flex flex-col gap-2.5 px-5 py-small'>
{Object.entries(isViewerManager ? MANAGER_EVENTS : EVENTS).map(([key, value]) => (
<Checkbox
key={key}
name={key}
checked={
Array.isArray(value)
? value.every(event => selectedValue.includes(event))
: selectedValue.includes(value)
}
label={t(key)}
onChange={e => onChange(Array.isArray(value) ? value : [value], e.target.checked)}
/>
))}
</div>
</div>
</div>
)
}
|