|
1 | | -import React from 'react'; |
| 1 | +import React, { useState } from 'react'; |
2 | 2 | import { useDispatch } from 'react-redux'; |
3 | 3 | import { Types } from '../../store/reducer/example-reducer'; |
4 | | -import { MdDeleteForever } from 'react-icons/md'; |
| 4 | +import { MdDeleteForever, MdEdit, MdDone } from 'react-icons/md'; |
| 5 | +import { parse } from 'marked'; |
| 6 | +import { sanitize } from 'dompurify' |
5 | 7 | import './styles.css'; |
6 | 8 |
|
7 | 9 | export default function ItemList({ item }) { |
8 | 10 |
|
| 11 | + const [newText, setNewText] = useState(item.text) |
| 12 | + const [optionEdit, setOptionEdit] = useState(false) |
9 | 13 | const dispatch = useDispatch(); |
10 | 14 |
|
11 | 15 | function remove() { |
12 | 16 | let id = item.id |
13 | 17 | dispatch({ type: Types.REMOVE, id }) |
14 | 18 | } |
15 | 19 |
|
| 20 | + function edit() { |
| 21 | + setOptionEdit(!optionEdit) |
| 22 | + } |
| 23 | + |
| 24 | + function save() { |
| 25 | + setOptionEdit(!optionEdit) |
| 26 | + let id = item.id |
| 27 | + dispatch({ type: Types.EDIT, data: { id, newText } }) |
| 28 | + } |
| 29 | + |
16 | 30 | return ( |
17 | 31 | <div className="content-wrapper"> |
18 | 32 | <div className="head"> |
19 | 33 | <button type="button" onClick={remove} className="trach"> |
20 | 34 | <MdDeleteForever width={16} /> |
21 | 35 | </button> |
| 36 | + {!optionEdit && |
| 37 | + <button type="button" onClick={edit} className="edit"> |
| 38 | + <MdEdit width={16} /> |
| 39 | + </button> |
| 40 | + } |
| 41 | + {optionEdit && |
| 42 | + <button type="button" onClick={save} className="save"> |
| 43 | + <MdDone width={16} /> |
| 44 | + </button> |
| 45 | + } |
22 | 46 | </div> |
23 | | - <div className="content" dangerouslySetInnerHTML={{ __html: item.text }} /> |
24 | | - <div className="footer"> |
25 | | - <small>Criado em: {item.createAt}</small> |
26 | | - </div> |
| 47 | + {!optionEdit && |
| 48 | + (<span> |
| 49 | + <div className="content" dangerouslySetInnerHTML={{ __html: sanitize(parse(item.text)) }} /> |
| 50 | + <div className="footer"> |
| 51 | + <small>Criado em: {item.createAt}</small> |
| 52 | + </div> |
| 53 | + </span> |
| 54 | + ) |
| 55 | + } |
| 56 | + {optionEdit && |
| 57 | + <textarea |
| 58 | + cols={100} |
| 59 | + rows={10} |
| 60 | + value={newText} |
| 61 | + onChange={e => setNewText(e.target.value)} |
| 62 | + placeholder="Digite sua nota" |
| 63 | + className="textarea" |
| 64 | + /> |
| 65 | + } |
27 | 66 | </div> |
28 | 67 | ); |
29 | 68 | } |
0 commit comments