2022-03-04 01:25:10 +00:00
|
|
|
/*
|
|
|
|
|
* 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 FilterListIcon from '@mui/icons-material/FilterList';
|
2023-06-04 21:08:39 +02:00
|
|
|
import { Button, Stack, Box } from '@mui/material';
|
2023-08-14 21:43:06 +02:00
|
|
|
import { useState } from 'react';
|
2023-06-05 01:42:39 +02:00
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
import { ISourceFilters, IState } from '@/typings';
|
|
|
|
|
import OptionsPanel from '@/components/molecules/OptionsPanel';
|
|
|
|
|
import CheckBoxFilter from '@/components/source/filters/CheckBoxFilter';
|
|
|
|
|
import HeaderFilter from '@/components/source/filters/HeaderFilter';
|
|
|
|
|
import SelectFilter from '@/components/source/filters/SelectFilter';
|
|
|
|
|
import SortFilter from '@/components/source/filters/SortFilter';
|
|
|
|
|
import TextFilter from '@/components/source/filters/TextFilter';
|
|
|
|
|
import TriStateFilter from '@/components/source/filters/TriStateFilter';
|
2022-03-04 01:25:10 +00:00
|
|
|
// this can only cycle once, so should be fine
|
|
|
|
|
// eslint-disable-next-line import/no-cycle
|
2023-06-05 01:42:39 +02:00
|
|
|
import GroupFilter from '@/components/source/filters/GroupFilter';
|
|
|
|
|
import SeperatorFilter from '@/components/source/filters/SeparatorFilter';
|
|
|
|
|
import StyledFab from '@/components/util/StyledFab';
|
2022-03-04 01:25:10 +00:00
|
|
|
|
|
|
|
|
interface IFilters {
|
2023-02-06 10:06:33 +01:00
|
|
|
sourceFilter: ISourceFilters[];
|
|
|
|
|
updateFilterValue: Function;
|
|
|
|
|
group: number | undefined;
|
|
|
|
|
update: any;
|
2022-03-04 01:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IFilters1 {
|
2023-02-06 10:06:33 +01:00
|
|
|
sourceFilter: ISourceFilters[];
|
|
|
|
|
updateFilterValue: Function;
|
|
|
|
|
resetFilterValue: Function;
|
|
|
|
|
setTriggerUpdate: Function;
|
|
|
|
|
update: any;
|
2022-03-04 01:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
export function Options({ sourceFilter, group, updateFilterValue, update }: IFilters) {
|
2022-03-04 01:25:10 +00:00
|
|
|
return (
|
2022-11-24 09:41:03 +01:00
|
|
|
<Stack key={`filters ${group}`}>
|
2023-02-06 10:06:33 +01:00
|
|
|
{sourceFilter.map((e: ISourceFilters, index) => {
|
|
|
|
|
let checkif = update.find(
|
|
|
|
|
(el: { group: number | undefined; position: number }) =>
|
|
|
|
|
el.group === group && el.position === index,
|
|
|
|
|
);
|
2022-03-04 21:48:33 +00:00
|
|
|
checkif = checkif ? checkif.state : checkif;
|
2022-03-04 01:25:10 +00:00
|
|
|
switch (e.type) {
|
|
|
|
|
case 'CheckBox':
|
|
|
|
|
return (
|
|
|
|
|
<CheckBoxFilter
|
|
|
|
|
key={`filters ${e.filter.name}`}
|
|
|
|
|
name={e.filter.name}
|
2023-05-28 02:18:37 +02:00
|
|
|
state={checkif != null ? checkif === 'true' : (e.filter.state as boolean)}
|
2022-03-04 01:25:10 +00:00
|
|
|
position={index}
|
|
|
|
|
group={group}
|
|
|
|
|
updateFilterValue={updateFilterValue}
|
2022-03-04 21:48:33 +00:00
|
|
|
update={update}
|
2022-03-04 01:25:10 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
case 'Group':
|
|
|
|
|
return (
|
|
|
|
|
<GroupFilter
|
|
|
|
|
key={`filters ${e.filter.name}`}
|
|
|
|
|
name={e.filter.name}
|
|
|
|
|
state={e.filter.state as ISourceFilters[]}
|
|
|
|
|
position={index}
|
|
|
|
|
updateFilterValue={updateFilterValue}
|
2022-03-04 21:48:33 +00:00
|
|
|
update={update}
|
2022-03-04 01:25:10 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
case 'Header':
|
2023-02-08 18:19:26 +01:00
|
|
|
return <HeaderFilter key={`filters ${e.filter.name}`} name={e.filter.name} />;
|
2022-03-04 01:25:10 +00:00
|
|
|
case 'Select':
|
|
|
|
|
return (
|
|
|
|
|
<SelectFilter
|
|
|
|
|
key={`filters ${e.filter.name}`}
|
|
|
|
|
name={e.filter.name}
|
2022-06-24 21:06:12 +01:00
|
|
|
values={e.filter.displayValues}
|
2023-05-28 02:18:37 +02:00
|
|
|
state={checkif != null ? parseInt(checkif, 10) : (e.filter.state as number)}
|
2022-03-04 01:25:10 +00:00
|
|
|
selected={e.filter.selected}
|
|
|
|
|
position={index}
|
|
|
|
|
group={group}
|
|
|
|
|
updateFilterValue={updateFilterValue}
|
2022-03-04 21:48:33 +00:00
|
|
|
update={update}
|
2022-03-04 01:25:10 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
case 'Separator':
|
2023-02-08 18:19:26 +01:00
|
|
|
return <SeperatorFilter key={`filters ${e.filter.name}`} name={e.filter.name} />;
|
2022-03-04 01:25:10 +00:00
|
|
|
case 'Sort':
|
|
|
|
|
return (
|
|
|
|
|
<SortFilter
|
|
|
|
|
key={`filters ${e.filter.name}`}
|
|
|
|
|
name={e.filter.name}
|
|
|
|
|
values={e.filter.values}
|
2023-05-28 02:18:37 +02:00
|
|
|
state={checkif ? JSON.parse(checkif) : { ...(e.filter.state as IState) }}
|
2022-03-04 01:25:10 +00:00
|
|
|
position={index}
|
|
|
|
|
group={group}
|
|
|
|
|
updateFilterValue={updateFilterValue}
|
2022-03-04 21:48:33 +00:00
|
|
|
update={update}
|
2022-03-04 01:25:10 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
case 'Text':
|
|
|
|
|
return (
|
|
|
|
|
<TextFilter
|
|
|
|
|
key={`filters ${e.filter.name}`}
|
|
|
|
|
name={e.filter.name}
|
2023-05-28 02:18:37 +02:00
|
|
|
state={checkif ?? (e.filter.state as string)}
|
2022-03-04 01:25:10 +00:00
|
|
|
position={index}
|
|
|
|
|
group={group}
|
|
|
|
|
updateFilterValue={updateFilterValue}
|
2022-03-04 21:48:33 +00:00
|
|
|
update={update}
|
2022-03-04 01:25:10 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
case 'TriState':
|
|
|
|
|
return (
|
|
|
|
|
<TriStateFilter
|
|
|
|
|
key={`filters ${e.filter.name}`}
|
|
|
|
|
name={e.filter.name}
|
2023-05-28 02:18:37 +02:00
|
|
|
state={checkif != null ? parseInt(checkif, 10) : (e.filter.state as number)}
|
2022-03-04 01:25:10 +00:00
|
|
|
position={index}
|
|
|
|
|
group={group}
|
|
|
|
|
updateFilterValue={updateFilterValue}
|
2022-03-04 21:48:33 +00:00
|
|
|
update={update}
|
2022-03-04 01:25:10 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
default:
|
2023-02-06 10:06:33 +01:00
|
|
|
return <Box key={`${e.filter.name}null`} />;
|
2022-03-04 01:25:10 +00:00
|
|
|
}
|
|
|
|
|
})}
|
2022-11-24 09:41:03 +01:00
|
|
|
</Stack>
|
2022-03-04 01:25:10 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function SourceOptions({
|
|
|
|
|
sourceFilter,
|
|
|
|
|
updateFilterValue,
|
|
|
|
|
resetFilterValue,
|
|
|
|
|
setTriggerUpdate,
|
2022-03-04 21:48:33 +00:00
|
|
|
update,
|
2022-03-04 01:25:10 +00:00
|
|
|
}: IFilters1) {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2023-08-14 21:43:06 +02:00
|
|
|
const [FilterOptions, setFilterOptions] = useState(false);
|
2022-03-04 01:25:10 +00:00
|
|
|
|
|
|
|
|
function handleReset() {
|
|
|
|
|
resetFilterValue(0);
|
|
|
|
|
setFilterOptions(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleSubmit() {
|
|
|
|
|
setTriggerUpdate(0);
|
|
|
|
|
setFilterOptions(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2023-05-15 18:23:26 +02:00
|
|
|
<StyledFab onClick={() => setFilterOptions(!FilterOptions)} variant="extended" color="primary">
|
2022-03-04 01:25:10 +00:00
|
|
|
<FilterListIcon />
|
2023-03-23 13:59:32 +01:00
|
|
|
{t('global.button.filter')}
|
2023-05-15 18:23:26 +02:00
|
|
|
</StyledFab>
|
2022-03-04 01:25:10 +00:00
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
<OptionsPanel open={FilterOptions} onClose={() => setFilterOptions(false)}>
|
2022-11-24 09:41:03 +01:00
|
|
|
<Box sx={{ display: 'flex', p: 2, pb: 0 }}>
|
2023-03-23 13:59:32 +01:00
|
|
|
<Button onClick={handleReset}>{t('global.button.reset')}</Button>
|
2023-02-06 10:06:33 +01:00
|
|
|
<Button sx={{ marginLeft: 'auto' }} variant="contained" onClick={handleSubmit}>
|
2023-03-23 13:59:32 +01:00
|
|
|
{t('global.button.submit')}
|
2022-03-04 01:25:10 +00:00
|
|
|
</Button>
|
|
|
|
|
</Box>
|
2022-11-24 09:41:03 +01:00
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
pb: 2,
|
|
|
|
|
mx: 2,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Options
|
|
|
|
|
sourceFilter={sourceFilter}
|
|
|
|
|
updateFilterValue={updateFilterValue}
|
|
|
|
|
group={undefined}
|
|
|
|
|
update={update}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
</OptionsPanel>
|
2022-03-04 01:25:10 +00:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|