const days = ["yesterday", "today", "tomorrow"];
class App extends React.Component {
state = {
day: 1,
data: [],
filteredData: [],
search: "",
shift: "first"
};
componentDidMount() {
this.fetchData();
}
fetchData = async () => {
const response = await fetch(`/data/${days[this.state.day]}.json`);
const data = (await response.json()).group;
this.setState({ data, shift: Object.keys(data)[0] }, this.filter);
};
loadDay = day => {
this.setState({ day }, this.fetchData);
};
updateSearch = e => {
this.setState({
search: e.target.value
});
};
filter = () => {
this.setState(({ search, data, shift }) => {
const s = search.toLowerCase();
return {
filteredData: data[shift].filter(n =>
n.firstName.toLowerCase().includes(s)
)
};
});
};
onClick = event => {
const shift = event.target.dataset.shift; //эта часть
this.setState(() => ({ shift }), this.filter);
};
render() {
const { search, shift, data, filteredData } = this.state;
return (
<div>
<TableSearch
value={search}
onChange={this.updateSearch}
onSearch={this.filter}
/>
{days.map((day, i) => (
<button
key={day}
onClick={() => this.loadDay(i)}
className={i === this.state.day ? "active" : ""}
>
{day}
</button>
))}
<br />
{Object.keys(data).map(n => (
<button
data-shift={n}
onClick={this.onClick}
className={n === shift ? "active" : ""}
>
{n} shift
</button>
))}
<TableData data={filteredData} />
</div>
);
}
}
export default App;