Advertisement
IlanZiin

#49, #50, #51, #52, #53, #54 LISTA DE CONTATOS

Sep 28th, 2022 (edited)
1,580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. App.jsx
  2. -----------------------
  3. import React, { useEffect, useRef, useState } from 'react'
  4. import Contato from './components/Contato'
  5. import { v4 as uuid } from 'uuid'
  6.  
  7. export default function App(){
  8.  
  9.     const [contato, setContato] = useState({nome: '', telefone: ''})
  10.     const [listaContatos, setListaContatos] = useState([])
  11.  
  12.     const inputNome = useRef()
  13.     const inputTelefone = useRef()
  14.  
  15.     function definirNome(event){
  16.         setContato({...contato, nome: event.target.value})
  17.     }
  18.  
  19.     function definirTelefone(event){
  20.         setContato({...contato, telefone: event.target.value})
  21.     }
  22.  
  23.     function adicionarContato(){
  24.         if(contato.nome === "" || contato.telefone === "")
  25.             return
  26.  
  27.         // se o contato jรก existe
  28.         let duplicado = listaContatos.find((ct) => ct.nome === contato.nome && ct.telefone === contato.telefone)
  29.         if(typeof duplicado !== 'undefined'){
  30.             inputTelefone.current.focus()
  31.             return
  32.         }
  33.  
  34.         //adicionar contato ร  lista
  35.         setListaContatos([...listaContatos, contato])
  36.  
  37.         // clear
  38.         setContato({nome: '', telefone: ''})
  39.  
  40.         // focus
  41.         inputNome.current.focus()
  42.     }
  43.  
  44.     function enterAdicionarContato(event){
  45.         if(event.code === 'Enter'){
  46.             adicionarContato()
  47.         }
  48.     }
  49.  
  50.     // carregar lista de contato no localstorage
  51.     useEffect(() => {
  52.         if(localStorage.getItem('meus_contatos') !== null){
  53.             setListaContatos(JSON.parse(localStorage.getItem('meus_contatos')))
  54.         }
  55.     }, [])
  56.    
  57.     // persistencia do state (atualizar lista de contato no localstorage)
  58.     useEffect(() => {
  59.         localStorage.setItem('meus_contatos', JSON.stringify(listaContatos))
  60.     }, [listaContatos])
  61.  
  62.     return (
  63.         <>
  64.             <h1>Minha lista de contato!</h1>
  65.             <hr />
  66.             <div>
  67.                 <label htmlFor="text_nome">Nome: </label><br/>
  68.                 <input type="text" id="text_nome" ref={inputNome} onChange={definirNome} value={contato.nome} />
  69.             </div>
  70.             <div>
  71.                 <label htmlFor="text_telefone">Telefone: </label><br/>
  72.                 <input type="text" id="text_telefone" ref={inputTelefone} onChange={definirTelefone} onKeyUp={enterAdicionarContato} value={contato.telefone} />
  73.             </div>
  74.             <button onClick={adicionarContato}>Adicionar contato</button>
  75.             <hr />
  76.  
  77.             {listaContatos.map(ct => {
  78.                 return <Contato key={uuid()} nome={ct.nome} telefone={ct.telefone} />
  79.             })}
  80.         </>
  81.     )
  82. }
  83. ---------------------------
  84. components/Contato.jsx
  85. ----------------------------
  86. import React from 'react'
  87.  
  88. export default function Contato(props){
  89.     return (
  90.         <div>
  91.             {`${props.nome} | ${props.telefone}`}
  92.         </div>
  93.     )
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement