Files
suwayomi-material-you-webui/webUI/react/src/components/MangaDetails.tsx

84 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-01-26 23:32:12 +03:30
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
2021-02-21 04:27:41 +03:30
import { Button, createStyles, makeStyles } from '@material-ui/core';
2021-02-13 21:12:18 +03:30
import React, { useState } from 'react';
import client from '../util/client';
2021-02-21 04:27:41 +03:30
import CategorySelect from './CategorySelect';
const useStyles = makeStyles(() => createStyles({
root: {
display: 'flex',
flexDirection: 'row-reverse',
'& button': {
marginLeft: 10,
},
},
}));
2021-01-19 20:20:28 +03:30
interface IProps{
2021-02-13 21:12:18 +03:30
manga: IManga
2021-03-14 23:57:33 +03:30
source: ISource
}
function getSourceName(source: ISource) {
if (source.name !== null) { return source.name; }
return source.id;
2021-01-19 20:20:28 +03:30
}
export default function MangaDetails(props: IProps) {
2021-02-21 04:27:41 +03:30
const classes = useStyles();
2021-03-14 23:57:33 +03:30
const { manga, source } = props;
2021-02-13 21:12:18 +03:30
const [inLibrary, setInLibrary] = useState<string>(
manga.inLibrary ? 'In Library' : 'Not In Library',
);
2021-02-21 04:41:56 +03:30
const [categoryDialogOpen, setCategoryDialogOpen] = useState<boolean>(false);
2021-02-13 21:12:18 +03:30
function addToLibrary() {
setInLibrary('adding');
client.get(`/api/v1/manga/${manga.id}/library/`).then(() => {
2021-02-13 21:12:18 +03:30
setInLibrary('In Library');
});
}
function removeFromLibrary() {
setInLibrary('removing');
client.delete(`/api/v1/manga/${manga.id}/library/`).then(() => {
2021-02-13 21:12:18 +03:30
setInLibrary('Not In Library');
});
}
function handleButtonClick() {
if (inLibrary === 'Not In Library') {
addToLibrary();
} else {
removeFromLibrary();
}
}
2021-01-19 20:20:28 +03:30
return (
2021-02-21 04:27:41 +03:30
<div>
2021-01-19 20:20:28 +03:30
<h1>
2021-03-14 23:57:33 +03:30
{manga.title}
2021-01-19 20:20:28 +03:30
</h1>
2021-03-14 23:57:33 +03:30
<h3>
Source:
{' '}
{getSourceName(source)}
</h3>
2021-02-21 04:27:41 +03:30
<div className={classes.root}>
2021-02-13 21:12:18 +03:30
<Button variant="outlined" onClick={() => handleButtonClick()}>{inLibrary}</Button>
2021-02-21 04:27:41 +03:30
{inLibrary === 'In Library'
&& <Button variant="outlined" onClick={() => setCategoryDialogOpen(true)}>Edit Categories</Button>}
2021-02-13 21:12:18 +03:30
</div>
2021-02-21 04:27:41 +03:30
<CategorySelect
open={categoryDialogOpen}
setOpen={setCategoryDialogOpen}
mangaId={manga.id}
/>
</div>
2021-01-19 20:20:28 +03:30
);
}