|
| 1 | +import { |
| 2 | + Box, |
| 3 | + Button, |
| 4 | + Checkbox, |
| 5 | + Container, |
| 6 | + Fade, |
| 7 | + Link, |
| 8 | + ListItem, |
| 9 | + ListItemButton, |
| 10 | + ListItemText, |
| 11 | + Modal, |
| 12 | + Paper, |
| 13 | + Stack, |
| 14 | + SxProps, |
| 15 | + Theme, |
| 16 | + Typography, |
| 17 | +} from '@mui/material' |
| 18 | +import { useEffect, useState } from 'react' |
| 19 | +import { UseFieldArrayReturn } from 'react-hook-form' |
| 20 | +import { useTranslation } from 'react-i18next' |
| 21 | +import { Redirect } from 'react-router-dom' |
| 22 | + |
| 23 | +import { NoData } from '@core/components/NoData/NoData.component' |
| 24 | +import { PaginationSynced } from '@core/components/Pagination/PaginationSynced.component' |
| 25 | +import { Scrollbar } from '@core/components/Scrollbar/Scrollbar.component' |
| 26 | +import { buildSkeletons } from '@core/utils/gui.utils' |
| 27 | + |
| 28 | +import { useAuth } from '@auth/hooks/useAuth.hook' |
| 29 | + |
| 30 | +import { IVideoForm } from '@videos/types/form.type' |
| 31 | + |
| 32 | +import { Attachment } from '@attachments/models/attachment.model' |
| 33 | +import { AttachmentParams } from '@attachments/models/attachment.params' |
| 34 | +import { useGetUserAttachmentsQuery } from '@attachments/services/attachment.service' |
| 35 | + |
| 36 | +import { AttachmentAvatar } from './AttachmentAvatar.component' |
| 37 | + |
| 38 | +interface Props { |
| 39 | + attachments: UseFieldArrayReturn<IVideoForm, 'attachments'> |
| 40 | + videoId?: string |
| 41 | + isOpen: boolean |
| 42 | + onClose: () => void |
| 43 | + sx?: SxProps<Theme> |
| 44 | +} |
| 45 | +export const AttachmentSelectorModal = ({ |
| 46 | + attachments, |
| 47 | + videoId, |
| 48 | + isOpen, |
| 49 | + onClose, |
| 50 | + sx: sxProps, |
| 51 | +}: Props) => { |
| 52 | + const { user } = useAuth() |
| 53 | + const { t } = useTranslation('attachments') |
| 54 | + |
| 55 | + const [page] = useState(1) |
| 56 | + |
| 57 | + const [filters, setFilters] = useState<AttachmentParams>({ |
| 58 | + page, |
| 59 | + pageSize: 10, |
| 60 | + userId: user!.id, |
| 61 | + }) |
| 62 | + |
| 63 | + const { data, isLoading } = useGetUserAttachmentsQuery(filters) |
| 64 | + |
| 65 | + const { fields, append, remove } = attachments |
| 66 | + |
| 67 | + const handleToggle = (attachment: Attachment) => () => { |
| 68 | + const currentIndex = fields.findIndex((e) => e.id === attachment.id) |
| 69 | + if (currentIndex === -1) { |
| 70 | + append(attachment) |
| 71 | + } else { |
| 72 | + remove(currentIndex) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + useEffect(() => { |
| 77 | + if (videoId && data) { |
| 78 | + for (const a of data.items) { |
| 79 | + if (a.videos.includes(videoId)) { |
| 80 | + append(a) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + }, [videoId, data]) |
| 85 | + |
| 86 | + const isAttachmentSelected = (attachment: Attachment) => |
| 87 | + fields.some((e) => e.id === attachment.id) |
| 88 | + |
| 89 | + /* If the user has no attachment, he is redirected to the attachment creation form */ |
| 90 | + if (data && data.totalCount === 0) |
| 91 | + return <Redirect push to="/users/profile/attachments/create" /> |
| 92 | + |
| 93 | + return ( |
| 94 | + <Modal |
| 95 | + sx={{ |
| 96 | + display: 'flex', |
| 97 | + alignItems: 'center', |
| 98 | + justifyContent: 'center', |
| 99 | + padding: 2, |
| 100 | + ...sxProps, |
| 101 | + }} |
| 102 | + open={isOpen} |
| 103 | + onClose={() => onClose()} |
| 104 | + aria-labelledby="element modal" |
| 105 | + closeAfterTransition |
| 106 | + BackdropProps={{ |
| 107 | + timeout: 500, |
| 108 | + }} |
| 109 | + > |
| 110 | + <Fade in={isOpen}> |
| 111 | + <Paper |
| 112 | + sx={{ |
| 113 | + width: { |
| 114 | + lg: '40%', |
| 115 | + md: '50%', |
| 116 | + sm: '70%', |
| 117 | + xs: '90%', |
| 118 | + }, |
| 119 | + bgcolor: 'background.default', |
| 120 | + borderRadius: 2, |
| 121 | + p: { |
| 122 | + sm: 2, |
| 123 | + xs: 1, |
| 124 | + }, |
| 125 | + }} |
| 126 | + variant="outlined" |
| 127 | + > |
| 128 | + <Typography variant="h4" sx={{ mb: '2%' }}> |
| 129 | + {t('forms.selector.title')} |
| 130 | + </Typography> |
| 131 | + <Scrollbar |
| 132 | + sx={{ |
| 133 | + maxHeight: (theme) => `calc(100vh - ${theme.spacing(30)})`, |
| 134 | + minHeight: '300px', |
| 135 | + }} |
| 136 | + > |
| 137 | + <Box sx={{ mt: 2 }}> |
| 138 | + {{ data } |
| 139 | + ? data?.items.map((item) => ( |
| 140 | + <ListItem |
| 141 | + key={item.id} |
| 142 | + secondaryAction={ |
| 143 | + <Checkbox |
| 144 | + edge="end" |
| 145 | + onChange={handleToggle(item)} |
| 146 | + checked={isAttachmentSelected(item)} |
| 147 | + /> |
| 148 | + } |
| 149 | + disablePadding |
| 150 | + > |
| 151 | + <ListItemButton onClick={handleToggle(item)}> |
| 152 | + <AttachmentAvatar url={item.url} /> |
| 153 | + <Link |
| 154 | + href={item.url} |
| 155 | + target="_blank" |
| 156 | + rel="noopener" |
| 157 | + color="inherit" |
| 158 | + underline="hover" |
| 159 | + > |
| 160 | + <ListItemText primary={item.title} /> |
| 161 | + </Link> |
| 162 | + </ListItemButton> |
| 163 | + </ListItem> |
| 164 | + )) |
| 165 | + : buildSkeletons(3)} |
| 166 | + </Box> |
| 167 | + </Scrollbar> |
| 168 | + <Stack spacing={0}> |
| 169 | + {!isLoading && |
| 170 | + (data && |
| 171 | + data.items.length > 0 && |
| 172 | + data.items.length < data.totalCount ? ( |
| 173 | + <Box display="flex" sx={{ mt: 3 }} justifyContent="center"> |
| 174 | + <PaginationSynced |
| 175 | + filters={filters} |
| 176 | + setFilters={setFilters} |
| 177 | + pageCount={Math.ceil(data?.totalCount / filters.pageSize)} |
| 178 | + /> |
| 179 | + </Box> |
| 180 | + ) : ( |
| 181 | + !data || |
| 182 | + (data.items.length === 0 && ( |
| 183 | + <NoData |
| 184 | + variant="attachments" |
| 185 | + link="/users/profile/attachments/create" |
| 186 | + /> |
| 187 | + )) |
| 188 | + ))} |
| 189 | + </Stack> |
| 190 | + <Container |
| 191 | + sx={{ display: 'flex', justifyContent: 'center', my: '1em' }} |
| 192 | + > |
| 193 | + <Button onClick={onClose} variant="contained"> |
| 194 | + {t('forms.selector.validate')} |
| 195 | + </Button> |
| 196 | + </Container> |
| 197 | + </Paper> |
| 198 | + </Fade> |
| 199 | + </Modal> |
| 200 | + ) |
| 201 | +} |
0 commit comments