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';
|
2022-11-24 09:41:03 +01:00
|
|
|
import { Button, Fab, Stack } from '@mui/material';
|
2022-03-04 01:25:10 +00:00
|
|
|
import { Box } from '@mui/system';
|
2022-11-24 09:41:03 +01:00
|
|
|
import OptionsPanel from 'components/molecules/OptionsPanel';
|
|
|
|
|
import React from 'react';
|
2023-02-05 16:15:20 +01:00
|
|
|
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-02-05 16:15:20 +01:00
|
|
|
import GroupFilter from 'components/source/filters/GroupFilter';
|
|
|
|
|
import SeperatorFilter from 'components/source/filters/SeparatorFilter';
|
2023-02-24 16:40:37 +01:00
|
|
|
import { ISourceFilters, IState } from 'typings';
|
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;
|
|
|
|
|
setSearch: 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-02-06 10:06:33 +01:00
|
|
|
state={checkif === 'false' || (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-02-06 10:06:33 +01:00
|
|
|
state={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-02-06 10:06:33 +01: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-02-06 10:06:33 +01: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-02-06 10:06:33 +01:00
|
|
|
state={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,
|
|
|
|
|
setSearch,
|
2022-03-04 21:48:33 +00:00
|
|
|
update,
|
2022-03-04 01:25:10 +00:00
|
|
|
}: IFilters1) {
|
|
|
|
|
const [FilterOptions, setFilterOptions] = React.useState(false);
|
|
|
|
|
|
|
|
|
|
function handleReset() {
|
|
|
|
|
resetFilterValue(0);
|
|
|
|
|
setFilterOptions(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleSubmit() {
|
|
|
|
|
setTriggerUpdate(0);
|
|
|
|
|
setSearch(true);
|
|
|
|
|
setFilterOptions(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Fab
|
|
|
|
|
sx={{ position: 'fixed', bottom: '2em', right: '3em' }}
|
|
|
|
|
onClick={() => setFilterOptions(!FilterOptions)}
|
|
|
|
|
variant="extended"
|
|
|
|
|
color="primary"
|
|
|
|
|
>
|
|
|
|
|
<FilterListIcon />
|
|
|
|
|
Filter
|
|
|
|
|
</Fab>
|
|
|
|
|
|
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-02-06 10:06:33 +01:00
|
|
|
<Button onClick={handleReset}>Reset</Button>
|
|
|
|
|
<Button sx={{ marginLeft: 'auto' }} variant="contained" onClick={handleSubmit}>
|
2022-03-04 01:25:10 +00:00
|
|
|
Submit
|
|
|
|
|
</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
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|