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

72 lines
2.1 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';
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-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-01-19 21:02:57 +03:30
const { manga } = 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');
fetch(`http://127.0.0.1:4567/api/v1/manga/${manga.id}/library/`).then(() => {
setInLibrary('In Library');
});
}
function removeFromLibrary() {
setInLibrary('removing');
fetch(`http://127.0.0.1:4567/api/v1/manga/${manga.id}/library/`, { method: 'DELETE', mode: 'cors' }).then(() => {
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>
{manga && manga.title}
</h1>
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
);
}