Guest User

Untitled

a guest
May 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. import React, {Component} from 'react'
  2. import {connect} from 'react-redux'
  3. import {bindActionCreators} from 'redux'
  4. import Grid from '../template/grid'
  5. import {add, clear, changeDescription, changeUrl, changeTag} from
  6. './bookmarkActions'
  7. import IconButton from '../template/iconButton'
  8.  
  9. class BookmarkForm extends Component {
  10.  
  11. render(){
  12. const {description, url, nameTag} = this.props
  13. return (
  14. <div role='form' className='bookmarkForm'>
  15. <Grid cols='12 9 3'>
  16. <input id='description' className='form-control'
  17. placeholder='Nome do favorito' type='text'
  18. onChange={this.props.changeDescription}
  19. value={this.props.description}></input>
  20. </Grid>
  21.  
  22. <Grid cols='12 9 3'>
  23. <input id='url' className='form-control'
  24. placeholder='URL' type='text'
  25. onChange={this.props.changeUrl}
  26. value={this.props.url}></input>
  27. </Grid>
  28.  
  29. <Grid cols='12 9 3'>
  30. <input id='tag' className='form-control'
  31. placeholder='Tags' type='text'
  32. onChange={this.props.changeTag}
  33. value={this.props.nameTag}></input>
  34. </Grid>
  35.  
  36. <Grid cols='12 3 3'>
  37. <IconButton style='primary' icon='plus'
  38. onClick={this.props.add}></IconButton>
  39.  
  40. <IconButton style='default' icon='close'
  41. onClick={this.props.clear}></IconButton>
  42. </Grid>
  43. </div>
  44. )
  45. }
  46. }
  47.  
  48. const mapStateToProps = state => ({description: state.bookmark.description,
  49. url: state.bookmark.url , nametag: state.bookmark.nameTag})
  50. const mapDispatchToProps = dispatch => bindActionCreators({add, clear,
  51. changeDescription, changeUrl, changeTag}, dispatch)
  52.  
  53. export default connect(mapStateToProps, mapDispatchToProps)(BookmarkForm)
  54.  
  55. import React, {Component} from 'react'
  56. import {connect} from 'react-redux'
  57. import {bindActionCreators} from 'redux'
  58.  
  59. import IconButton from '../template/iconButton'
  60. import Tag from '../template/tag'
  61. import {remove} from './bookmarkActions'
  62.  
  63. const BookmarkList = props => {
  64.  
  65. const renderRows = () => {
  66. const list = props.list || []
  67. return list.map((bookmark,index) => (
  68. <tr key={index}>
  69. <td>{bookmark.description}</td>
  70. <td><a href={'http://' + `${bookmark.url}`}>{bookmark.url}</a></td>
  71. <td>{bookmark.nameTag}</td>
  72. <td>
  73. <IconButton style='danger' icon='trash-o'
  74. onClick={props.remove}></IconButton>
  75. </td>
  76. </tr>
  77. ))
  78. }
  79.  
  80. return (
  81. <table className='table'>
  82. <thead>
  83. <tr>
  84. <th>Nome</th>
  85. <th>URL</th>
  86. <th>Tags</th>
  87. <th className='tableActions'>Ações</th>
  88. </tr>
  89. </thead>
  90. <tbody>
  91. {renderRows()}
  92. </tbody>
  93. </table>
  94. )
  95. }
  96.  
  97.  
  98. const mapStateToProps = state => ({list: state.bookmark.list})
  99. const mapDispatchToProps = dispatch => bindActionCreators({remove},
  100. dispatch)
  101. export default connect(mapStateToProps, mapDispatchToProps)(BookmarkList)
  102.  
  103. export const changeDescription = (event) => ({
  104. type: 'DESCRIPTION_CHANGED',
  105. payload: event.target.value
  106. })
  107.  
  108. export const changeUrl = (event) => ({
  109. type: 'URL_CHANGED',
  110. payload: event.target.value
  111. })
  112.  
  113. export const changeTag = (event) => ({
  114. type: 'TAG_CHANGED',
  115. payload: event.target.value
  116. })
  117.  
  118. export function add() {
  119. return {type: 'BOOKMARK_ADDED'}
  120. }
  121.  
  122. export function remove() {
  123. return {type: 'BOOKMARK_REMOVED'}
  124. }
  125.  
  126. export function clear() {
  127. return {type: 'BOOKMARK_CLEAR'}
  128. }
  129.  
  130. const INITIAL_STATE = {description: '', url: '', nameTag: '', list: [] }
  131.  
  132. export default (state = INITIAL_STATE, action) => {
  133. switch(action.type) {
  134. case 'DESCRIPTION_CHANGED':
  135. return {...state, description: action.payload}
  136. case 'URL_CHANGED':
  137. return {...state, url: action.payload}
  138. case 'TAG_CHANGED':
  139. return {...state, nameTag: action.payload}
  140. case 'BOOKMARK_ADDED':
  141. return {...state, list: [...state.list, {description: state.description, url: state.url, nameTag: state.nameTag }]}
  142. case 'BOOKMARK_REMOVED':
  143. return {...state, list: []}
  144. case 'BOOKMARK_CLEAR':
  145. return {...state, description: '', url: '', nameTag: ''}
  146. default:
  147. return state
  148. }
  149. }
Add Comment
Please, Sign In to add comment