-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddAlbumPage.tsx
More file actions
98 lines (87 loc) · 2.35 KB
/
AddAlbumPage.tsx
File metadata and controls
98 lines (87 loc) · 2.35 KB
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
import ArrowBackIosNewIcon from '@mui/icons-material/ArrowBackIosNew';
import DeleteIcon from '@mui/icons-material/Delete';
import { Button, IconButton, Tooltip } from '@mui/material';
import { deleteAlbum } from 'api/albumApi';
import { CreateAlbum } from 'components/Albums/CreateAlbum';
import { ImageGallery } from 'components/ImageGallery';
import UploadImage from 'components/UploadImage';
import toast from 'react-hot-toast';
import { Link as RouterLink, useNavigate, useParams } from 'react-router-dom';
import MainLayout from '../../layout/MainLayout';
const AddAlbumPage = () => {
const { albumId } = useParams<{ albumId?: string }>();
const hasAlbumId = Boolean(albumId);
const navigate = useNavigate();
const delAlbumMutation = deleteAlbum();
const handleDelete = () => {
if (!albumId) return;
if (!confirm('Are you sure you want to delete this album?')) return;
delAlbumMutation.mutate(albumId, {
onSuccess: () => {
toast.success('Album deleted.');
navigate('/albums');
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onError: (err: any) => {
const errors = err?.response?.data?.errors;
const albumNotEmptyMessage: string | undefined =
errors?.AlbumNotEmpty?.[0];
const firstModelError: string | undefined = errors
? Object.values(errors).flat()[0] as string
: undefined;
const apiMessage =
albumNotEmptyMessage ||
firstModelError ||
err?.message ||
'Unexpected error occurred';
toast.error(`Error deleting album: ${apiMessage}`);
},
});
};
const titleNode = (
<IconButton
component={RouterLink}
to="/albums"
aria-label="Back to albums"
size="small"
sx={{
borderRadius: 1,
p: 0,
'& svg': { fontSize: 28 },
}}
>
<ArrowBackIosNewIcon />
</IconButton>
);
return (
<MainLayout
title={titleNode}
actions={
hasAlbumId ? (
<Tooltip title="Delete album">
<span>
<Button
variant="outlined"
color="error"
startIcon={<DeleteIcon />}
onClick={handleDelete}
disabled={delAlbumMutation.isPending}
>
Delete Album
</Button>
</span>
</Tooltip>
) : undefined
}
>
<CreateAlbum />
{hasAlbumId && (
<>
<UploadImage />
<ImageGallery albumId={albumId} />
</>
)}
</MainLayout>
);
};
export default AddAlbumPage;