Source filter scroll fix (array of filters on submit) (#149)

* scrolling filters issue fix

scrolling after changing filters and not submitting works properly now

* fixed it

wasn't something i introduced just then, just forgot to fix

* source filters array on submit
This commit is contained in:
robo
2022-03-04 21:48:33 +00:00
committed by GitHub
parent bdd03f6698
commit 68351aa12f
8 changed files with 86 additions and 32 deletions

View File

@@ -27,6 +27,7 @@ interface IFilters {
sourceFilter: ISourceFilters[] sourceFilter: ISourceFilters[]
updateFilterValue: Function updateFilterValue: Function
group: number | undefined group: number | undefined
update: any
} }
interface IFilters1 { interface IFilters1 {
@@ -35,26 +36,33 @@ interface IFilters1 {
resetFilterValue: Function resetFilterValue: Function
setTriggerUpdate: Function setTriggerUpdate: Function
setSearch: Function setSearch: Function
update: any
} }
export function Options({ export function Options({
sourceFilter, sourceFilter,
group, group,
updateFilterValue, updateFilterValue,
update,
}: IFilters) { }: IFilters) {
return ( return (
<Box key={`filters ${group}`}> <Box key={`filters ${group}`}>
{ sourceFilter.map((e: ISourceFilters, index) => { { sourceFilter.map((e: ISourceFilters, index) => {
let checkif = update.find((el: {
group: number | undefined; position: number;
}) => el.group === group && el.position === index);
checkif = checkif ? checkif.state : checkif;
switch (e.type) { switch (e.type) {
case 'CheckBox': case 'CheckBox':
return ( return (
<CheckBoxFilter <CheckBoxFilter
key={`filters ${e.filter.name}`} key={`filters ${e.filter.name}`}
name={e.filter.name} name={e.filter.name}
state={e.filter.state as boolean} state={checkif === 'false' || e.filter.state as boolean}
position={index} position={index}
group={group} group={group}
updateFilterValue={updateFilterValue} updateFilterValue={updateFilterValue}
update={update}
/> />
); );
case 'Group': case 'Group':
@@ -65,6 +73,7 @@ export function Options({
state={e.filter.state as ISourceFilters[]} state={e.filter.state as ISourceFilters[]}
position={index} position={index}
updateFilterValue={updateFilterValue} updateFilterValue={updateFilterValue}
update={update}
/> />
); );
case 'Header': case 'Header':
@@ -80,11 +89,12 @@ export function Options({
key={`filters ${e.filter.name}`} key={`filters ${e.filter.name}`}
name={e.filter.name} name={e.filter.name}
values={e.filter.values} values={e.filter.values}
state={e.filter.state as number} state={parseInt(checkif, 10) || e.filter.state as number}
selected={e.filter.selected} selected={e.filter.selected}
position={index} position={index}
group={group} group={group}
updateFilterValue={updateFilterValue} updateFilterValue={updateFilterValue}
update={update}
/> />
); );
case 'Separator': case 'Separator':
@@ -100,10 +110,11 @@ export function Options({
key={`filters ${e.filter.name}`} key={`filters ${e.filter.name}`}
name={e.filter.name} name={e.filter.name}
values={e.filter.values} values={e.filter.values}
state={e.filter.state as IState} state={checkif ? JSON.parse(checkif) : e.filter.state as IState}
position={index} position={index}
group={group} group={group}
updateFilterValue={updateFilterValue} updateFilterValue={updateFilterValue}
update={update}
/> />
); );
case 'Text': case 'Text':
@@ -111,10 +122,11 @@ export function Options({
<TextFilter <TextFilter
key={`filters ${e.filter.name}`} key={`filters ${e.filter.name}`}
name={e.filter.name} name={e.filter.name}
state={e.filter.state as string} state={checkif || e.filter.state as string}
position={index} position={index}
group={group} group={group}
updateFilterValue={updateFilterValue} updateFilterValue={updateFilterValue}
update={update}
/> />
); );
case 'TriState': case 'TriState':
@@ -122,10 +134,11 @@ export function Options({
<TriStateFilter <TriStateFilter
key={`filters ${e.filter.name}`} key={`filters ${e.filter.name}`}
name={e.filter.name} name={e.filter.name}
state={e.filter.state as number} state={parseInt(checkif, 10) || e.filter.state as number}
position={index} position={index}
group={group} group={group}
updateFilterValue={updateFilterValue} updateFilterValue={updateFilterValue}
update={update}
/> />
); );
default: default:
@@ -142,6 +155,7 @@ export default function SourceOptions({
resetFilterValue, resetFilterValue,
setTriggerUpdate, setTriggerUpdate,
setSearch, setSearch,
update,
}: IFilters1) { }: IFilters1) {
const [FilterOptions, setFilterOptions] = React.useState(false); const [FilterOptions, setFilterOptions] = React.useState(false);
@@ -196,6 +210,7 @@ export default function SourceOptions({
sourceFilter={sourceFilter} sourceFilter={sourceFilter}
updateFilterValue={updateFilterValue} updateFilterValue={updateFilterValue}
group={undefined} group={undefined}
update={update}
/> />
</Drawer> </Drawer>
</> </>

View File

@@ -15,6 +15,7 @@ interface Props {
position: number position: number
group: number | undefined group: number | undefined
updateFilterValue: Function updateFilterValue: Function
update: any
} }
export default function CheckBoxFilter(props: Props) { export default function CheckBoxFilter(props: Props) {
@@ -24,12 +25,16 @@ export default function CheckBoxFilter(props: Props) {
position, position,
group, group,
updateFilterValue, updateFilterValue,
update,
} = props; } = props;
const [val, setval] = React.useState(state); const [val, setval] = React.useState(state);
const handleChange = (event: { target: { name: any; checked: any; }; }) => { const handleChange = (event: { target: { name: any; checked: any; }; }) => {
setval(event.target.checked); setval(event.target.checked);
updateFilterValue({ position, state: event.target.checked.toString(), group }); const upd = update.filter((e: {
position: number; group: number | undefined;
}) => !(position === e.position && group === e.group));
updateFilterValue([...upd, { position, state: event.target.checked.toString(), group }]);
}; };
if (state !== undefined) { if (state !== undefined) {

View File

@@ -18,6 +18,7 @@ interface Props {
name: string name: string
position: number position: number
updateFilterValue: Function updateFilterValue: Function
update: any
} }
export default function GroupFilter(props: Props) { export default function GroupFilter(props: Props) {
@@ -26,6 +27,7 @@ export default function GroupFilter(props: Props) {
name, name,
position, position,
updateFilterValue, updateFilterValue,
update,
} = props; } = props;
const [open, setOpen] = React.useState(false); const [open, setOpen] = React.useState(false);
@@ -46,6 +48,7 @@ export default function GroupFilter(props: Props) {
sourceFilter={state} sourceFilter={state}
group={position} group={position}
updateFilterValue={updateFilterValue} updateFilterValue={updateFilterValue}
update={update}
/> />
</List> </List>
</Collapse> </Collapse>

View File

@@ -21,6 +21,7 @@ interface Props {
position: number position: number
updateFilterValue: Function updateFilterValue: Function
group: number | undefined group: number | undefined
update: any
} }
interface Selected { interface Selected {
@@ -35,6 +36,7 @@ function hasSelect(
state: number, state: number,
position: number, position: number,
updateFilterValue: Function, updateFilterValue: Function,
update: any,
group?: number, group?: number,
) { ) {
const [val, setval] = React.useState(state); const [val, setval] = React.useState(state);
@@ -42,7 +44,10 @@ function hasSelect(
const handleChange = (event: { target: { name: any; value: any; }; }) => { const handleChange = (event: { target: { name: any; value: any; }; }) => {
const vall = values.map((e) => e.displayname).indexOf(`${event.target.value}`); const vall = values.map((e) => e.displayname).indexOf(`${event.target.value}`);
setval(vall); setval(vall);
updateFilterValue({ position, state: vall.toString(), group }); const upd = update.filter((e: {
position: number; group: number | undefined;
}) => !(position === e.position && group === e.group));
updateFilterValue([...upd, { position, state: vall.toString(), group }]);
}; };
const rett = values.map((e: Selected) => ( const rett = values.map((e: Selected) => (
@@ -84,6 +89,7 @@ function noSelect(
state: number, state: number,
position: number, position: number,
updateFilterValue: Function, updateFilterValue: Function,
update: any,
group?: number, group?: number,
) { ) {
const [val, setval] = React.useState(state); const [val, setval] = React.useState(state);
@@ -92,7 +98,10 @@ function noSelect(
const handleChange = (event: { target: { name: any; value: any; }; }) => { const handleChange = (event: { target: { name: any; value: any; }; }) => {
const vall = values.indexOf(`${event.target.value}`); const vall = values.indexOf(`${event.target.value}`);
setval(vall); setval(vall);
updateFilterValue({ position, state: vall.toString(), group }); const upd = update.filter((e: {
position: number; group: number | undefined;
}) => !(position === e.position && group === e.group));
updateFilterValue([...upd, { position, state: vall.toString(), group }]);
}; };
const rett = values.map((value: string) => (<MenuItem key={`${name} ${value}`} value={value}>{value}</MenuItem>)); const rett = values.map((value: string) => (<MenuItem key={`${name} ${value}`} value={value}>{value}</MenuItem>));
@@ -126,6 +135,7 @@ export default function SelectFilter({
selected, selected,
position, position,
updateFilterValue, updateFilterValue,
update,
group, group,
}: Props) { }: Props) {
if (selected === undefined) { if (selected === undefined) {
@@ -135,6 +145,7 @@ export default function SelectFilter({
state, state,
position, position,
updateFilterValue, updateFilterValue,
update,
group, group,
); );
} }
@@ -145,6 +156,7 @@ export default function SelectFilter({
state, state,
position, position,
updateFilterValue, updateFilterValue,
update,
group, group,
); );
} }

View File

@@ -25,6 +25,7 @@ interface Props {
position: number position: number
group: number | undefined group: number | undefined
updateFilterValue: Function updateFilterValue: Function
update: any
} }
export default function SortFilter(props: Props) { export default function SortFilter(props: Props) {
@@ -35,6 +36,7 @@ export default function SortFilter(props: Props) {
position, position,
group, group,
updateFilterValue, updateFilterValue,
update,
} = props; } = props;
const [val, setval] = React.useState(state); const [val, setval] = React.useState(state);
@@ -55,7 +57,10 @@ export default function SortFilter(props: Props) {
} }
tmp.index = index; tmp.index = index;
setval(tmp); setval(tmp);
updateFilterValue({ position, state: JSON.stringify(tmp), group }); const upd = update.filter((e: {
position: number; group: number | undefined;
}) => !(position === e.position && group === e.group));
updateFilterValue([...upd, { position, state: JSON.stringify(tmp), group }]);
}; };
const ret = ( const ret = (

View File

@@ -20,6 +20,7 @@ interface Props {
position: number position: number
group: number | undefined group: number | undefined
updateFilterValue: Function updateFilterValue: Function
update: any
} }
export default function TextFilter(props: Props) { export default function TextFilter(props: Props) {
@@ -29,16 +30,20 @@ export default function TextFilter(props: Props) {
position, position,
group, group,
updateFilterValue, updateFilterValue,
update,
} = props; } = props;
const [Search, setsearch] = React.useState(''); const [Search, setsearch] = React.useState(state || '');
let typingTimer: NodeJS.Timeout; let typingTimer: NodeJS.Timeout;
function doneTyping(e: React.ChangeEvent<HTMLInputElement>) { function doneTyping(e: React.ChangeEvent<HTMLInputElement>) {
updateFilterValue({ position, state: e.target.value === '' ? '' : e.target.value.toString(), group }); const upd = update.filter((el: {
position: number; group: number | undefined;
}) => !(position === el.position && group === el.group));
updateFilterValue([...upd, { position, state: e.target.value, group }]);
} }
function handleChange(e: React.ChangeEvent<HTMLInputElement>) { function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
setsearch(e.target.value === '' ? '' : e.target.value); setsearch(e.target.value);
clearTimeout(typingTimer); clearTimeout(typingTimer);
typingTimer = setTimeout(() => { doneTyping(e); }, 2500); typingTimer = setTimeout(() => { doneTyping(e); }, 2500);

View File

@@ -16,6 +16,7 @@ interface Props {
position: number position: number
group: number | undefined group: number | undefined
updateFilterValue: Function updateFilterValue: Function
update: any
} }
export default function TriStateFilter(props: Props) { export default function TriStateFilter(props: Props) {
@@ -25,6 +26,7 @@ export default function TriStateFilter(props: Props) {
position, position,
group, group,
updateFilterValue, updateFilterValue,
update,
} = props; } = props;
const [val, setval] = React.useState({ const [val, setval] = React.useState({
[name]: state, [name]: state,
@@ -40,11 +42,14 @@ export default function TriStateFilter(props: Props) {
setval({ setval({
...tmp, ...tmp,
}); });
updateFilterValue({ const upd = update.filter((e: {
position: number; group: number | undefined;
}) => !(position === e.position && group === e.group));
updateFilterValue([...upd, {
position, position,
state: (tmp[name] === undefined ? 0 : tmp[name]).toString(), state: (tmp[name] === undefined ? 0 : tmp[name]).toString(),
group, group,
}); }]);
}; };
if (state !== undefined) { if (state !== undefined) {

View File

@@ -36,7 +36,7 @@ export default function SourceMangas(props: { popular: boolean }) {
const [Search, setSearch] = useState<boolean>(); const [Search, setSearch] = useState<boolean>();
const [query, setquery] = useQueryParam('query', StringParam); const [query, setquery] = useQueryParam('query', StringParam);
const [reset, setReset] = React.useState(2); const [reset, setReset] = React.useState(2);
const [update, setUpdate] = React.useState(); const [update, setUpdate] = useState<IPos[]>([]);
const [triggerUpdate, setTriggerUpdate] = useState<number>(2); const [triggerUpdate, setTriggerUpdate] = useState<number>(2);
const [Data, SetData] = useState<ISourceFilters[]>(); const [Data, SetData] = useState<ISourceFilters[]>();
@@ -72,31 +72,34 @@ export default function SourceMangas(props: { popular: boolean }) {
setTriggerUpdate(1); setTriggerUpdate(1);
return; return;
} }
setFetched(false); if (update.length > 0) {
setMangas([]); const rep = update;
setLastPageNum(0); setUpdate([]);
if (Noreset === undefined && Search) { setNoreset(null); }
}, [triggerUpdate]);
useEffect(() => {
if (update !== undefined) {
const { position, state, group }: IPos = update;
client.post(`/api/v1/source/${sourceId}/filters`, client.post(`/api/v1/source/${sourceId}/filters`,
JSON.stringify(group === undefined ? { rep.map((e: IPos) => {
position, const { position, state, group }: IPos = e;
state, return group === undefined ? {
} : {
position: group,
state: JSON.stringify({
position, position,
state, state,
}), } : {
position: group,
state: JSON.stringify({
position,
state,
}),
};
})) }))
.then(() => { .then(() => {
setTriggerUpdate(0);
makeFilters(); makeFilters();
}); });
} else {
setFetched(false);
setMangas([]);
setLastPageNum(0);
if (Noreset === undefined && Search) { setNoreset(null); }
} }
}, [update]); }, [triggerUpdate]);
useEffect(() => { useEffect(() => {
if (reset === 0) { if (reset === 0) {
@@ -207,6 +210,7 @@ export default function SourceMangas(props: { popular: boolean }) {
resetFilterValue={setReset} resetFilterValue={setReset}
setTriggerUpdate={setTriggerUpdate} setTriggerUpdate={setTriggerUpdate}
setSearch={setSearch} setSearch={setSearch}
update={update}
/> />
)} )}
</> </>