Skip to content

Commit

Permalink
feat: hook for functions filter 👷
Browse files Browse the repository at this point in the history
  • Loading branch information
ddecrulle committed Aug 18, 2022
1 parent 6740502 commit 46c0976
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 44 deletions.
8 changes: 6 additions & 2 deletions src/ui/components/pages/service/section/blocFunction.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Box, Card } from '@mui/material';
import React from 'react';
import React, { useEffect } from 'react';
import { makeStyles } from 'tss-react/mui';

const BlocFunction = ({ functions }) => {
useEffect(() => {
console.log(functions);
}, [functions]);

const { classes, theme } = useStyles();

return <div> Function</div>;
return <div> {functions}</div>;
};

const useStyles = makeStyles()((theme) => ({
Expand Down
13 changes: 7 additions & 6 deletions src/ui/components/pages/service/serviceOffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import BlocFilter from './section/blocFilter';
import BlocFunction from './section/blocFunction';
import { useTreeUrlStatus } from 'ui/utils/hooks/searchParams';
import { CoreApiContext } from 'ui/coreApi';
import { useFiltredAndOrderedFunctions } from 'ui/utils/hooks/functions';

const ServiceOffer = () => {
const { classes, theme } = useStyles();
Expand All @@ -24,11 +25,11 @@ const ServiceOffer = () => {
});
}, [getFunctions, getHierarchies]);

useEffect(() => {
//functions.filter()
//console.log(findId(functions[0], 'gsbpm'));
console.log(treeState);
}, [treeState]);
const filtredFunctions = useFiltredAndOrderedFunctions(
treeState.selected,
treeState.filtered,
functions
);

return (
<>
Expand All @@ -50,7 +51,7 @@ const ServiceOffer = () => {
<Grid item md={8} xs={12}>
<Box className={classes.box}>
<Card className={classes.card}>
<BlocFunction functions={functions} />
<BlocFunction functions={filtredFunctions} />
</Card>
</Box>
</Grid>
Expand Down
35 changes: 0 additions & 35 deletions src/ui/utils/functions/filterAndOrder.js

This file was deleted.

1 change: 0 additions & 1 deletion src/ui/utils/functions/index.js

This file was deleted.

1 change: 1 addition & 0 deletions src/ui/utils/hooks/functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useFiltredAndOrderedFunctions } from './useFiltredAndOrderedFunctions';
58 changes: 58 additions & 0 deletions src/ui/utils/hooks/functions/useFiltredAndOrderedFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useEffect, useState } from 'react';

export const useFiltredAndOrderedFunctions = (
selected,
filtered,
functions
) => {
//TODEBUG

const [arrayOfIdToFilter, setArrayOfIdToFilter] = useState([]);
const [objOfIdByHierarchy, setObjOfIdByHierarchy] = useState({});
const [filtrerFunctions, setFiltrerFunctions] = useState([]);

const concatArrayWithoutDuplicate = (a, b) => [...new Set(a.concat(b))];

useEffect(() => {
setArrayOfIdToFilter(concatArrayWithoutDuplicate(selected, filtered));
}, [selected, filtered]);

useEffect(() => {
setObjOfIdByHierarchy(
arrayOfIdToFilter.reduce((acc, curr) => {
const keyword = curr.split('-')[0];
return keyword in acc
? { ...acc, [keyword]: [...acc[keyword], curr] }
: { ...acc, [keyword]: [curr] };
}, {})
);
}, [arrayOfIdToFilter]);

useEffect(() => {
const searchArray = (fn) => (array, nodeKey) =>
Array.isArray(array)
? array.length === 0
? false
: searchArray(fn)(array[0], nodeKey) ||
searchArray(fn)(array.slice(1), nodeKey)
: searchTree(fn)(array, nodeKey);

const searchTree = (fn) => (obj, nodeKey) =>
fn(obj, () =>
obj[nodeKey] == null ? false : searchArray(fn)(obj[nodeKey], nodeKey)
);

const findId = (arrayTarget, obj, nodeKey) =>
searchArray((node, next) =>
arrayTarget.includes(node.id) ? true : next()
)(obj, nodeKey);

setFiltrerFunctions(
Object.entries(objOfIdByHierarchy).forEach(([key, value]) =>
functions.filter((fct) => findId(value, fct, key))
)
);
}, [functions, objOfIdByHierarchy]);

return filtrerFunctions;
};

0 comments on commit 46c0976

Please sign in to comment.