Disclaimer: I know the topic is very unspecific. But I don't know how to describe my issue better. Feel free to help me in the comments and I'll update it later.
I fetch an array of objects coming from a MySQL database to my React App Component. It has this structure:
const fetchedData = [
{
"id": 46923,
"type": "Change Request",
"business": "Systems",
"owner": "Max",
"created_on": "2019-08-16T05:39:00.000Z",
"many more": "values",
},
{
"id": 46924,
"type": "Change Notice",
"business": "Tools",
"owner": "Max",
"created_on": "2019-09-06T11:19:00.000Z",
"many more": "values",
},
]
The type can have three values only:
- Change Request
- Change Notice
- Change Task
The business can have three values only:
- Systems
- Tools
- Misc
I have a react component that has checkboxes for each value:
function ControlBoard({ applyFilter }) {
const classes = useStyles();
const [state, setState] = useState({
business: {
systems: false,
tools: false,
misc: false,
},
type: {
changerequest: false,
changenotice: false,
changetask: false,
},
});
const handleChange = (event) => {
const split = event.target.name.split("|");
const group = split[0];
const name = split[1];
let prop = state;
prop[group][name] = event.target.checked;
setState({ ...prop });
};
const setFilter = () => {
applyFilter(state);
};
useEffect(() => {
setFilter();
}, [state]);
return (
<div className={classes.root}>
<div className={classes.title}>Businesses</div>
<div className={classes.items}>
<div className={classes.item}>
<FormControlLabel
control={
<Checkbox
checked={state.business.systems}
onChange={handleChange}
name="business|systems"
color="primary"
/>
}
label="Systems"
/>
</div>
<div className={classes.item}>
<FormControlLabel
control={
<Checkbox
checked={state.business.tools}
onChange={handleChange}
name="business|tools"
color="primary"
/>
}
label="Tools"
/>
</div>
<div className={classes.item}>
<FormControlLabel
control={
<Checkbox
checked={state.business.misc}
onChange={handleChange}
name="business|misc"
color="primary"
/>
}
label="Misc"
/>
</div>
</div>
<div className={classes.title}>Type</div>
<div className={classes.items}>
<div className={classes.item}>
<FormControlLabel
control={
<Checkbox
checked={state.type.changerequest}
onChange={handleChange}
name="type|changerequest"
color="primary"
/>
}
label="Change Request"
/>
</div>
<div className={classes.item}>
<FormControlLabel
control={
<Checkbox
checked={state.type.changenotice}
onChange={handleChange}
name="type|changenotice"
color="primary"
/>
}
label="Change Notice"
/>
</div>
<div className={classes.item}>
<FormControlLabel
control={
<Checkbox
checked={state.type.changetask}
onChange={handleChange}
name="type|changetask"
color="primary"
/>
}
label="Change Task"
/>
</div>
</div>
</div>
);
}
export default ControlBoard;
Thus I have my state object:
{
business: {
systems: false,
tools: false,
misc: false,
},
type: {
changerequest: false,
changenotice: false,
changetask: false,
},
}
How can I filter my fetchedData array against my state? Each combination of boolean values (including multiple true values of business and type) is possible. Later there will be more values than business and type.
To check against the fetched data values it could be possible to use e.g.:
state.type[0].toUpperCase() === fetchedDataRow.type.toUpperCase().replace(" ", "")