Skip to content

Commit 7beaaf0

Browse files
committed
Criando modelo Nota e separando a exibição em um novo componente
1 parent db4a119 commit 7beaaf0

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

src/components/ComponentList/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import React, { useState } from 'react';
22
import { useSelector, useDispatch } from 'react-redux';
33
import { Types } from '../../store/reducer/example-reducer';
44

5+
import Note from '../../models/Note';
6+
import ItemList from '../ItemList'
7+
58
export default function ComponentList() {
69

710
const qty = 20
@@ -13,7 +16,9 @@ export default function ComponentList() {
1316
const dispatch = useDispatch();
1417

1518
function add() {
16-
dispatch({ type: Types.ADD, title: text })
19+
let note = new Note(text)
20+
dispatch({ type: Types.ADD, note })
21+
setText('')
1722
}
1823

1924
return (
@@ -26,7 +31,7 @@ export default function ComponentList() {
2631
/>
2732
<button type="button" onClick={add}>ADD</button>
2833
<ul>
29-
{list.map(item => <li key={item}>{item}</li>)}
34+
{list.map(item => <li key={item.id}><ItemList item={item} /> </li>)}
3035
</ul>
3136
</>
3237
);

src/components/ItemList/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
3+
// import { Container } from './styles';
4+
5+
export default function ItemList({ item }) {
6+
return (
7+
<span>
8+
{item.text} - {item.createAt}
9+
</span>
10+
);
11+
}

src/models/Note.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import crypto from 'crypto';
2+
3+
export default class Note {
4+
5+
constructor(text) {
6+
this.id = crypto.randomBytes(4).toString('HEX')
7+
this.text = text
8+
this.createAt = new Date().toLocaleDateString()
9+
}
10+
11+
}

src/store/reducer/example-reducer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import { createActions, createReducer } from 'reduxsauce';
55
* Ex.: Type.ADD e add = { type: Type.ADD, params }
66
*/
77
export const { Types, Creators } = createActions({
8-
add: ["title"]
8+
add: ["note"]
99
})
1010

1111
const INITIAL_STATE = {
1212
data: []
1313
}
1414

1515
const add = (state = INITIAL_STATE, action) => {
16-
return { ...state, data: [...state.data, action.title] }
16+
return { ...state, data: [...state.data, action.note] }
1717
}
1818

1919
/**

0 commit comments

Comments
 (0)