-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddToAlbumDialog.tsx
More file actions
132 lines (116 loc) · 4.59 KB
/
AddToAlbumDialog.tsx
File metadata and controls
132 lines (116 loc) · 4.59 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
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
import FolderIcon from '@mui/icons-material/Folder';
import {
Button,
Checkbox,
CircularProgress, Dialog, DialogActions, DialogContent, DialogTitle, List, ListItemButton,
ListItemIcon, ListItemText, Typography
} from '@mui/material';
import { getAlbums } from 'api/albumApi';
import { Album } from 'models/Album';
import * as React from 'react';
type Props = {
open: boolean;
onClose: () => void;
onSelect: (albumId: string[]) => void;
title?: string;
hideSystemAlbums?: boolean;
};
export const AddToAlbumDialog: React.FC<Props> = ({
open,
onClose,
onSelect,
title = 'Choose album(s)',
hideSystemAlbums = false,
}) => {
const { data: albums, isLoading, isError, error } = getAlbums();
const [selectedIds, setSelectedIds] = React.useState<string[]>([]);
React.useEffect(() => {
if (!open) {
setSelectedIds([]);
}
}, [open]);
const visibleAlbums = React.useMemo<Album[]>(() => {
const list = albums ?? [];
return hideSystemAlbums ? list.filter((a) => !a.isSystem) : list;
}, [albums, hideSystemAlbums]);
const handleToggle = (id: string) => {
setSelectedIds((prev) =>
prev.includes(id) ? prev.filter((x) => x !== id) : [...prev, id],
);
};
const handleConfirm = () => {
if (selectedIds.length === 0) return;
onSelect(selectedIds);
onClose();
};
return (
<Dialog
open={open}
onClose={onClose}
fullWidth
maxWidth="xs"
aria-labelledby="select-album-title"
>
<DialogTitle id="select-album-title">{title}</DialogTitle>
<DialogContent dividers>
{isLoading && <CircularProgress size={24} />}
{isError && (
<Typography color="error">
Error loading albums {(error as Error)?.message ?? 'Unknown error'}
</Typography>
)}
{!isLoading && !isError && visibleAlbums.length === 0 && (
<Typography color="text.secondary">There are no albums to pick</Typography>
)}
{!isLoading && !isError && visibleAlbums.length > 0 && (
<List dense disablePadding>
{visibleAlbums.map((album) => {
const checked = selectedIds.includes(album.id);
return (
<ListItemButton
key={album.id}
onClick={() => handleToggle(album.id)}
selected={checked}
sx={{
borderRadius: 1,
mb: 0.5,
'&:hover': { bgcolor: 'action.hover' },
}}
>
<ListItemIcon sx={{ minWidth: 40 }}>
<Checkbox
edge="start"
checked={checked}
tabIndex={-1}
disableRipple
/>
</ListItemIcon>
<ListItemIcon sx={{ minWidth: 36 }}>
<FolderIcon />
</ListItemIcon>
<ListItemText
primary={album.name}
secondary={new Date(album.dateCreated).toLocaleDateString()}
primaryTypographyProps={{ noWrap: true, fontWeight: 600 }}
secondaryTypographyProps={{ noWrap: true }}
/>
</ListItemButton>
);
})}
</List>
)}
</DialogContent>
<DialogActions>
<Button onClick={onClose}>Cancel</Button>
<Button
onClick={handleConfirm}
variant="contained"
disabled={selectedIds.length === 0}
>
Add
</Button>
</DialogActions>
</Dialog>
);
};
export default AddToAlbumDialog;