Files
suwayomi-material-you-webui/src/components/manga/ExtensionLangSelect.tsx

131 lines
4.2 KiB
TypeScript
Raw Normal View History

/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
2021-03-08 21:04:42 +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 } from 'react';
2021-09-09 17:51:22 +04:30
import makeStyles from '@mui/styles/makeStyles';
import createStyles from '@mui/styles/createStyles';
import Button from '@mui/material/Button';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import DialogActions from '@mui/material/DialogActions';
import Dialog from '@mui/material/Dialog';
import Switch from '@mui/material/Switch';
import IconButton from '@mui/material/IconButton';
import FilterListIcon from '@mui/icons-material/FilterList';
import { List, ListItemSecondaryAction, ListItemText } from '@mui/material';
import ListItem from '@mui/material/ListItem';
import { langCodeToName } from 'util/language';
import cloneObject from 'util/cloneObject';
2021-03-08 21:04:42 +03:30
const useStyles = makeStyles(() => createStyles({
paper: {
maxHeight: 435,
width: '80%',
},
}));
2021-10-18 14:22:33 +03:30
function removeAll(firstList: any[], secondList: any[]) {
console.log(secondList);
secondList.forEach((item) => {
const index = firstList.indexOf(item);
if (index !== -1) {
firstList.splice(index, 1);
}
});
return firstList;
}
2021-03-08 21:04:42 +03:30
interface IProps {
shownLangs: string[]
setShownLangs: (arg0: string[]) => void
allLangs: string[]
2021-10-18 14:22:33 +03:30
forcedLangs?: string[]
2021-03-08 21:04:42 +03:30
}
export default function ExtensionLangSelect(props: IProps) {
2021-10-18 14:22:33 +03:30
const {
shownLangs, setShownLangs, allLangs, forcedLangs,
} = props;
2021-03-08 21:04:42 +03:30
// hold a copy and only sate state on parent when OK pressed, improves performance
2021-10-18 14:22:33 +03:30
const [mShownLangs, setMShownLangs] = useState(removeAll(shownLangs, forcedLangs!));
2021-03-08 21:04:42 +03:30
const classes = useStyles();
const [open, setOpen] = useState<boolean>(false);
const handleCancel = () => {
setOpen(false);
};
const handleOk = () => {
setOpen(false);
setShownLangs(mShownLangs);
};
const handleChange = (event: React.ChangeEvent<HTMLInputElement>, lang: string) => {
const { checked } = event.target as HTMLInputElement;
if (checked) {
setMShownLangs([...mShownLangs, lang]);
} else {
const clone = cloneObject(mShownLangs);
2021-03-08 21:04:42 +03:30
clone.splice(clone.indexOf(lang), 1);
setMShownLangs(clone);
}
};
return (
<>
<IconButton
onClick={() => setOpen(true)}
aria-label="display more actions"
edge="end"
color="inherit"
2021-09-09 17:51:22 +04:30
size="large"
2021-03-08 21:04:42 +03:30
>
<FilterListIcon />
</IconButton>
<Dialog
classes={classes}
maxWidth="xs"
open={open}
>
<DialogTitle>Enabled Languages</DialogTitle>
2021-03-09 16:44:09 +03:30
<DialogContent dividers style={{ padding: 0 }}>
<List>
2021-03-08 21:04:42 +03:30
{allLangs.map((lang) => (
2021-03-09 16:44:09 +03:30
<ListItem key={lang}>
<ListItemText primary={langCodeToName(lang)} />
<ListItemSecondaryAction>
<Switch
2021-03-08 21:04:42 +03:30
checked={mShownLangs.indexOf(lang) !== -1}
onChange={(e) => handleChange(e, lang)}
/>
2021-03-09 16:44:09 +03:30
</ListItemSecondaryAction>
</ListItem>
2021-03-08 21:04:42 +03:30
))}
2021-03-09 16:44:09 +03:30
</List>
2021-03-08 21:04:42 +03:30
</DialogContent>
<DialogActions>
<Button autoFocus onClick={handleCancel} color="primary">
Cancel
</Button>
<Button onClick={handleOk} color="primary">
Ok
</Button>
</DialogActions>
</Dialog>
</>
);
}
2021-10-18 14:22:33 +03:30
ExtensionLangSelect.defaultProps = {
forcedLangs: [] as string[],
};