2021-11-29 19:08:02 +03:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
|
import ListItem from '@mui/material/ListItem';
|
|
|
|
|
import ListItemText from '@mui/material/ListItemText';
|
|
|
|
|
import Dialog from '@mui/material/Dialog';
|
|
|
|
|
import DialogTitle from '@mui/material/DialogTitle';
|
|
|
|
|
import DialogContent from '@mui/material/DialogContent';
|
|
|
|
|
import DialogActions from '@mui/material/DialogActions';
|
|
|
|
|
import FormGroup from '@mui/material/FormGroup';
|
|
|
|
|
import Checkbox from '@mui/material/Checkbox';
|
|
|
|
|
import FormControlLabel from '@mui/material/FormControlLabel';
|
|
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
|
import cloneObject from 'util/cloneObject';
|
2023-02-24 16:40:37 +01:00
|
|
|
import { MultiSelectListPreferenceProps } from 'typings';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2021-11-29 19:08:02 +03:30
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
interface IListDialogProps {
|
|
|
|
|
selectedValues: string[];
|
|
|
|
|
open: boolean;
|
|
|
|
|
onClose: (arg0: string[] | null) => void;
|
|
|
|
|
values: string[];
|
|
|
|
|
title: string;
|
2021-11-29 19:08:02 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ListDialog(props: IListDialogProps) {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
const { selectedValues: selectedValuesProp, open, onClose, values, title } = props;
|
2021-11-29 19:08:02 +03:30
|
|
|
const [selectedValues, setSelectedValues] = React.useState(selectedValuesProp);
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (!open) {
|
|
|
|
|
setSelectedValues(selectedValuesProp);
|
|
|
|
|
}
|
|
|
|
|
}, [selectedValuesProp, open]);
|
|
|
|
|
|
|
|
|
|
const handleCancel = () => {
|
|
|
|
|
onClose(null);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleOk = () => {
|
|
|
|
|
onClose(selectedValues);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleChange = (event: React.ChangeEvent<HTMLInputElement>, value: string) => {
|
|
|
|
|
const { checked } = event.target as HTMLInputElement;
|
|
|
|
|
|
|
|
|
|
const hasEntry = selectedValues.some((selectedValue) => value === selectedValue);
|
|
|
|
|
if (checked) {
|
|
|
|
|
if (!hasEntry) {
|
|
|
|
|
const selectedValuesClone = cloneObject(selectedValues) as string[];
|
|
|
|
|
selectedValuesClone.push(value);
|
|
|
|
|
setSelectedValues(selectedValuesClone);
|
|
|
|
|
}
|
2023-02-06 10:06:33 +01:00
|
|
|
} else if (hasEntry) {
|
|
|
|
|
// not checked and has entry
|
2021-11-29 19:08:02 +03:30
|
|
|
const selectedValuesClone = cloneObject(selectedValues) as string[];
|
|
|
|
|
const index = selectedValuesClone.indexOf(value);
|
|
|
|
|
selectedValuesClone.splice(index, 1);
|
|
|
|
|
setSelectedValues(selectedValuesClone);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2023-02-08 18:19:26 +01:00
|
|
|
<Dialog sx={{ '& .MuiDialog-paper': { width: '80%', maxHeight: 435 } }} maxWidth="xs" open={open}>
|
2021-12-04 10:23:12 +03:30
|
|
|
<DialogTitle>{title}</DialogTitle>
|
2021-11-29 19:08:02 +03:30
|
|
|
<DialogContent dividers>
|
|
|
|
|
<FormGroup>
|
|
|
|
|
{values.map((value) => (
|
|
|
|
|
<FormControlLabel
|
2023-02-06 10:06:33 +01:00
|
|
|
control={
|
2021-11-29 19:08:02 +03:30
|
|
|
<Checkbox
|
2023-02-08 18:19:26 +01:00
|
|
|
checked={selectedValues.some((selectedValue) => value === selectedValue)}
|
2021-11-29 19:08:02 +03:30
|
|
|
onChange={(e) => handleChange(e, value)}
|
|
|
|
|
color="default"
|
|
|
|
|
/>
|
2023-02-06 10:06:33 +01:00
|
|
|
}
|
2021-11-29 19:08:02 +03:30
|
|
|
label={value}
|
|
|
|
|
key={value}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</FormGroup>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
<DialogActions>
|
|
|
|
|
<Button autoFocus onClick={handleCancel}>
|
2023-03-23 13:59:32 +01:00
|
|
|
{t('global.button.cancel')}
|
2021-11-29 19:08:02 +03:30
|
|
|
</Button>
|
2023-03-23 13:59:32 +01:00
|
|
|
<Button onClick={handleOk}>{t('global.button.ok')}</Button>
|
2021-11-29 19:08:02 +03:30
|
|
|
</DialogActions>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function MultiSelectListPreference(props: MultiSelectListPreferenceProps) {
|
2023-02-06 10:06:33 +01:00
|
|
|
const { title, summary, currentValue, updateValue, entryValues, entries } = props;
|
2021-11-29 19:08:02 +03:30
|
|
|
const [internalCurrentValue, setInternalCurrentValue] = useState<string[]>(currentValue);
|
|
|
|
|
const [dialogOpen, setDialogOpen] = useState<boolean>(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setInternalCurrentValue(currentValue);
|
|
|
|
|
}, [currentValue]);
|
|
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
const findEntriesOf = (values: string[]) =>
|
|
|
|
|
values.map((value) => {
|
|
|
|
|
const idx = entryValues.indexOf(value);
|
|
|
|
|
return entries[idx];
|
|
|
|
|
});
|
2021-11-29 19:08:02 +03:30
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
const findEntryValuesOf = (values: string[]) =>
|
|
|
|
|
values.map((value) => {
|
|
|
|
|
const idx = entries.indexOf(value);
|
|
|
|
|
return entryValues[idx];
|
|
|
|
|
});
|
2021-11-29 19:08:02 +03:30
|
|
|
|
|
|
|
|
const getSummary = () => summary;
|
|
|
|
|
|
|
|
|
|
const handleDialogClose = (newValue: string[] | null) => {
|
|
|
|
|
if (newValue !== null) {
|
|
|
|
|
// console.log(newValue);
|
|
|
|
|
updateValue(findEntryValuesOf(newValue));
|
|
|
|
|
|
|
|
|
|
// appear smooth
|
|
|
|
|
setInternalCurrentValue(newValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDialogOpen(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2023-02-06 10:06:33 +01:00
|
|
|
<ListItem button onClick={() => setDialogOpen(true)}>
|
2021-11-29 19:08:02 +03:30
|
|
|
<ListItemText primary={title} secondary={getSummary()} />
|
|
|
|
|
</ListItem>
|
|
|
|
|
<ListDialog
|
2021-12-04 10:23:12 +03:30
|
|
|
title={title}
|
2021-11-29 19:08:02 +03:30
|
|
|
open={dialogOpen}
|
|
|
|
|
onClose={handleDialogClose}
|
|
|
|
|
selectedValues={findEntriesOf(internalCurrentValue)}
|
|
|
|
|
values={entries}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|