Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. import React, { useState, useEffect } from 'react'
  2. import blogService from '../services/blogs'
  3. import userService from '../services/users'
  4. import Togglable from './Togglable'
  5. import BlogForm from './BlogForm'
  6. import Notification from './Notification'
  7.  
  8. const BlogDisplay = (props) => {
  9. const [blogs, setBlogs] = useState([])
  10. const [notification, setNotification] = useState(null)
  11. const [errorMessage, setErrorMessage] = useState(null)
  12. const blogFormRef = React.createRef()
  13.  
  14. useEffect(() => {
  15. blogService.getAll().then(blogs =>
  16. setBlogs(blogs)
  17. )
  18. }, [])
  19.  
  20.  
  21. const blogStyle = {
  22. paddingTop: 10,
  23. paddingLeft: 2,
  24. border: 'solid',
  25. borderWidth: 1,
  26. marginBottom: 5,
  27. }
  28. const showBlogs = () => {
  29.  
  30. return (
  31. blogs.sort((b1, b2) => b2.likes - b1.likes).map(b =>
  32. contentVisibility(b)
  33. )
  34. )
  35.  
  36. }
  37. const addLike = async (blog) => {
  38. let returnedBlog = await blogService.update({
  39. user: blog.user.id,
  40. title: blog.title,
  41. author: blog.author,
  42. url: blog.url,
  43. likes: blog.likes + 1,
  44. visible: blog.visible,
  45. }, blog.id)
  46. setBlogs(blogs.map(b => b.id !== blog.id ? b : returnedBlog))
  47. }
  48.  
  49. const contentVisibility = (blog) => {
  50. console.log('LISÄÄJÄ' + blog.user.username)
  51.  
  52. if (blog.visible === false) {
  53. return (
  54. <div key={blog.id} style={blogStyle}>
  55. <div>{blog.title} {blog.author} <button onClick={() => handleToggleVisibility(blog)}>view</button>
  56. </div>
  57. </div>
  58. )
  59. } else {
  60. return (
  61. <div key={blog.id} style={blogStyle}>
  62. <div>{blog.title} {blog.author} <button onClick={() => handleToggleVisibility(blog)}>hide</button></div>
  63. <div>{blog.url} </div>
  64. <div>likes {blog.likes} <button onClick={() => addLike(blog)}>like</button> </div>
  65. <div>added by {blog.user.username} </div>
  66. <div>{props.user.username === blog.user.username && <button onClick={() => deleteBlog(blog)}>delete</button>} <br /></div>
  67. </div>
  68. )
  69. }
  70.  
  71. }
  72. const handleToggleVisibility = async (blog) => {
  73. console.log(blog)
  74. let returnedBlog = await blogService.update({
  75. user: blog.user.id,
  76. title: blog.title,
  77. author: blog.author,
  78. url: blog.url,
  79. likes: blog.likes,
  80. visible: !blog.visible,
  81. }, blog.id)
  82. console.log('TÄMÄ' + returnedBlog.user)
  83. setBlogs(blogs.map(b => b.id !== blog.id ? b : returnedBlog))
  84.  
  85. }
  86. const addBlog = async (blogObject) => {
  87. let users = await userService.getAll()
  88. let user = users.filter(u => u.username === props.user.username)
  89. console.log(user)
  90. blogFormRef.current.toggleVisibility()
  91. try {
  92. let b = await blogService.create({
  93. title: blogObject.title,
  94. author: blogObject.author,
  95. url: blogObject.url,
  96. visible: false,
  97. })
  98. setBlogs(blogs.concat(b))
  99. setNotification(`a new blog ${b.title} by ${b.author} was added`)
  100. setTimeout(() => {
  101. setNotification(null)
  102. }, 5000)
  103. } catch (exception) {
  104. console.log(blogObject)
  105. setErrorMessage('something went wrong')
  106. setTimeout(() => {
  107. setErrorMessage(null)
  108. }, 5000)
  109. }
  110. }
  111. const deleteBlog = async (blogObject) => {
  112. let result = window.confirm("Are you sure you want to delete this blog?");
  113. if (result) {
  114. try {
  115. let b = await blogService.remove(blogObject.id)
  116. setBlogs(blogs.filter(b => b.id !== blogObject.id))
  117. setErrorMessage(`a new blog ${b.title} by ${b.author} was deleted`)
  118. setTimeout(() => {
  119. setErrorMessage(null)
  120. }, 5000)
  121. } catch (exception) {
  122. console.log(blogObject)
  123. setErrorMessage('something went wrong')
  124. setTimeout(() => {
  125. setErrorMessage(null)
  126. }, 5000)
  127. }
  128. }
  129.  
  130. }
  131. const showBlogForm = () => {
  132. return (
  133. <div>
  134. <h2>Add new</h2>
  135. <Togglable buttonLabel="new blog" ref={blogFormRef}>
  136. {props.user !== null && <BlogForm createNewBlog={addBlog} />}
  137. </Togglable>
  138. </div>
  139. )
  140. }
  141. return (
  142. <>
  143. <Notification message={errorMessage} type="warning" />
  144. <Notification message={notification} type="success" />
  145. {props.user !== null && showBlogForm()}
  146. {props.user !== null && showBlogs()}
  147. </>
  148.  
  149. )
  150.  
  151. }
  152. export default BlogDisplay
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement