Skip to content

Commit 0f046cc

Browse files
committed
Editando uma nota
1 parent ddfc128 commit 0f046cc

File tree

5 files changed

+100
-11
lines changed

5 files changed

+100
-11
lines changed

src/components/ComponentList/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ export default function ComponentList() {
2424

2525
function add() {
2626
if (text.length > 0) {
27-
let mdText = sanitize(marked.parse(text))
28-
let note = new Note(mdText)
27+
let note = new Note(text)
2928
dispatch({ type: Types.ADD, note })
3029
setText('')
3130
setPreviewText('')
@@ -67,6 +66,7 @@ export default function ComponentList() {
6766
value={text}
6867
onChange={e => setText(e.target.value)}
6968
placeholder="Digite sua nota"
69+
className="textarea"
7070
/>
7171
</div>
7272
<div className={`preview ${hide()}`} dangerouslySetInnerHTML={{ __html: previewText }} />

src/components/ComponentList/styles.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
justify-content: center;
77
}
88

9-
.content-add textarea {
9+
.textarea {
1010
padding: 10px;
1111
font-size: 18px;
1212
border-radius: 3px;

src/components/ItemList/index.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,68 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import { useDispatch } from 'react-redux';
33
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'
57
import './styles.css';
68

79
export default function ItemList({ item }) {
810

11+
const [newText, setNewText] = useState(item.text)
12+
const [optionEdit, setOptionEdit] = useState(false)
913
const dispatch = useDispatch();
1014

1115
function remove() {
1216
let id = item.id
1317
dispatch({ type: Types.REMOVE, id })
1418
}
1519

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+
1630
return (
1731
<div className="content-wrapper">
1832
<div className="head">
1933
<button type="button" onClick={remove} className="trach">
2034
<MdDeleteForever width={16} />
2135
</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+
}
2246
</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+
}
2766
</div>
2867
);
2968
}

src/components/ItemList/styles.css

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,42 @@ span {
2323
font-weight: bold;
2424
}
2525

26+
.edit {
27+
text-decoration: none;
28+
border: none;
29+
padding: 7px 10px;
30+
31+
-webkit-box-shadow: -1px 3px 15px -2px rgba(209,209,209,1);
32+
-moz-box-shadow: -1px 3px 15px -2px rgba(209,209,209,1);
33+
box-shadow: -1px 3px 15px -2px rgba(209,209,209,1);
34+
35+
margin: 3px;
36+
border-radius: 21px;
37+
color: #434343;
38+
cursor: pointer;
39+
text-transform: uppercase;
40+
font-size: 16px;
41+
font-weight: bold;
42+
}
43+
44+
.save {
45+
text-decoration: none;
46+
border: none;
47+
padding: 7px 10px;
48+
49+
-webkit-box-shadow: -1px 3px 15px -2px rgba(209,209,209,1);
50+
-moz-box-shadow: -1px 3px 15px -2px rgba(209,209,209,1);
51+
box-shadow: -1px 3px 15px -2px rgba(209,209,209,1);
52+
53+
margin: 3px;
54+
border-radius: 21px;
55+
color: #008002;
56+
cursor: pointer;
57+
text-transform: uppercase;
58+
font-size: 16px;
59+
font-weight: bold;
60+
}
61+
2662
.content-wrapper {
2763
width: 100%;
2864
}

src/store/reducer/example-reducer.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { createActions, createReducer } from 'reduxsauce';
66
*/
77
export const { Types, Creators } = createActions({
88
add: ["note"],
9-
remove: ["id"]
9+
remove: ["id"],
10+
edit: ["data"]
1011
})
1112

1213
const INITIAL_STATE = {
@@ -27,6 +28,18 @@ const remove = (state = INITIAL_STATE, action) => {
2728
return { ...state, data: newData }
2829
}
2930

31+
const edit = (state = INITIAL_STATE, action) => {
32+
let newData = state.data.map(note => {
33+
if (action.data.id === note.id) {
34+
note.text = action.data.newText
35+
}
36+
return note
37+
})
38+
localStorage.clear()
39+
localStorage.setItem('list', JSON.stringify(newData))
40+
return { ...state, data: newData }
41+
}
42+
3043
/**
3144
* Criando os reducers
3245
* Ex.: [Types.ADD]: add == switch actions.type
@@ -38,5 +51,6 @@ const remove = (state = INITIAL_STATE, action) => {
3851

3952
export default createReducer(INITIAL_STATE, {
4053
[Types.ADD]: add,
41-
[Types.REMOVE]: remove
54+
[Types.REMOVE]: remove,
55+
[Types.EDIT]: edit
4256
})

0 commit comments

Comments
 (0)