Advertisement
Guest User

Untitled

a guest
Apr 25th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useRef, useReducer, useState } from 'react'
  2.  
  3. function App() {
  4.  
  5.     const inputRef = useRef<HTMLInputElement | any>(null)
  6.  
  7.     const handleSubmit = (e: any) => {
  8.         e.preventDefault()
  9.         if (inputRef.current?.value !== "") {
  10.             dispatch({
  11.                 type: 'ADD_TODO',
  12.                 payload: inputRef.current?.value,
  13.                 id: editingIndex
  14.             })
  15.         }
  16.         inputRef.current && (inputRef.current.value = "")
  17.     }
  18.  
  19.     const [editingIndex, setEditingIndex] = useState<null | number>(null)
  20.  
  21.     const [todo, dispatch] = useReducer((state: any, action: any): any => {
  22.         switch (action.type) {
  23.             case 'ADD_TODO':
  24.                 setEditingIndex(null)
  25.                 const tempState = [...state]
  26.                 if (action.id) {
  27.                     tempState[action.id] = { ...tempState[action.id], name: action.payload }
  28.                 }
  29.                 else {
  30.                     tempState.push({
  31.                         id: action.id | state.length,
  32.                         name: action.payload,
  33.                         isCheck: false,
  34.                         toggleAll: false
  35.                     })
  36.                 }
  37.                 return tempState
  38.             case 'CHECK_TODO':
  39.                 return state.filter((item: any, index: any): any => {
  40.                     if (index === action.id) {
  41.                         item.isCheck = !item.isCheck
  42.                     }
  43.                     return item
  44.                 })
  45.             case 'EDIT_TODO':
  46.                 inputRef.current.focus()
  47.                 inputRef.current.value = action.payload
  48.                 return state
  49.             case 'DELETE_TODO':
  50.                 return state.filter((item: any, index: any) => index !== action.id)
  51.             case 'CLEAR_TODOS':
  52.                 return []
  53.             case 'ON_OFF':
  54.                 const newState = state.map((item: any) => {
  55.                     item.toggleAll = !item.toggleAll
  56.                     return item
  57.                 })
  58.                 return newState.map((item: any) => {
  59.                     item.isCheck = item.toggleAll
  60.                     return item
  61.                 })
  62.             case 'SELECT':
  63.                 switch (action.option) {
  64.                     case 'all':
  65.                         const tempState1 = [...state]
  66.                         return tempState1
  67.                     case 'complete':
  68.                         const tempState2 = [...state]
  69.                         return tempState2.filter((i: any) => i.isCheck === true)
  70.                     case 'active':
  71.                         const tempState3 = [...state]
  72.                         return tempState3.filter((i: any) => i.isCheck === false)
  73.                     default:
  74.                         return state
  75.                 }
  76.         }
  77.     }, [])
  78.  
  79.     const handleEditing = (index: number, item: { name: string }) => {
  80.         setEditingIndex(index);
  81.         dispatch({
  82.             type: 'EDIT_TODO',
  83.             id: index,
  84.             payload: item.name
  85.         })
  86.     }
  87.  
  88.     const todos = todo.map((item: any, index: number) => {
  89.         return (
  90.             <li key={index}>
  91.                 <input
  92.                     type="checkbox"
  93.                     checked={item.isCheck}
  94.                     onChange={() => dispatch({ type: 'CHECK_TODO', id: index })}
  95.                 />
  96.                 {item.name}
  97.                 <button onClick={() => handleEditing(index, item)}>/</button>
  98.                 <button onClick={() => dispatch({ type: 'DELETE_TODO', id: index })}>x</button>
  99.             </li>
  100.         )
  101.     })
  102.  
  103.     return (
  104.         <div>
  105.             <form onSubmit={handleSubmit}>
  106.                 <input
  107.                     type="text"
  108.                     placeholder='Buy milk'
  109.                     ref={inputRef}
  110.                 />
  111.             </form>
  112.             <button onClick={() => dispatch({ type: 'CLEAR_TODOS' })}>Clear</button>
  113.             <button onClick={() => dispatch({ type: 'ON_OFF' })}>On/Off</button>
  114.  
  115.             <select onChange={(e: any) => dispatch({ type: 'SELECT', option: e.target.value })}>
  116.                 <option value="choose" selected>Choose</option>
  117.                 <option value="all">All</option>
  118.                 <option value="active">Active</option>
  119.                 <option value="complete">Complete</option>
  120.             </select>
  121.             <ul>{todos}</ul>
  122.         </div>
  123.     )
  124. }
  125.  
  126. export default App
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement