Files
suwayomi-material-you-webui/src/screens/settings/Categories.tsx

249 lines
9.7 KiB
TypeScript
Raw Normal View History

2021-05-30 04:01:49 +04:30
/* eslint-disable @typescript-eslint/no-shadow */
/* eslint-disable react/destructuring-assignment */
/* eslint-disable react/jsx-props-no-spreading */
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
2021-02-20 01:23:52 +03:30
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import React, { useState, useContext, useEffect } from 'react';
import {
List,
ListItem,
ListItemText,
ListItemIcon,
IconButton,
} from '@material-ui/core';
2021-05-30 04:01:49 +04:30
import {
DragDropContext, Droppable, Draggable, DropResult, DraggingStyle, NotDraggingStyle,
} from 'react-beautiful-dnd';
2021-02-20 01:23:52 +03:30
import DragHandleIcon from '@material-ui/icons/DragHandle';
import EditIcon from '@material-ui/icons/Edit';
import { useTheme } from '@material-ui/core/styles';
import Fab from '@material-ui/core/Fab';
import AddIcon from '@material-ui/icons/Add';
import DeleteIcon from '@material-ui/icons/Delete';
import Button from '@material-ui/core/Button';
import TextField from '@material-ui/core/TextField';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import DialogTitle from '@material-ui/core/DialogTitle';
import Checkbox from '@material-ui/core/Checkbox';
import FormControlLabel from '@material-ui/core/FormControlLabel';
2021-05-30 04:01:49 +04:30
import NavbarContext from 'context/NavbarContext';
import client from 'util/client';
import { Palette } from '@material-ui/core/styles/createPalette';
2021-02-20 01:23:52 +03:30
2021-05-30 04:01:49 +04:30
const getItemStyle = (isDragging: boolean,
draggableStyle: DraggingStyle | NotDraggingStyle | undefined, palette: Palette) => ({
2021-02-20 01:23:52 +03:30
// styles we need to apply on draggables
...draggableStyle,
...(isDragging && {
background: palette.type === 'dark' ? '#424242' : 'rgb(235,235,235)',
}),
});
export default function Categories() {
2021-03-09 16:44:09 +03:30
const { setTitle, setAction } = useContext(NavbarContext);
useEffect(() => { setTitle('Categories'); setAction(<></>); }, []);
2021-05-30 04:01:49 +04:30
const [categories, setCategories] = useState<ICategory[]>([]);
const [categoryToEdit, setCategoryToEdit] = useState<number>(-1); // -1 means new category
const [dialogOpen, setDialogOpen] = useState<boolean>(false);
const [dialogName, setDialogName] = useState<string>('');
const [dialogDefault, setDialogDefault] = useState<boolean>(false);
2021-02-20 01:23:52 +03:30
const theme = useTheme();
2021-05-30 04:01:49 +04:30
const [updateTriggerHolder, setUpdateTriggerHolder] = useState<number>(0); // just a hack
2021-02-20 01:23:52 +03:30
const triggerUpdate = () => setUpdateTriggerHolder(updateTriggerHolder + 1); // just a hack
useEffect(() => {
if (!dialogOpen) {
client.get('/api/v1/category/')
.then((response) => response.data)
2021-08-21 03:54:37 +04:30
.then((data) => { if (data.length > 0 && data[0].name === 'Default') data.shift(); return data; })
2021-02-20 01:23:52 +03:30
.then((data) => setCategories(data));
}
}, [updateTriggerHolder]);
2021-05-30 04:01:49 +04:30
const categoryReorder = (list: ICategory[], from: number, to: number) => {
2021-02-20 01:23:52 +03:30
const formData = new FormData();
2021-05-30 04:01:49 +04:30
formData.append('from', `${from + 1}`);
formData.append('to', `${to + 1}`);
2021-08-29 20:47:57 +04:30
client.patch('/api/v1/category/reorder', formData)
.finally(() => triggerUpdate());
2021-02-20 01:23:52 +03:30
// also move it in local state to avoid jarring moving behviour...
const result = Array.from(list);
const [removed] = result.splice(from, 1);
result.splice(to, 0, removed);
return result;
};
2021-05-30 04:01:49 +04:30
const onDragEnd = (result: DropResult) => {
2021-02-20 01:23:52 +03:30
// dropped outside the list?
if (!result.destination) {
return;
}
setCategories(categoryReorder(
categories,
result.source.index,
result.destination.index,
));
};
const resetDialog = () => {
setDialogName('');
setDialogDefault(false);
2021-02-20 01:23:52 +03:30
setCategoryToEdit(-1);
};
const handleDialogOpen = () => {
2021-02-20 01:23:52 +03:30
resetDialog();
setDialogOpen(true);
};
2021-05-30 04:01:49 +04:30
const handleEditDialogOpen = (index:number) => {
setDialogName(categories[index].name);
setDialogDefault(categories[index].default);
setCategoryToEdit(index);
setDialogOpen(true);
};
const handleDialogCancel = () => {
setDialogOpen(false);
2021-02-20 01:23:52 +03:30
};
const handleDialogSubmit = () => {
setDialogOpen(false);
2021-02-20 01:23:52 +03:30
const formData = new FormData();
formData.append('name', dialogName);
2021-05-30 04:01:49 +04:30
formData.append('default', dialogDefault.toString());
2021-02-20 01:23:52 +03:30
if (categoryToEdit === -1) {
client.post('/api/v1/category/', formData)
.finally(() => triggerUpdate());
2021-02-20 01:23:52 +03:30
} else {
const category = categories[categoryToEdit];
client.patch(`/api/v1/category/${category.id}`, formData)
.finally(() => triggerUpdate());
2021-02-20 01:23:52 +03:30
}
};
2021-05-30 04:01:49 +04:30
const deleteCategory = (index:number) => {
2021-02-20 01:23:52 +03:30
const category = categories[index];
client.delete(`/api/v1/category/${category.id}`)
.finally(() => triggerUpdate());
2021-02-20 01:23:52 +03:30
};
return (
<>
<DragDropContext onDragEnd={onDragEnd}>
<Droppable droppableId="droppable">
{(provided) => (
<List ref={provided.innerRef}>
{categories.map((item, index) => (
<Draggable
key={item.id}
draggableId={item.id.toString()}
index={index}
>
{(provided, snapshot) => (
<ListItem
2021-05-30 04:01:49 +04:30
ContainerProps={{ ref: provided.innerRef } as any}
2021-02-20 01:23:52 +03:30
{...provided.draggableProps}
{...provided.dragHandleProps}
style={getItemStyle(
snapshot.isDragging,
provided.draggableProps.style,
theme.palette,
)}
ref={provided.innerRef}
>
<ListItemIcon>
<DragHandleIcon />
</ListItemIcon>
<ListItemText
primary={item.name}
/>
<IconButton
onClick={() => {
handleEditDialogOpen(index);
2021-02-20 01:23:52 +03:30
}}
>
<EditIcon />
</IconButton>
<IconButton
onClick={() => {
deleteCategory(index);
}}
>
<DeleteIcon />
</IconButton>
</ListItem>
)}
</Draggable>
))}
{provided.placeholder}
</List>
)}
</Droppable>
</DragDropContext>
<Fab
color="primary"
aria-label="add"
style={{
position: 'absolute',
bottom: theme.spacing(2),
right: theme.spacing(2),
}}
onClick={handleDialogOpen}
>
<AddIcon />
</Fab>
<Dialog open={dialogOpen} onClose={handleDialogCancel}>
2021-02-20 01:23:52 +03:30
<DialogTitle id="form-dialog-title">
{categoryToEdit === -1 ? 'New Catalog' : 'Edit Catalog'}
2021-02-20 01:23:52 +03:30
</DialogTitle>
<DialogContent>
<TextField
autoFocus
margin="dense"
id="name"
label="Category Name"
type="text"
fullWidth
value={dialogName}
onChange={(e) => setDialogName(e.target.value)}
/>
<FormControlLabel
control={(
<Checkbox
checked={dialogDefault}
onChange={(e) => setDialogDefault(e.target.checked)}
color="default"
/>
)}
label="Default category when adding new manga to library"
2021-02-20 01:23:52 +03:30
/>
</DialogContent>
<DialogActions>
<Button onClick={handleDialogCancel} color="primary">
Cancel
</Button>
<Button onClick={handleDialogSubmit} color="primary">
Submit
</Button>
</DialogActions>
</Dialog>
</>
);
}