diff --git a/classes/src/App.js b/classes/src/App.js index 5985a41..1631388 100644 --- a/classes/src/App.js +++ b/classes/src/App.js @@ -1,11 +1,22 @@ -import Users from './components/Users'; +import UserFinder from './components/UserFinder'; +import UsersContext from './store/users-context'; + +const DUMMY_USERS = [ + { id: 'u1', name: 'Max' }, + { id: 'u2', name: 'Manuel' }, + { id: 'u3', name: 'Julie' }, +]; function App() { + const usersContext = { + users: DUMMY_USERS + } + return ( -
- -
+ + + ); } -export default App; +export default App; \ No newline at end of file diff --git a/classes/src/components/User.js b/classes/src/components/User.js index 0ea3ed9..6fe428f 100644 --- a/classes/src/components/User.js +++ b/classes/src/components/User.js @@ -2,6 +2,9 @@ import React from "react"; import classes from "./User.module.css"; class User extends React.Component { + componentWillUnmount(){ + console.log("User will unmount") + } render() { return
  • {this.props.name}
  • ; } diff --git a/classes/src/components/UserFinder.js b/classes/src/components/UserFinder.js new file mode 100644 index 0000000..956cf4d --- /dev/null +++ b/classes/src/components/UserFinder.js @@ -0,0 +1,47 @@ +import { Component, Fragment } from "react"; +import UsersContext from "../store/users-context"; +import classes from "./UserFinder.module.css"; +import Users from "./Users"; + +class UserFinder extends Component { + static contextType = UsersContext; + + constructor() { + super(); + this.state = { + filteredUsers: [], + searchTerm: "", + }; + } + + componentDidMount() { + this.setState({ filteredUsers: this.context.users }); + } + + componentDidUpdate(prevProps, prevState) { + if (prevState.searchTerm !== this.state.searchTerm) { + this.setState({ + filteredUsers: this.context.users.filter((user) => + user.name.includes(this.state.searchTerm) + ), + }); + } + } + + searchChangeHandler(event) { + this.setState({ searchTerm: event.target.value }); + } + + render() { + return ( + +
    + +
    + +
    + ); + } +} + +export default UserFinder; diff --git a/classes/src/components/UserFinder.module.css b/classes/src/components/UserFinder.module.css new file mode 100644 index 0000000..b0bbe3b --- /dev/null +++ b/classes/src/components/UserFinder.module.css @@ -0,0 +1,8 @@ +.finder { + margin: 2rem auto; + background-color: white; + padding: 1rem; + border-radius: 12px; + max-width: 40rem; + text-align: center; +} \ No newline at end of file diff --git a/classes/src/components/Users.js b/classes/src/components/Users.js index df7f904..f60b77b 100644 --- a/classes/src/components/Users.js +++ b/classes/src/components/Users.js @@ -1,13 +1,8 @@ -import React, { useState } from "react"; +import React from "react"; import User from "./User"; import classes from "./Users.module.css"; -const DUMMY_USERS = [ - { id: "u1", name: "Max" }, - { id: "u2", name: "Manuel" }, - { id: "u3", name: "Julie" }, -]; class Users extends React.Component { constructor() { @@ -29,7 +24,7 @@ class Users extends React.Component { render() { const usersList = ( diff --git a/classes/src/store/users-context.js b/classes/src/store/users-context.js new file mode 100644 index 0000000..506c941 --- /dev/null +++ b/classes/src/store/users-context.js @@ -0,0 +1,7 @@ +import React from 'react'; + +const UsersContext = React.createContext({ + users: [] +}); + +export default UsersContext; \ No newline at end of file