|
@@ -0,0 +1,119 @@
|
|
|
+import React from 'react'
|
|
|
+import './style.css'
|
|
|
+import {useReducer, useEffect} from 'react'
|
|
|
+import userData from "../../data/USersData/UsersData";
|
|
|
+
|
|
|
+let id = Math.random() * 1000000000
|
|
|
+
|
|
|
+let newArr = [...userData]
|
|
|
+const initialStore = {
|
|
|
+ users: []
|
|
|
+}
|
|
|
+
|
|
|
+let getUsersFetch = () => {
|
|
|
+ return new Promise((res) => {
|
|
|
+ res(userData)
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+const reducer = (store, action) => {
|
|
|
+
|
|
|
+ switch (action.type) {
|
|
|
+ case 'GET_USERS':
|
|
|
+ return {
|
|
|
+ ...store,
|
|
|
+ users: action.payload
|
|
|
+ }
|
|
|
+ case 'DEL_USER':
|
|
|
+ const index = store.users.indexOf(action.payload);
|
|
|
+ if (index > -1) {
|
|
|
+ store.users.splice(index, 1);
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ ...store,
|
|
|
+ users: store.users
|
|
|
+ }
|
|
|
+ case 'FIND_USER':
|
|
|
+ return {
|
|
|
+ ...store,
|
|
|
+ users: action.payload
|
|
|
+ }
|
|
|
+ case 'CREATE_USER':
|
|
|
+ return {
|
|
|
+ store,
|
|
|
+ users: action.payload
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ return store;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const Users = () => {
|
|
|
+ const [store, dispatch] = useReducer(reducer, initialStore)
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ getUsersFetch().then((users) => {
|
|
|
+ dispatch({type: 'GET_USERS', payload: users})
|
|
|
+ })
|
|
|
+ }, [])
|
|
|
+
|
|
|
+ const HandleFilterByName = event => {
|
|
|
+ const {value} = event.target
|
|
|
+ const result = newArr.filter(user => {
|
|
|
+ return user.name.toLocaleLowerCase().includes(value.toLocaleLowerCase())
|
|
|
+ })
|
|
|
+ dispatch({type: 'FIND_USER', payload: result})
|
|
|
+ }
|
|
|
+
|
|
|
+ function createUser(event){
|
|
|
+ event.preventDefault()
|
|
|
+ let name = event.target.elements.name.value;
|
|
|
+ let age = event.target.elements.age.value
|
|
|
+ let gender = event.target.elements.gender.value
|
|
|
+ const newArray =[...store.users.concat({
|
|
|
+ _id: id++,
|
|
|
+ name: name,
|
|
|
+ age: age,
|
|
|
+ gender: gender
|
|
|
+ })]
|
|
|
+ dispatch({type:'CREATE_USER',payload: newArray})
|
|
|
+ }
|
|
|
+
|
|
|
+ return (
|
|
|
+ <>
|
|
|
+ <div className='allInfo'>
|
|
|
+ <form onSubmit={createUser} className='form'>
|
|
|
+ <input name='name' placeholder="Enter name..." type="text"/>
|
|
|
+ <input name='age' placeholder="Enter age..." type="text"/>
|
|
|
+ <input name='gender' placeholder="Enter gender..." type="text"/>
|
|
|
+ <button>Create user</button>
|
|
|
+ </form>
|
|
|
+ <div>
|
|
|
+ <div className='allCards'>
|
|
|
+ <div className='forInput'>
|
|
|
+ <input type="text" placeholder='Enter name...' onChange={HandleFilterByName}/>
|
|
|
+ </div>
|
|
|
+ {
|
|
|
+ store.users.map(user => {
|
|
|
+ return (
|
|
|
+ <div className="card" key={id++}>
|
|
|
+ <img src={'https://i1.sndcdn.com/artworks-000580655825-uvkn3k-t500x500.jpg'}
|
|
|
+ alt="Аватар"/>
|
|
|
+ <h1>{user.name}</h1>
|
|
|
+ <p>{user.age}</p>
|
|
|
+ <p>{user.balance}</p>
|
|
|
+ <p>{user.gender}</p>
|
|
|
+ <button onClick={() => dispatch({type: 'DEL_USER', payload: user})}>delete</button>
|
|
|
+ </div>
|
|
|
+ )
|
|
|
+ })
|
|
|
+ }
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ <footer className='footer'>Total user count {store.users.length}</footer>
|
|
|
+ </>
|
|
|
+ )
|
|
|
+}
|
|
|
+export default Users
|